|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import type { CodexProfile } from "../auth/codex/store.js"; |
| 3 | +import type { XaiProfile } from "../auth/xai/store.js"; |
| 4 | +import { mergeOAuthCatalog } from "./index.js"; |
| 5 | +import type { |
| 6 | + ProviderSettings, |
| 7 | + ResolvedProvider, |
| 8 | + Settings, |
| 9 | +} from "./settings.js"; |
| 10 | + |
| 11 | +// Regression tests for CL-5606: after a successful ChatGPT browser login the |
| 12 | +// merged catalog must not list a separately-added legacy bare `codex` row |
| 13 | +// alongside the credential-backed `codex/<profile>` entry. |
| 14 | + |
| 15 | +const resolved: ResolvedProvider = { |
| 16 | + providerName: "openai", |
| 17 | + baseURL: "https://api.openai.com/v1", |
| 18 | + apiKey: "sk-test", |
| 19 | + model: "gpt-5", |
| 20 | +}; |
| 21 | + |
| 22 | +function settingsWith(providers: Record<string, ProviderSettings>): Settings { |
| 23 | + return { providers } as Settings; |
| 24 | +} |
| 25 | + |
| 26 | +const entry = (): ProviderSettings => ({ |
| 27 | + baseURL: "https://chatgpt.com/backend-api/codex/responses", |
| 28 | + models: ["gpt-5.1-codex-max"], |
| 29 | +}); |
| 30 | + |
| 31 | +const codexDefault: CodexProfile = { |
| 32 | + name: "default", |
| 33 | + tokens: { access: "codex-access", refresh: "r", expiresAt: 1 }, |
| 34 | + createdAt: 0, |
| 35 | +}; |
| 36 | +const xaiWork: XaiProfile = { |
| 37 | + name: "work", |
| 38 | + tokens: { access: "xai-access", refresh: "r", expiresAt: 1 }, |
| 39 | + createdAt: 0, |
| 40 | +}; |
| 41 | + |
| 42 | +describe("mergeOAuthCatalog legacy bare-row dedupe (CL-5606)", () => { |
| 43 | + test("a legacy bare codex row is dropped once codex/default is connected", () => { |
| 44 | + const merged = mergeOAuthCatalog( |
| 45 | + settingsWith({ codex: entry(), "codex/default": entry() }), |
| 46 | + resolved, |
| 47 | + [codexDefault], |
| 48 | + [], |
| 49 | + ); |
| 50 | + expect(merged.map((p) => p.name)).toEqual(["codex/default"]); |
| 51 | + }); |
| 52 | + |
| 53 | + test("a legacy bare xai row is dropped once xai/work is connected", () => { |
| 54 | + const merged = mergeOAuthCatalog( |
| 55 | + settingsWith({ xai: entry() }), |
| 56 | + resolved, |
| 57 | + [], |
| 58 | + [xaiWork], |
| 59 | + ); |
| 60 | + expect(merged.map((p) => p.name)).toEqual(["xai/work"]); |
| 61 | + }); |
| 62 | + |
| 63 | + test("a bare codex row survives when nothing credential-backed exists", () => { |
| 64 | + // Not connected: no auth-store profile and no qualified entry. The legacy |
| 65 | + // single-instance row is the only ChatGPT access — keep it. |
| 66 | + const merged = mergeOAuthCatalog( |
| 67 | + settingsWith({ codex: entry() }), |
| 68 | + resolved, |
| 69 | + [], |
| 70 | + [], |
| 71 | + ); |
| 72 | + expect(merged.map((p) => p.name)).toEqual(["codex"]); |
| 73 | + }); |
| 74 | + |
| 75 | + test("unrelated and API-key rows are untouched by the dedupe", () => { |
| 76 | + const merged = mergeOAuthCatalog( |
| 77 | + settingsWith({ |
| 78 | + openai: { |
| 79 | + baseURL: "https://api.openai.com/v1", |
| 80 | + apiKey: "sk-test", |
| 81 | + models: ["gpt-5"], |
| 82 | + }, |
| 83 | + codex: entry(), |
| 84 | + }), |
| 85 | + resolved, |
| 86 | + [codexDefault], |
| 87 | + [], |
| 88 | + ); |
| 89 | + expect(merged.map((p) => p.name)).toEqual(["openai", "codex/default"]); |
| 90 | + }); |
| 91 | +}); |
0 commit comments