diff --git a/src/cli.ts b/src/cli.ts index f488026..a35d4ee 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -791,7 +791,7 @@ export async function handleAuth(runtime: Awaited' }, +}; + +describe('handleAuth retry logic', () => { + it('retries once when the first attempt is unauthorized', async () => { + const { handleAuth } = await cliModulePromise; + const runtime = { + registerDefinition: vi.fn(), + getDefinition: vi.fn().mockReturnValue(baseDefinition), + listTools: vi + .fn() + .mockRejectedValueOnce(new Error('SSE error: Non-200 status code (401)')) + .mockResolvedValueOnce([{ name: 'ok' }]), + } as unknown as Awaited>; + + await expect(handleAuth(runtime, ['adhoc-server'])).resolves.toBeUndefined(); + expect(runtime.listTools).toHaveBeenCalledTimes(2); + }); + + it('throws after the second unauthorized attempt', async () => { + const { handleAuth } = await cliModulePromise; + const runtime = { + registerDefinition: vi.fn(), + getDefinition: vi.fn().mockReturnValue(baseDefinition), + listTools: vi.fn().mockRejectedValue(new Error('SSE error: Non-200 status code (401)')), + } as unknown as Awaited>; + + await expect(handleAuth(runtime, ['adhoc-server'])).rejects.toThrow(/Failed to authorize/); + expect(runtime.listTools).toHaveBeenCalledTimes(2); + }); +});