Add re-auth y/N confirmation across auth provider commands

This commit is contained in:
William Valentin
2026-02-15 20:00:11 -08:00
parent 22930cbe2e
commit e0f2d27247
7 changed files with 354 additions and 8 deletions
+132
View File
@@ -0,0 +1,132 @@
import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const {
mockLoadStoredAnthropicAuth,
mockLoadStoredAnthropicAuthToken,
mockStoreAnthropicAuth,
mockStoreAnthropicAuthToken,
} = vi.hoisted(() => ({
mockLoadStoredAnthropicAuth: vi.fn(),
mockLoadStoredAnthropicAuthToken: vi.fn(),
mockStoreAnthropicAuth: vi.fn(),
mockStoreAnthropicAuthToken: vi.fn(),
}));
const { mockCreateInterface } = vi.hoisted(() => ({
mockCreateInterface: vi.fn(),
}));
vi.mock('../auth/index.js', () => ({
loadStoredAnthropicAuth: mockLoadStoredAnthropicAuth,
loadStoredAnthropicAuthToken: mockLoadStoredAnthropicAuthToken,
storeAnthropicAuth: mockStoreAnthropicAuth,
storeAnthropicAuthToken: mockStoreAnthropicAuthToken,
}));
vi.mock('readline', () => ({
default: {
createInterface: mockCreateInterface,
},
}));
function mockReadlineAnswers(answers: string[]): void {
const queue = [...answers];
mockCreateInterface.mockImplementation(() => ({
question: (_prompt: string, cb: (answer: string) => void) => cb(queue.shift() ?? ''),
close: () => undefined,
}));
}
describe('anthropic-auth command', () => {
beforeEach(() => {
vi.clearAllMocks();
mockLoadStoredAnthropicAuth.mockReset();
mockLoadStoredAnthropicAuthToken.mockReset();
mockStoreAnthropicAuth.mockReset();
mockStoreAnthropicAuthToken.mockReset();
mockCreateInterface.mockReset();
});
it('cancels key re-auth when key exists and user answers no', async () => {
mockLoadStoredAnthropicAuth.mockReturnValue({ api_key: 'sk-ant-existing', created_at: '2026-02-16T00:00:00.000Z' });
mockReadlineAnswers(['n']);
const program = new Command();
const { registerAnthropicAuthCommand } = await import('./anthropic-auth.js');
registerAnthropicAuthCommand(program);
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
throw new Error(`EXIT:${code ?? 0}`);
}) as never);
await expect(program.parseAsync(['node', 'test', 'anthropic-auth'])).rejects.toThrow('EXIT:0');
expect(mockStoreAnthropicAuth).not.toHaveBeenCalled();
expect(consoleLog).toHaveBeenCalledWith('Cancelled.');
exitSpy.mockRestore();
consoleLog.mockRestore();
});
it('stores a new key when key exists and user answers yes', async () => {
mockLoadStoredAnthropicAuth.mockReturnValue({ api_key: 'sk-ant-existing', created_at: '2026-02-16T00:00:00.000Z' });
mockReadlineAnswers(['y', 'sk-ant-new']);
const program = new Command();
const { registerAnthropicAuthCommand } = await import('./anthropic-auth.js');
registerAnthropicAuthCommand(program);
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
await program.parseAsync(['node', 'test', 'anthropic-auth']);
expect(mockStoreAnthropicAuth).toHaveBeenCalledWith('sk-ant-new');
expect(consoleError).not.toHaveBeenCalled();
consoleLog.mockRestore();
consoleError.mockRestore();
});
it('cancels token re-auth when token exists and user answers no', async () => {
mockLoadStoredAnthropicAuthToken.mockReturnValue('tok-existing');
mockReadlineAnswers(['n']);
const program = new Command();
const { registerAnthropicAuthCommand } = await import('./anthropic-auth.js');
registerAnthropicAuthCommand(program);
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
throw new Error(`EXIT:${code ?? 0}`);
}) as never);
await expect(program.parseAsync(['node', 'test', 'anthropic-auth', '--token'])).rejects.toThrow('EXIT:0');
expect(mockStoreAnthropicAuthToken).not.toHaveBeenCalled();
expect(consoleLog).toHaveBeenCalledWith('Cancelled.');
exitSpy.mockRestore();
consoleLog.mockRestore();
});
it('stores a new token when token exists and user answers yes', async () => {
mockLoadStoredAnthropicAuthToken.mockReturnValue('tok-existing');
mockReadlineAnswers(['y', 'tok-new']);
const program = new Command();
const { registerAnthropicAuthCommand } = await import('./anthropic-auth.js');
registerAnthropicAuthCommand(program);
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
await program.parseAsync(['node', 'test', 'anthropic-auth', '--token']);
expect(mockStoreAnthropicAuthToken).toHaveBeenCalledWith('tok-new');
expect(consoleError).not.toHaveBeenCalled();
consoleLog.mockRestore();
consoleError.mockRestore();
});
});