import { storeToken } from './storeToken'; import { getKeytar } from './getKeytar'; import { confirm } from './confirm'; import { getCredentials, setCredentials } from './state/credentials'; jest.mock('./getKeytar'); jest.mock('./confirm'); jest.mock('./state/credentials'); jest.mock('./log'); // Strip color so assertions match on the raw message text. jest.mock('cli-color', () => ({ red: (s: string) => s })); // storeToken must report the ACTUAL reason a keystore write failed rather than // the old blanket "No keychain could be found for storing the token" — that // message was a lie during the Node 24 incident (the real cause was a native // module that never loaded). The plaintext fallback stays, but it must be // reached through an honest error. describe('storeToken', () => { const TOKEN = 'refresh-token-abc'; let errSpy: jest.SpyInstance; beforeEach(() => { jest.resetAllMocks(); errSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); }); afterEach(() => { errSpy.mockRestore(); }); it('prints the caught error, not "No keychain could be found", on keystore failure (AC-2.2)', async () => { (getKeytar as jest.Mock).mockResolvedValue({ setPassword: jest .fn() .mockRejectedValue(new Error('libsecret is not available')), getPassword: jest.fn(), deletePassword: jest.fn(), }); (confirm as jest.Mock).mockResolvedValue(false); await expect(storeToken(TOKEN)).rejects.toThrow('Failed to save token'); const printed = errSpy.mock.calls.flat().join('\n'); expect(printed).toContain('libsecret is not available'); expect(printed).not.toContain( 'No keychain could be found for storing the token' ); }); it('prints the stringified reason (not "undefined") when the keystore rejects with a non-Error (AC-2.2)', async () => { // Some native bindings reject with a plain string/object; `.message` would // be undefined. Guards against reintroducing an uninformative error. (getKeytar as jest.Mock).mockResolvedValue({ setPassword: jest.fn().mockRejectedValue('libsecret missing'), getPassword: jest.fn(), deletePassword: jest.fn(), }); (confirm as jest.Mock).mockResolvedValue(false); await expect(storeToken(TOKEN)).rejects.toThrow('Failed to save token'); const printed = errSpy.mock.calls.flat().join('\n'); expect(printed).toContain('libsecret missing'); expect(printed).not.toContain('undefined'); }); it('falls back to the plaintext credentials file when the user confirms (AC-2.3)', async () => { (getKeytar as jest.Mock).mockResolvedValue({ setPassword: jest .fn() .mockRejectedValue(new Error('keychain unavailable')), getPassword: jest.fn(), deletePassword: jest.fn(), }); (confirm as jest.Mock).mockResolvedValue(true); (getCredentials as jest.Mock).mockResolvedValue({}); await storeToken(TOKEN); expect(setCredentials).toHaveBeenCalledWith( expect.objectContaining({ refresh_token: TOKEN }) ); }); it('stores through the keychain adapter when it works (no fallback) (AC-2.2)', async () => { const setPassword = jest.fn().mockResolvedValue(undefined); (getKeytar as jest.Mock).mockResolvedValue({ setPassword, getPassword: jest.fn(), deletePassword: jest.fn(), }); await storeToken(TOKEN); expect(setPassword).toHaveBeenCalledWith('alkscli', 'alkstoken', TOKEN); expect(confirm).not.toHaveBeenCalled(); expect(setCredentials).not.toHaveBeenCalled(); }); });