|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + ensureAuthProfileStore: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("openclaw/plugin-sdk/provider-auth", () => ({ |
| 11 | + ensureAuthProfileStore: mocks.ensureAuthProfileStore, |
| 12 | +})); |
| 13 | + |
| 14 | +let bridgeCodexAppServerStartOptions: typeof import("./auth-bridge.js").bridgeCodexAppServerStartOptions; |
| 15 | + |
| 16 | +describe("bridgeCodexAppServerStartOptions", () => { |
| 17 | + const tempDirs: string[] = []; |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + ({ bridgeCodexAppServerStartOptions } = await import("./auth-bridge.js")); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => { |
| 24 | + mocks.ensureAuthProfileStore.mockReset(); |
| 25 | + await Promise.all( |
| 26 | + tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })), |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("bridges canonical OpenClaw oauth into an isolated CODEX_HOME", async () => { |
| 31 | + const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-")); |
| 32 | + tempDirs.push(agentDir); |
| 33 | + mocks.ensureAuthProfileStore.mockReturnValue({ |
| 34 | + version: 1, |
| 35 | + profiles: { |
| 36 | + "openai-codex:default": { |
| 37 | + type: "oauth", |
| 38 | + provider: "openai-codex", |
| 39 | + access: "access-token", |
| 40 | + refresh: "refresh-token", |
| 41 | + expires: Date.now() + 60_000, |
| 42 | + accountId: "acct-123", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const result = await bridgeCodexAppServerStartOptions({ |
| 48 | + startOptions: { |
| 49 | + command: "codex", |
| 50 | + args: ["app-server"], |
| 51 | + headers: { authorization: "Bearer dev-token" }, |
| 52 | + env: { EXISTING: "1" }, |
| 53 | + clearEnv: ["FOO"], |
| 54 | + }, |
| 55 | + agentDir, |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result).toMatchObject({ |
| 59 | + env: { |
| 60 | + EXISTING: "1", |
| 61 | + CODEX_HOME: expect.stringContaining(path.join(agentDir, "harness-auth", "codex")), |
| 62 | + }, |
| 63 | + clearEnv: expect.arrayContaining(["FOO", "OPENAI_API_KEY"]), |
| 64 | + }); |
| 65 | + |
| 66 | + const authFile = JSON.parse( |
| 67 | + await fs.readFile(path.join(result.env?.CODEX_HOME ?? "", "auth.json"), "utf8"), |
| 68 | + ); |
| 69 | + expect(authFile).toEqual({ |
| 70 | + auth_mode: "chatgpt", |
| 71 | + tokens: { |
| 72 | + access_token: "access-token", |
| 73 | + refresh_token: "refresh-token", |
| 74 | + account_id: "acct-123", |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("leaves start options unchanged when canonical oauth is unavailable", async () => { |
| 80 | + const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-")); |
| 81 | + tempDirs.push(agentDir); |
| 82 | + const startOptions = { |
| 83 | + command: "codex", |
| 84 | + args: ["app-server"], |
| 85 | + headers: { authorization: "Bearer dev-token" }, |
| 86 | + }; |
| 87 | + mocks.ensureAuthProfileStore.mockReturnValue({ |
| 88 | + version: 1, |
| 89 | + profiles: {}, |
| 90 | + }); |
| 91 | + |
| 92 | + await expect( |
| 93 | + bridgeCodexAppServerStartOptions({ |
| 94 | + startOptions, |
| 95 | + agentDir, |
| 96 | + authProfileId: "openai-codex:missing", |
| 97 | + }), |
| 98 | + ).resolves.toEqual(startOptions); |
| 99 | + }); |
| 100 | +}); |
0 commit comments