|
| 1 | +/** |
| 2 | + * Unit tests for the Codex OAuth-backed LLM provider. |
| 3 | + */ |
| 4 | + |
| 5 | +import { readFile } from 'node:fs/promises'; |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 7 | + |
| 8 | +vi.mock('node:fs/promises', () => ({ |
| 9 | + readFile: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const { CodexLLM } = await import('../codex-llm.js'); |
| 13 | +const readFileMock = vi.mocked(readFile); |
| 14 | +const fetchMock = vi.fn<typeof fetch>(); |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + vi.unstubAllGlobals(); |
| 19 | +}); |
| 20 | + |
| 21 | +function provider(): InstanceType<typeof CodexLLM> { |
| 22 | + return new CodexLLM({ |
| 23 | + llmProvider: 'codex', |
| 24 | + llmModel: 'gpt-5.4-mini', |
| 25 | + llmApiUrl: undefined, |
| 26 | + codexAuthPath: '/tmp/codex-auth.json', |
| 27 | + costLoggingEnabled: false, |
| 28 | + costRunId: 'test', |
| 29 | + costLogDir: '/tmp/test-cost', |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function mockAuth(): void { |
| 34 | + readFileMock.mockResolvedValue(JSON.stringify({ |
| 35 | + auth_mode: 'chatgpt', |
| 36 | + tokens: { |
| 37 | + access_token: 'codex-token', |
| 38 | + account_id: 'acct-123', |
| 39 | + }, |
| 40 | + })); |
| 41 | +} |
| 42 | + |
| 43 | +function mockSseResponse(body: string): void { |
| 44 | + vi.stubGlobal('fetch', fetchMock); |
| 45 | + fetchMock.mockResolvedValue({ |
| 46 | + ok: true, |
| 47 | + status: 200, |
| 48 | + text: async () => body, |
| 49 | + } as Response); |
| 50 | +} |
| 51 | + |
| 52 | +function sse(...parts: string[]): string { |
| 53 | + return parts.map((part) => `event: response.text.delta\ndata: ${JSON.stringify({ delta: part })}\n`).join(''); |
| 54 | +} |
| 55 | + |
| 56 | +describe('CodexLLM', () => { |
| 57 | + it('calls the Codex backend directly with OAuth credentials from the Codex auth file', async () => { |
| 58 | + mockAuth(); |
| 59 | + mockSseResponse(sse('{', '"memories": []', '}')); |
| 60 | + |
| 61 | + const text = await provider().chat([ |
| 62 | + { role: 'system', content: 'Extract memory JSON.' }, |
| 63 | + { role: 'user', content: 'User prefers concise answers.' }, |
| 64 | + ], { jsonMode: true, maxTokens: 256 }); |
| 65 | + |
| 66 | + const fetchCall = fetchMock.mock.calls[0]; |
| 67 | + const requestBody = JSON.parse(String(fetchCall?.[1]?.body)) as Record<string, unknown>; |
| 68 | + expect(text).toBe('{"memories": []}'); |
| 69 | + expect(fetchCall?.[0]).toBe('https://chatgpt.com/backend-api/codex/responses'); |
| 70 | + expect(fetchCall?.[1]?.headers).toMatchObject({ |
| 71 | + Authorization: 'Bearer codex-token', |
| 72 | + 'OpenAI-Account-ID': 'acct-123', |
| 73 | + Origin: 'https://chatgpt.com', |
| 74 | + }); |
| 75 | + expect(requestBody.model).toBe('gpt-5.4-mini'); |
| 76 | + expect(requestBody.instructions).toContain('Return only valid JSON'); |
| 77 | + expect(requestBody.input).toEqual([ |
| 78 | + { type: 'message', role: 'user', content: 'User prefers concise answers.' }, |
| 79 | + ]); |
| 80 | + expect(requestBody.store).toBe(false); |
| 81 | + expect(requestBody.stream).toBe(true); |
| 82 | + expect(requestBody.max_output_tokens).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('rejects missing Codex auth with setup guidance', async () => { |
| 86 | + readFileMock.mockRejectedValue(new Error('ENOENT')); |
| 87 | + |
| 88 | + await expect(provider().chat([{ role: 'user', content: 'hello' }])) |
| 89 | + .rejects.toThrow('Run `codex login`'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('rejects non-ChatGPT Codex auth files', async () => { |
| 93 | + readFileMock.mockResolvedValue(JSON.stringify({ auth_mode: 'apikey' })); |
| 94 | + |
| 95 | + await expect(provider().chat([{ role: 'user', content: 'hello' }])) |
| 96 | + .rejects.toThrow('not a ChatGPT login'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('surfaces Codex HTTP auth failures with re-login guidance', async () => { |
| 100 | + mockAuth(); |
| 101 | + vi.stubGlobal('fetch', fetchMock); |
| 102 | + fetchMock.mockResolvedValue({ |
| 103 | + ok: false, |
| 104 | + status: 401, |
| 105 | + text: async () => 'unauthorized', |
| 106 | + } as Response); |
| 107 | + |
| 108 | + await expect(provider().chat([{ role: 'user', content: 'hello' }])) |
| 109 | + .rejects.toThrow('Run `codex login` again'); |
| 110 | + }); |
| 111 | +}); |
0 commit comments