Add y/N re-auth confirmation to zai-auth command

This commit is contained in:
William Valentin
2026-02-15 19:57:52 -08:00
parent 8b1ed2f689
commit 22930cbe2e
3 changed files with 105 additions and 2 deletions
+80
View File
@@ -0,0 +1,80 @@
import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const { mockLoadStoredZaiAuth, mockStoreZaiAuth } = vi.hoisted(() => ({
mockLoadStoredZaiAuth: vi.fn(),
mockStoreZaiAuth: vi.fn(),
}));
const { mockCreateInterface } = vi.hoisted(() => ({
mockCreateInterface: vi.fn(),
}));
vi.mock('../auth/index.js', () => ({
loadStoredZaiAuth: mockLoadStoredZaiAuth,
storeZaiAuth: mockStoreZaiAuth,
}));
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('zai-auth command', () => {
beforeEach(() => {
vi.clearAllMocks();
mockLoadStoredZaiAuth.mockReset();
mockStoreZaiAuth.mockReset();
mockCreateInterface.mockReset();
});
it('cancels when credential exists and user answers no', async () => {
mockLoadStoredZaiAuth.mockReturnValue({ api_key: 'existing-key', created_at: '2026-02-16T00:00:00.000Z' });
mockReadlineAnswers(['n']);
const program = new Command();
const { registerZaiAuthCommand } = await import('./zai-auth.js');
registerZaiAuthCommand(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', 'zai-auth'])).rejects.toThrow('EXIT:0');
expect(mockStoreZaiAuth).not.toHaveBeenCalled();
expect(consoleLog).toHaveBeenCalledWith('Cancelled.');
exitSpy.mockRestore();
consoleLog.mockRestore();
});
it('re-prompts and stores new key when credential exists and user answers yes', async () => {
mockLoadStoredZaiAuth.mockReturnValue({ api_key: 'existing-key', created_at: '2026-02-16T00:00:00.000Z' });
mockReadlineAnswers(['y', 'new-zai-key']);
const program = new Command();
const { registerZaiAuthCommand } = await import('./zai-auth.js');
registerZaiAuthCommand(program);
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => undefined);
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
await program.parseAsync(['node', 'test', 'zai-auth']);
expect(mockStoreZaiAuth).toHaveBeenCalledWith('new-zai-key');
expect(consoleError).not.toHaveBeenCalled();
consoleLog.mockRestore();
consoleError.mockRestore();
});
});
+13 -2
View File
@@ -27,6 +27,14 @@ async function promptHidden(question: string): Promise<string> {
return answer.trim();
}
async function promptYesNo(question: string): Promise<boolean> {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
const answer = await new Promise<string>((resolve) => rl.question(question, resolve));
rl.close();
const normalized = answer.trim().toLowerCase();
return normalized === 'y' || normalized === 'yes';
}
export function registerZaiAuthCommand(program: Command): void {
program
.command('zai-auth')
@@ -35,8 +43,11 @@ export function registerZaiAuthCommand(program: Command): void {
const existing = loadStoredZaiAuth();
if (existing) {
console.log('Z.AI credential already exists.');
console.log('Delete ~/.config/flynn/auth.json zai/zhipuai entry if you want to re-authenticate.');
process.exit(0);
const confirmed = await promptYesNo('Re-authenticate and replace it? (y/N): ');
if (!confirmed) {
console.log('Cancelled.');
process.exit(0);
}
}
console.log('Z.AI uses API keys (HTTP Bearer), not an OAuth device flow.');