|
| 1 | +/** |
| 2 | + * Codex provider E2E tests — verifies real API communication. |
| 3 | + * |
| 4 | + * Requires ~/.codex/auth.json (run `codex login` first). |
| 5 | + * Skips gracefully if auth is not configured. |
| 6 | + * |
| 7 | + * Run: pnpm test:e2e |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, beforeAll, beforeEach } from 'vitest' |
| 11 | +import OpenAI from 'openai' |
| 12 | +import { readFile } from 'node:fs/promises' |
| 13 | +import { join } from 'node:path' |
| 14 | +import { homedir } from 'node:os' |
| 15 | + |
| 16 | +// ==================== Setup ==================== |
| 17 | + |
| 18 | +const OAUTH_BASE_URL = 'https://chatgpt.com/backend-api/codex' |
| 19 | +const MODEL = 'gpt-5.4-mini' // Use mini for faster/cheaper e2e |
| 20 | + |
| 21 | +let client: OpenAI | null = null |
| 22 | + |
| 23 | +async function tryLoadToken(): Promise<string | null> { |
| 24 | + const codexHome = process.env.CODEX_HOME ?? join(homedir(), '.codex') |
| 25 | + try { |
| 26 | + const raw = JSON.parse(await readFile(join(codexHome, 'auth.json'), 'utf-8')) |
| 27 | + return raw?.tokens?.access_token ?? null |
| 28 | + } catch { |
| 29 | + return null |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +beforeAll(async () => { |
| 34 | + const token = await tryLoadToken() |
| 35 | + if (!token) { |
| 36 | + console.warn('codex e2e: ~/.codex/auth.json not found, skipping tests') |
| 37 | + return |
| 38 | + } |
| 39 | + client = new OpenAI({ apiKey: token, baseURL: OAUTH_BASE_URL }) |
| 40 | + console.log('codex e2e: client initialized') |
| 41 | +}, 15_000) |
| 42 | + |
| 43 | +// ==================== Tests ==================== |
| 44 | + |
| 45 | +describe('Codex API — basic communication', () => { |
| 46 | + beforeEach(({ skip }) => { if (!client) skip('no codex auth') }) |
| 47 | + |
| 48 | + it('receives a text response for a simple prompt', async () => { |
| 49 | + const stream = client!.responses.stream({ |
| 50 | + model: MODEL, |
| 51 | + instructions: 'You are a helpful assistant. Be very brief.', |
| 52 | + input: [{ role: 'user', content: 'What is 2+2? Answer with just the number.' }], |
| 53 | + store: false, |
| 54 | + }) |
| 55 | + |
| 56 | + let text = '' |
| 57 | + for await (const event of stream) { |
| 58 | + if (event.type === 'response.output_text.delta') text += event.delta |
| 59 | + } |
| 60 | + |
| 61 | + expect(text).toBeTruthy() |
| 62 | + expect(text).toContain('4') |
| 63 | + }, 30_000) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('Codex API — tool call round-trip', () => { |
| 67 | + beforeEach(({ skip }) => { if (!client) skip('no codex auth') }) |
| 68 | + |
| 69 | + const tools: OpenAI.Responses.Tool[] = [{ |
| 70 | + type: 'function', |
| 71 | + name: 'get_price', |
| 72 | + description: 'Get the current price of a stock by symbol', |
| 73 | + parameters: { |
| 74 | + type: 'object', |
| 75 | + properties: { symbol: { type: 'string', description: 'Stock ticker symbol' } }, |
| 76 | + required: ['symbol'], |
| 77 | + }, |
| 78 | + strict: null, |
| 79 | + }] |
| 80 | + |
| 81 | + it('receives a function call with call_id, name, and arguments', async () => { |
| 82 | + const stream = client!.responses.stream({ |
| 83 | + model: MODEL, |
| 84 | + instructions: 'You are a stock assistant. Always use the get_price tool when asked about prices.', |
| 85 | + input: [{ role: 'user', content: 'What is the price of AAPL?' }], |
| 86 | + tools, |
| 87 | + store: false, |
| 88 | + }) |
| 89 | + |
| 90 | + let funcCall: { call_id: string; name: string; arguments: string } | null = null |
| 91 | + for await (const event of stream) { |
| 92 | + if (event.type === 'response.output_item.done') { |
| 93 | + const item = (event as any).item |
| 94 | + if (item?.type === 'function_call') { |
| 95 | + funcCall = { call_id: item.call_id, name: item.name, arguments: item.arguments } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + expect(funcCall).not.toBeNull() |
| 101 | + expect(funcCall!.call_id).toBeTruthy() |
| 102 | + expect(funcCall!.name).toBe('get_price') |
| 103 | + const args = JSON.parse(funcCall!.arguments) |
| 104 | + expect(args.symbol).toMatch(/AAPL/i) |
| 105 | + }, 30_000) |
| 106 | + |
| 107 | + it('completes a full tool call round-trip', async () => { |
| 108 | + // Round 1: get function call |
| 109 | + const stream1 = client!.responses.stream({ |
| 110 | + model: MODEL, |
| 111 | + instructions: 'You are a stock assistant. Always use the get_price tool.', |
| 112 | + input: [{ role: 'user', content: 'Price of MSFT?' }], |
| 113 | + tools, |
| 114 | + store: false, |
| 115 | + }) |
| 116 | + |
| 117 | + let funcCall: { call_id: string; name: string; arguments: string } | null = null |
| 118 | + for await (const event of stream1) { |
| 119 | + if (event.type === 'response.output_item.done') { |
| 120 | + const item = (event as any).item |
| 121 | + if (item?.type === 'function_call') { |
| 122 | + funcCall = { call_id: item.call_id, name: item.name, arguments: item.arguments } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + expect(funcCall).not.toBeNull() |
| 128 | + |
| 129 | + // Round 2: send tool result back, get final text |
| 130 | + const stream2 = client!.responses.stream({ |
| 131 | + model: MODEL, |
| 132 | + instructions: 'You are a stock assistant.', |
| 133 | + input: [ |
| 134 | + { role: 'user', content: 'Price of MSFT?' }, |
| 135 | + { type: 'function_call', call_id: funcCall!.call_id, name: funcCall!.name, arguments: funcCall!.arguments } as any, |
| 136 | + { type: 'function_call_output', call_id: funcCall!.call_id, output: '{"price": 420.50, "currency": "USD"}' } as any, |
| 137 | + ], |
| 138 | + tools, |
| 139 | + store: false, |
| 140 | + }) |
| 141 | + |
| 142 | + let responseText = '' |
| 143 | + for await (const event of stream2) { |
| 144 | + if (event.type === 'response.output_text.delta') responseText += event.delta |
| 145 | + } |
| 146 | + |
| 147 | + expect(responseText).toBeTruthy() |
| 148 | + expect(responseText).toMatch(/420/i) |
| 149 | + }, 30_000) |
| 150 | +}) |
| 151 | + |
| 152 | +describe('Codex API — structured multi-turn input', () => { |
| 153 | + beforeEach(({ skip }) => { if (!client) skip('no codex auth') }) |
| 154 | + |
| 155 | + it('references earlier conversation context', async () => { |
| 156 | + const stream = client!.responses.stream({ |
| 157 | + model: MODEL, |
| 158 | + instructions: 'You are a helpful assistant. Be very brief.', |
| 159 | + input: [ |
| 160 | + { role: 'user', content: 'My name is Alice.' }, |
| 161 | + { role: 'assistant', content: 'Nice to meet you, Alice!' }, |
| 162 | + { role: 'user', content: 'What is my name?' }, |
| 163 | + ], |
| 164 | + store: false, |
| 165 | + }) |
| 166 | + |
| 167 | + let text = '' |
| 168 | + for await (const event of stream) { |
| 169 | + if (event.type === 'response.output_text.delta') text += event.delta |
| 170 | + } |
| 171 | + |
| 172 | + expect(text).toBeTruthy() |
| 173 | + expect(text.toLowerCase()).toContain('alice') |
| 174 | + }, 30_000) |
| 175 | +}) |
0 commit comments