MCPcopy Create free account
hub / github.com/EvoMap/evolver / installKeychainMock

Function installKeychainMock

test/workspaceKeychain.test.js:36–79  ·  view source on GitHub ↗
({
  available = true,
  initialEntries = {},
  readReportsUnavailable = false, // simulate mid-execution unavailability (locked keyring)
  writeFails = false,             // simulate keychain write rejection
} = {})

Source from the content-addressed store, hash-verified

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.
36function 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
81function clearKeychainMock() {
82 delete require.cache[KEYCHAIN_PATH];

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected