From f36be7254e37106b4933e75ca6faa8a67bf6bd54 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 7 Nov 2025 00:51:10 +0000 Subject: [PATCH] Add auth retry tests --- src/cli.ts | 10 ++++++++- tests/cli-auth-retry.test.ts | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/cli-auth-retry.test.ts 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); + }); +});