({
available = true,
initialEntries = {},
readReportsUnavailable = false, // simulate mid-execution unavailability (locked keyring)
writeFails = false, // simulate keychain write rejection
} = {})
| 34 | // Replace workspaceKeychain.js in the require cache with a mock that |
| 35 | // records all calls and lets each test pre-seed its own state. |
| 36 | function installKeychainMock({ |
| 37 | available = true, |
| 38 | initialEntries = {}, |
| 39 | readReportsUnavailable = false, // simulate mid-execution unavailability (locked keyring) |
| 40 | writeFails = false, // simulate keychain write rejection |
| 41 | } = {}) { |
| 42 | const store = new Map(Object.entries(initialEntries)); |
| 43 | const calls = { read: [], write: [], loadAddon: 0 }; |
| 44 | const mock = { |
| 45 | KEYCHAIN_SERVICE: 'evomap.evolver.workspace-id', |
| 46 | getMode() { |
| 47 | const raw = String(process.env.EVOLVER_WORKSPACE_KEYCHAIN || 'auto').toLowerCase().trim(); |
| 48 | if (raw === 'force' || raw === 'off' || raw === 'auto') return raw; |
| 49 | return 'auto'; |
| 50 | }, |
| 51 | loadAddon() { |
| 52 | calls.loadAddon++; |
| 53 | return available ? { Entry: function () {} } : null; |
| 54 | }, |
| 55 | readFromKeychain(account) { |
| 56 | calls.read.push(account); |
| 57 | if (!available) return { available: false, id: null }; |
| 58 | if (readReportsUnavailable) return { available: false, id: null }; |
| 59 | const id = store.get(account) || null; |
| 60 | return { available: true, id }; |
| 61 | }, |
| 62 | writeToKeychain(account, id) { |
| 63 | calls.write.push({ account, id }); |
| 64 | if (!available) return false; |
| 65 | if (writeFails) return false; |
| 66 | store.set(account, id); |
| 67 | return true; |
| 68 | }, |
| 69 | }; |
| 70 | require.cache[KEYCHAIN_PATH] = { |
| 71 | id: KEYCHAIN_PATH, |
| 72 | filename: KEYCHAIN_PATH, |
| 73 | loaded: true, |
| 74 | exports: mock, |
| 75 | children: [], |
| 76 | paths: [], |
| 77 | }; |
| 78 | return { mock, store, calls }; |
| 79 | } |
| 80 | |
| 81 | function clearKeychainMock() { |
| 82 | delete require.cache[KEYCHAIN_PATH]; |
no outgoing calls
no test coverage detected