import { promptForToken } from './promptForToken'; import { getAlks } from './getAlks'; import { getPasswordFromPrompt } from './getPasswordFromPrompt'; jest.mock('./getAlks'); jest.mock('./getPasswordFromPrompt'); jest.mock('./log'); jest.mock('./state/server'); jest.mock('open'); // promptForToken is the shared token-entry path for both `alks developer // login2fa` and `alks developer configure` (refresh-token choice). getAlks is // token-only: it validates the refresh token by exchanging it for an access // token internally. These tests guard against the regression where // promptForToken called getAlks({}) with no token — exchanging `undefined` and // failing every login2fa before the entered token was ever validated. describe('promptForToken', () => { const ENTERED_TOKEN = 'refresh-token-abc'; beforeEach(() => { // Non-TTY: skip the browser-open branch and go straight to the prompt. (process.stdin as unknown as { isTTY?: boolean }).isTTY = undefined; (getPasswordFromPrompt as jest.Mock).mockResolvedValue(ENTERED_TOKEN); }); afterEach(() => { jest.resetAllMocks(); }); it('validates the entered token by passing it to getAlks and returns it', async () => { (getAlks as jest.Mock).mockResolvedValue({}); const result = await promptForToken(); expect(getAlks).toHaveBeenCalledWith({ token: ENTERED_TOKEN }); expect(result).toBe(ENTERED_TOKEN); }); it('prefixes and rethrows when getAlks rejects an invalid token', async () => { (getAlks as jest.Mock).mockRejectedValue(new Error('invalid token')); await expect(promptForToken()).rejects.toThrow( /Error validating refresh token\. invalid token/ ); }); });