Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/utils/__tests__/usage-token-buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('getUsageToken dump-keychain behavior', () => {
mockedExecFileSync.mockImplementation(realExecFileSync);
vi.spyOn(process, 'platform', 'get').mockReturnValue('darwin');
vi.spyOn(fs, 'statSync').mockImplementation(() => { throw new Error('cache missing'); });
// The write side of the keychain cache (usage-fetch.ts
// keychain-fallback-empty marker, usage.json cache, lock file) must
// never touch the real ~/.cache during tests.
vi.spyOn(fs, 'mkdirSync').mockReturnValue(undefined);
vi.spyOn(fs, 'writeFileSync').mockReturnValue(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -90,4 +95,21 @@ describe('getUsageToken dump-keychain behavior', () => {
expect(getUsageToken()).toBe('hashed-token');
expect(dumpMaxBuffer).toBe(8 * 1024 * 1024);
});

it('writes the keychain miss marker when the dump finds no credentials', () => {
const readSpy = vi.spyOn(fs, 'readFileSync').mockImplementation(() => { throw new Error('no files'); });
const writeSpy = vi.spyOn(fs, 'writeFileSync');

mockedExecFileSync.mockImplementation((command: string) => {
if (command === 'security') {
throw new Error('security: item not found');
}
throw new Error(`Unexpected command: ${command}`);
});

expect(getUsageToken()).toBeNull();
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('keychain-fallback-empty'), '');
expect(writeSpy).not.toHaveBeenCalledWith(expect.stringContaining('usage.lock'), expect.anything());
readSpy.mockRestore();
});
});
Loading