Skip to content

test: add tests for project-cache and env-file modules#200

Merged
BYK merged 4 commits intomainfrom
byk/test-project-cache-env-file
Feb 5, 2026
Merged

test: add tests for project-cache and env-file modules#200
BYK merged 4 commits intomainfrom
byk/test-project-cache-env-file

Conversation

@BYK
Copy link
Member

@BYK BYK commented Feb 5, 2026

Summary

Add comprehensive tests for two previously uncovered modules, bringing 54 new tests total.

Tests Added

db/project-cache.ts (19 tests)

Function Tests Description
getCachedProject 4 Returns undefined on miss, returns data on hit, different org/project IDs
setCachedProject 5 Creates entries, upserts on conflict, stores timestamps, multiple entries
getCachedProjectByDsnKey 3 Separate key namespace for DSN lookups
setCachedProjectByDsnKey 3 Stores by DSN public key, upsert behavior
clearProjectCache 2 Clears all entries, handles empty cache
Cache key uniqueness 3 Special chars, numeric IDs, separate namespaces

Coverage: 9.90% → 100% (when run individually)

dsn/env-file.ts (35 tests)

Property-based tests (6):

  • Extracts DSN from unquoted, double-quoted, single-quoted values
  • Returns null when no SENTRY_DSN present
  • First DSN wins when multiple present
  • Handles whitespace around equals sign

Edge case unit tests (12):

  • Empty content, only comments, only whitespace
  • Commented out SENTRY_DSN, trailing comments
  • Windows line endings (CRLF)
  • Values with equals signs, empty values
  • SENTRY_DSN_OTHER variations ignored

Integration tests with temp directories (17):

  • detectFromEnvFiles: priority order (.env.local > .env), handles missing files
  • detectFromAllEnvFiles: collects all DSNs, returns source mtimes
  • detectFromMonorepoEnvFiles: scans packages/, apps/ subdirectories
  • ENV_FILES constant: correct priority order

Coverage: 13.73% → 99.19% (when run individually)

Running the Tests

# Run just these tests
bun test test/lib/db/project-cache.test.ts
bun test test/lib/dsn/env-file.test.ts

# Run all unit tests (includes these)
bun run test:unit

Add comprehensive tests for two previously uncovered modules:

## db/project-cache.ts (19 tests)
- getCachedProject: undefined on miss, returns cached data on hit
- setCachedProject: creates entries, upserts on conflict, stores timestamps
- getCachedProjectByDsnKey: separate key namespace for DSN lookups
- setCachedProjectByDsnKey: stores by DSN public key
- clearProjectCache: clears all entries
- Cache key uniqueness and edge cases (special chars, numeric IDs)

## dsn/env-file.ts (35 tests)
Property-based tests:
- Extracts DSN from unquoted, double-quoted, single-quoted values
- Returns null when no SENTRY_DSN present
- Handles whitespace around equals sign

Unit tests for edge cases:
- Empty content, comments, trailing comments
- Windows line endings, values with equals signs
- Multiple SENTRY_DSN declarations (first wins)

Integration tests with temp directories:
- detectFromEnvFiles: priority order (.env.local > .env)
- detectFromAllEnvFiles: collects all DSNs, returns mtimes
- detectFromMonorepoEnvFiles: scans packages/, apps/ subdirs

Coverage improvements (when run individually):
- project-cache.ts: 9.90% → 100%
- env-file.ts: 13.73% → 99.19%
@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Codecov Results 📊

✅ Patch coverage is 100.00%. Project has 1746 uncovered lines.
✅ Project coverage is 77.99%. Comparing base (base) to head (head).

Files with missing lines (34)
File Patch % Lines
human.ts 53.59% ⚠️ 388 Missing
resolve-target.ts 15.28% ⚠️ 366 Missing
oauth.ts 25.10% ⚠️ 194 Missing
api-client.ts 70.62% ⚠️ 176 Missing
upgrade.ts 45.49% ⚠️ 139 Missing
migration.ts 47.44% ⚠️ 82 Missing
telemetry.ts 77.51% ⚠️ 56 Missing
api.ts 89.80% ⚠️ 47 Missing
seer.ts 75.54% ⚠️ 45 Missing
schema.ts 89.56% ⚠️ 40 Missing
seer.ts 79.87% ⚠️ 30 Missing
preload.ts 53.23% ⚠️ 29 Missing
utils.ts 88.94% ⚠️ 25 Missing
detector.ts 90.10% ⚠️ 20 Missing
code-scanner.ts 95.00% ⚠️ 16 Missing
arg-parsing.ts 90.00% ⚠️ 12 Missing
dsn-cache.ts 94.62% ⚠️ 12 Missing
fix.ts 83.61% ⚠️ 10 Missing
fs-utils.ts 57.14% ⚠️ 9 Missing
project-root.ts 97.73% ⚠️ 7 Missing
version-check.ts 91.14% ⚠️ 7 Missing
feedback.ts 84.21% ⚠️ 6 Missing
auth.ts 95.52% ⚠️ 6 Missing
upgrade.ts 93.83% ⚠️ 5 Missing
resolver.ts 94.57% ⚠️ 5 Missing
index.ts 95.96% ⚠️ 4 Missing
project-aliases.ts 97.40% ⚠️ 2 Missing
project-root-cache.ts 96.92% ⚠️ 2 Missing
alias.ts 99.28% ⚠️ 1 Missing
env-file.ts 99.19% ⚠️ 1 Missing
parser.ts 98.63% ⚠️ 1 Missing
colors.ts 97.96% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    77.97%    77.99%    +0.02%
==========================================
  Files           58        58         —
  Lines         7933      7933         —
  Branches         0         0         —
==========================================
+ Hits          6185      6187        +2
- Misses        1748      1746        -2
- Partials         0         0         —

Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Semver Impact of This PR

None (no version bump detected)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


  • test: add tests for project-cache and env-file modules by BYK in #200

🤖 This preview updates automatically when you update the PR.

Address PR review comment - verify exactly 2 DSNs are returned
and check both specific DSN values are present.
- project-cache.test.ts: Use correct env var SENTRY_CONFIG_DIR (not
  SENTRY_CLI_CONFIG_DIR) for proper database isolation between tests

- env-file.test.ts: Move beforeEach/afterEach hooks inside integration
  test describe block so pure function tests don't create unnecessary
  temp directories. Also removed unused SENTRY_CLI_CONFIG_DIR setting
  since env-file functions don't use the database.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Add closeDatabase() call in afterEach to release SQLite WAL-mode file
handles before deleting temp directories. This prevents orphaned temp
directories on Windows where locked files can't be deleted.
@BYK BYK merged commit aa33a20 into main Feb 5, 2026
24 checks passed
@BYK BYK deleted the byk/test-project-cache-env-file branch February 5, 2026 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant