Skip to content

test(daemon): add KeychainSecretStore unit tests - #112

Merged
sudhirverma merged 2 commits into
mainfrom
test/keychain-secret-store-unit-tests
Aug 12, 2026
Merged

test(daemon): add KeychainSecretStore unit tests#112
sudhirverma merged 2 commits into
mainfrom
test/keychain-secret-store-unit-tests

Conversation

@sudhirverma

@sudhirverma sudhirverma commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add unit tests for KeychainSecretStore covering get/set/delete/has, keytar import failures, SEA dlopen loading, and node:sea detection branches.
  • Inline keytar in vitest config so import('keytar') is mockable in unit tests.
  • Stabilize test mock lifecycle: re-register default keytar mock after doUnmock/resetModules, and use vi.dynamicImportSettled() instead of zero-delay timers for constructor import settling.
  • Achieves 100% line and branch coverage on packages/daemon/src/daemon/secrets/keychain.ts with no production code changes.

Commits

  • test(daemon): add KeychainSecretStore unit tests — 26 tests; keychain coverage 28% → 100%
  • test(daemon): stabilize keychain test mock lifecycle — address CodeRabbit review: re-register keytar mock in beforeEach, use vi.dynamicImportSettled() for deterministic constructor loading

Test plan

  • npm run lint -w packages/daemon
  • npm run test:coverage -w packages/daemon -- src/daemon/secrets/keychain.test.ts
  • npm test -w packages/daemon (1108 tests)
  • CI build

Cover keytar CRUD, import failures, SEA dlopen path, and node:sea
branches at 100% line/branch coverage. Inline keytar in vitest so
dynamic import() is mockable in unit tests.
@github-actions github-actions Bot added the chore label Aug 12, 2026
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Tests
    • Added comprehensive coverage for secure keychain storage, including retrieval, saving, deletion, availability checks, caching, logging, and failure handling.
    • Added coverage for packaged-runtime secret loading and fallback behavior.
    • Improved test reliability by configuring the keychain dependency for in-test execution.

Walkthrough

The pull request adds comprehensive KeychainSecretStore tests for keytar operations, failures, caching, export shapes, and SEA loading. Vitest now inlines keytar during tests.

Changes

Keychain test coverage

Layer / File(s) Summary
Test harness and dependency setup
packages/daemon/src/daemon/secrets/keychain.test.ts, packages/daemon/vitest.config.ts
Adds keytar mocks, fresh-import helpers, cache reset helpers, SEA loading patches, and Vitest keytar inlining.
Keychain operation and failure tests
packages/daemon/src/daemon/secrets/keychain.test.ts
Tests read, write, delete, has, logging, unavailable keytar behavior, error wrapping, and cached loading.
Runtime-specific keytar loading
packages/daemon/src/daemon/secrets/keychain.test.ts
Tests named exports, SEA native loading through process.dlopen, node:sea detection, and normal-runtime fallback.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the addition of KeychainSecretStore unit tests.
Description check ✅ Passed The description accurately explains the tests, Vitest configuration change, coverage, and test status.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.55%. Comparing base (b13b7b9) to head (6ecf228).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #112      +/-   ##
==========================================
+ Coverage   67.78%   68.55%   +0.76%     
==========================================
  Files          37       37              
  Lines        4973     4973              
  Branches     1568     1568              
==========================================
+ Hits         3371     3409      +38     
+ Misses       1056     1023      -33     
+ Partials      546      541       -5     
Flag Coverage Δ
daemon 68.55% <ø> (+0.76%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/daemon/src/daemon/secrets/keychain.test.ts`:
- Line 83: Replace both zero-delay timer waits in
packages/daemon/src/daemon/secrets/keychain.test.ts at lines 83 and 97 with
await vi.dynamicImportSettled(), ensuring tests deterministically wait for
KeychainSecretStore’s loadKeytar dynamic import before resetting or injecting
state.
- Around line 63-65: Update the keychain test setup and both helpers: after
vi.doUnmock('keytar'), re-register the default keytar mock with vi.doMock before
importing ./keychain.js, and replace each zero-delay timer with await
vi.dynamicImportSettled() so constructor loading completes before modifying
store internals.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7a96f22a-8c21-43f8-85c2-afee00397091

📥 Commits

Reviewing files that changed from the base of the PR and between b13b7b9 and 21da28a.

📒 Files selected for processing (2)
  • packages/daemon/src/daemon/secrets/keychain.test.ts
  • packages/daemon/vitest.config.ts

Comment thread packages/daemon/src/daemon/secrets/keychain.test.ts
Comment thread packages/daemon/src/daemon/secrets/keychain.test.ts Outdated
Re-register default keytar mock after doUnmock/resetModules so constructor
loadKeytar() never hits the native module. Replace zero-delay timers with
vi.dynamicImportSettled() for deterministic constructor import settling.
@sonarqubecloud

Copy link
Copy Markdown

@sudhirverma
sudhirverma merged commit 6a3db8a into main Aug 12, 2026
14 checks passed
@sudhirverma
sudhirverma deleted the test/keychain-secret-store-unit-tests branch August 12, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants