import { execFileSync } from 'child_process'; import { getKeytar } from './getKeytar'; // NON-MOCKED integration test — this is the regression guard the incident was // missing. Every prior keystore test mocked keytar, so the real native binding // was never loaded and CI shipped green while `require('keytar.node')` failed on // Node 24. Here we exercise the real @napi-rs/keyring binding. // // Two levels, per plan AD-3: // 1. Load assertion (runs everywhere): resolving getKeytar() imports // @napi-rs/keyring, which loads the native .node at require time. If the // binding can't load on this Node, getKeytar() rejects and this test fails // (AC-4.2) — exactly the failure that previously slipped through. // 2. Round-trip (gated on a secret service being available): a real // set → get → delete → get against the OS keychain (AC-4.1, AC-1.1). // macOS/Windows always have one; headless Linux needs gnome-keyring/D-Bus, // provisioned in CI (TASK-6). Where unavailable, only the load assertion runs. // A secret service backs the round-trip on macOS (darwin) and Windows (win32) // out of the box; on Linux it exists only when D-Bus + a keyring daemon are // present (CI sets DBUS_SESSION_BUS_ADDRESS, or force with KEYSTORE_ROUNDTRIP=1). const secretServiceAvailable = process.platform !== 'linux' || !!process.env.DBUS_SESSION_BUS_ADDRESS || process.env.KEYSTORE_ROUNDTRIP === '1'; const roundTripIt = secretServiceAvailable ? it : it.skip; describe('getKeytar integration (real @napi-rs/keyring binding)', () => { it('loads the native keychain binding on this Node version (AC-4.2)', async () => { // Rejects if the native binding cannot load — the regression this guards. const keytar = await getKeytar(); expect(typeof keytar.getPassword).toBe('function'); expect(typeof keytar.setPassword).toBe('function'); expect(typeof keytar.deletePassword).toBe('function'); }); roundTripIt( 'round-trips set → get → delete → get against the real OS keychain (AC-4.1, AC-1.1)', async () => { const keytar = await getKeytar(); // Disposable, unique key so the test never collides with real CLI tokens. const service = `alkscli-integration-${process.pid}-${Date.now()}`; const account = 'roundtrip'; try { await keytar.setPassword(service, account, 'secret-xyz'); expect(await keytar.getPassword(service, account)).toBe('secret-xyz'); await keytar.deletePassword(service, account); expect(await keytar.getPassword(service, account)).toBeUndefined(); } finally { // Best-effort cleanup in case an assertion above threw mid-way. await keytar.deletePassword(service, account).catch(() => undefined); } } ); // Size regression guard: a realistic ALKS refresh token is ~1700 characters. // Under keyring's setPassword() that was UTF-16-encoded to ~3400 bytes and // rejected by Windows Credential Manager's 2560-byte blob limit, so `alks // developer configure` failed on every Windows machine. The adapter now writes // raw UTF-8 bytes, which fits. This exercises the real binding rather than a // mock, so it catches a platform store rejecting the payload on whatever OS it // runs on — note CI is Linux-only, so on Windows this must be run locally. roundTripIt( 'round-trips a realistic ~1700-character refresh token through the real OS keychain', async () => { const keytar = await getKeytar(); const service = `alkscli-longtoken-${process.pid}-${Date.now()}`; const account = 'roundtrip'; const token = '1' + 'a1B2c3D4'.repeat(212); try { await keytar.setPassword(service, account, token); expect(await keytar.getPassword(service, account)).toBe(token); } finally { await keytar.deletePassword(service, account).catch(() => undefined); } } ); // Backward compatibility (AC-1.5 / NFR-2): a token written by the OLD keytar // build must remain readable after the swap. keytar stored a standard macOS // generic password keyed by (service, account) — exactly what // `security add-generic-password` creates — so writing one that way faithfully // simulates a pre-upgrade entry, then reading it back through the new adapter // proves interop under the same alkscli/alkstoken key scheme. // // OPT-IN (KEYSTORE_MIGRATION_CHECK=1) and macOS-only: reading a keychain item // created by another program (`security`, not node) triggers a macOS ACL // authorization prompt, which blocks in any non-interactive context (CI, jest // workers). Run it deliberately on a dev Mac — approve the prompt (Always // Allow) — to reproduce the manual TASK-8 migration check as a committed guard. const runMigrationCheck = process.platform === 'darwin' && process.env.KEYSTORE_MIGRATION_CHECK === '1'; const migrationIt = runMigrationCheck ? it : it.skip; migrationIt( 'reads a keytar-format keychain entry written before the swap (AC-1.5)', async () => { const keytar = await getKeytar(); const service = `alkscli-migcheck-${process.pid}-${Date.now()}`; const account = 'alkstoken'; // Simulate the legacy keytar-written entry via the OS-native tool. // -A widens the ACL; note macOS may still prompt on the cross-creator // read (that is why this test is opt-in and expects an interactive Allow). execFileSync('security', [ 'add-generic-password', '-s', service, '-a', account, '-w', 'legacy-keytar-token', '-A', '-U', ]); try { expect(await keytar.getPassword(service, account)).toBe( 'legacy-keytar-token' ); } finally { await keytar.deletePassword(service, account).catch(() => undefined); } }, 30000 ); });