|
| 1 | +/** |
| 2 | + * Live integration smoke for the Codex account-auth LLM provider. |
| 3 | + * |
| 4 | + * This test is default-on for developer machines where `codex login` has |
| 5 | + * created a ChatGPT auth file. Set ATOMICMEMORY_SKIP_CODEX_TEST=1 to opt out |
| 6 | + * when avoiding local account usage. |
| 7 | + */ |
| 8 | + |
| 9 | +import { readFile } from 'node:fs/promises'; |
| 10 | +import { homedir } from 'node:os'; |
| 11 | +import { join } from 'node:path'; |
| 12 | +import { describe, expect, it } from 'vitest'; |
| 13 | +import { CodexLLM } from '../codex-llm.js'; |
| 14 | +import { DEFAULT_CODEX_LLM_MODEL } from '../llm-defaults.js'; |
| 15 | + |
| 16 | +const SKIP_ENV = 'ATOMICMEMORY_SKIP_CODEX_TEST'; |
| 17 | + |
| 18 | +interface CodexReadiness { |
| 19 | + runnable: boolean; |
| 20 | + authPath: string; |
| 21 | + reason: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface CodexAuthFile { |
| 25 | + auth_mode?: string; |
| 26 | + tokens?: { |
| 27 | + access_token?: string; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +const readiness = await resolveCodexReadiness(); |
| 32 | + |
| 33 | +describe.skipIf(!readiness.runnable)(`CodexLLM live integration (${readiness.reason})`, () => { |
| 34 | + it('runs through Codex local auth without OPENAI_API_KEY', async () => { |
| 35 | + const previousOpenAiApiKey = process.env.OPENAI_API_KEY; |
| 36 | + delete process.env.OPENAI_API_KEY; |
| 37 | + |
| 38 | + try { |
| 39 | + const provider = new CodexLLM({ |
| 40 | + llmProvider: 'codex', |
| 41 | + llmModel: DEFAULT_CODEX_LLM_MODEL, |
| 42 | + llmApiUrl: undefined, |
| 43 | + codexAuthPath: readiness.authPath, |
| 44 | + costLoggingEnabled: false, |
| 45 | + costRunId: 'codex-live-test', |
| 46 | + costLogDir: '/tmp/atomicmemory-codex-live-test', |
| 47 | + }); |
| 48 | + |
| 49 | + const output = await provider.chat([ |
| 50 | + { role: 'system', content: 'Return exactly: atomicmemory-codex-ok' }, |
| 51 | + { role: 'user', content: 'Run the AtomicMemory Codex live smoke test.' }, |
| 52 | + ]); |
| 53 | + |
| 54 | + expect(output).toContain('atomicmemory-codex-ok'); |
| 55 | + } finally { |
| 56 | + restoreEnv('OPENAI_API_KEY', previousOpenAiApiKey); |
| 57 | + } |
| 58 | + }, 120_000); |
| 59 | +}); |
| 60 | + |
| 61 | +async function resolveCodexReadiness(): Promise<CodexReadiness> { |
| 62 | + const authPath = process.env.CODEX_AUTH_PATH ?? join(process.env.CODEX_HOME ?? join(homedir(), '.codex'), 'auth.json'); |
| 63 | + if (process.env[SKIP_ENV] === '1') { |
| 64 | + return { runnable: false, authPath, reason: `${SKIP_ENV}=1` }; |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + const parsed = JSON.parse(await readFile(authPath, 'utf8')) as CodexAuthFile; |
| 69 | + if (parsed.auth_mode !== 'chatgpt') { |
| 70 | + return { runnable: false, authPath, reason: `codex auth at ${authPath} is not ChatGPT login auth` }; |
| 71 | + } |
| 72 | + if (!parsed.tokens?.access_token) { |
| 73 | + return { runnable: false, authPath, reason: `codex auth at ${authPath} has no access token` }; |
| 74 | + } |
| 75 | + return { runnable: true, authPath, reason: `codex auth file is present at ${authPath}` }; |
| 76 | + } catch (error) { |
| 77 | + return { runnable: false, authPath, reason: `codex auth unavailable: ${errorMessage(error)}` }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function restoreEnv(name: string, value: string | undefined): void { |
| 82 | + if (value === undefined) { |
| 83 | + delete process.env[name]; |
| 84 | + } else { |
| 85 | + process.env[name] = value; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function errorMessage(error: unknown): string { |
| 90 | + return error instanceof Error ? error.message : String(error); |
| 91 | +} |
0 commit comments