|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import type { CodexProfile } from "../auth/codex/store.js"; |
3 | 3 | import type { XaiProfile } from "../auth/xai/store.js"; |
4 | | -import { mergeOAuthCatalog } from "./index.js"; |
| 4 | +import { |
| 5 | + codexProfilesToCatalogEntries, |
| 6 | + codexProvidersAsSettings, |
| 7 | +} from "./codex-providers.js"; |
| 8 | +import { |
| 9 | + dropOrphanedOAuthEntries, |
| 10 | + mergeOAuthCatalog, |
| 11 | + overlayOAuthProjections, |
| 12 | + providerCatalogToSettings, |
| 13 | + runtimeSettingsWithCatalog, |
| 14 | +} from "./index.js"; |
5 | 15 | import type { |
6 | 16 | ProviderSettings, |
7 | 17 | ResolvedProvider, |
@@ -89,3 +99,103 @@ describe("mergeOAuthCatalog legacy bare-row dedupe (CL-5606)", () => { |
89 | 99 | expect(merged.map((p) => p.name)).toEqual(["openai", "codex/default"]); |
90 | 100 | }); |
91 | 101 | }); |
| 102 | + |
| 103 | +describe("CL-6728: OAuth projections do not overwrite hand-named provider entries", () => { |
| 104 | + const handNamed = (): ProviderSettings => ({ |
| 105 | + baseURL: "https://hand-named.example.com/v1", |
| 106 | + apiKey: "hand-named-key", |
| 107 | + models: ["hand-model"], |
| 108 | + }); |
| 109 | + const liveMine: CodexProfile = { |
| 110 | + name: "mine", |
| 111 | + tokens: { access: "live-token", refresh: "r", expiresAt: 1 }, |
| 112 | + createdAt: 0, |
| 113 | + }; |
| 114 | + const liveProjected = () => codexProvidersAsSettings([liveMine]); |
| 115 | + const liveCatalog = () => codexProfilesToCatalogEntries([liveMine]); |
| 116 | + |
| 117 | + test("overlayOAuthProjections keeps a hand-named codex/<slug> API-key entry", () => { |
| 118 | + const overlaid = overlayOAuthProjections( |
| 119 | + settingsWith({ "codex/mine": handNamed() }), |
| 120 | + liveProjected(), |
| 121 | + ); |
| 122 | + expect(overlaid?.providers["codex/mine"]?.apiKey).toBe("hand-named-key"); |
| 123 | + }); |
| 124 | + |
| 125 | + test("overlayOAuthProjections still applies the live token over a credential-less OAuth placeholder", () => { |
| 126 | + const overlaid = overlayOAuthProjections( |
| 127 | + settingsWith({ "codex/mine": entry() }), |
| 128 | + liveProjected(), |
| 129 | + ); |
| 130 | + expect(overlaid?.providers["codex/mine"]?.apiKey).toBe("live-token"); |
| 131 | + }); |
| 132 | + |
| 133 | + test("dropOrphanedOAuthEntries never drops a hand-named API-key entry", () => { |
| 134 | + const kept = dropOrphanedOAuthEntries( |
| 135 | + settingsWith({ "codex/mine": handNamed() }), |
| 136 | + {}, |
| 137 | + ); |
| 138 | + expect(kept?.providers["codex/mine"]?.apiKey).toBe("hand-named-key"); |
| 139 | + }); |
| 140 | + |
| 141 | + test("dropOrphanedOAuthEntries still drops a credential-less orphan", () => { |
| 142 | + const dropped = dropOrphanedOAuthEntries( |
| 143 | + settingsWith({ "codex/mine": entry() }), |
| 144 | + {}, |
| 145 | + ); |
| 146 | + expect(dropped?.providers["codex/mine"]).toBeUndefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + test("runtimeSettingsWithCatalog keeps a hand-named codex/<slug> API-key entry", () => { |
| 150 | + const runtime = runtimeSettingsWithCatalog( |
| 151 | + settingsWith({ "codex/mine": handNamed() }), |
| 152 | + liveCatalog(), |
| 153 | + ); |
| 154 | + expect(runtime.providers["codex/mine"]?.apiKey).toBe("hand-named-key"); |
| 155 | + }); |
| 156 | + |
| 157 | + test("runtimeSettingsWithCatalog still overlays the live token over a placeholder", () => { |
| 158 | + const runtime = runtimeSettingsWithCatalog( |
| 159 | + settingsWith({ "codex/mine": entry() }), |
| 160 | + liveCatalog(), |
| 161 | + ); |
| 162 | + expect(runtime.providers["codex/mine"]?.apiKey).toBe("live-token"); |
| 163 | + }); |
| 164 | + |
| 165 | + test("mergeOAuthCatalog keeps a hand-named codex/<slug> entry when its profile is live", () => { |
| 166 | + const merged = mergeOAuthCatalog( |
| 167 | + settingsWith({ "codex/mine": handNamed() }), |
| 168 | + resolved, |
| 169 | + [liveMine], |
| 170 | + [], |
| 171 | + ); |
| 172 | + const rows = merged.filter((p) => p.name === "codex/mine"); |
| 173 | + expect(rows).toHaveLength(1); |
| 174 | + expect(rows[0]?.apiKey).toBe("hand-named-key"); |
| 175 | + expect(rows[0]?.codexProfile).toBeUndefined(); |
| 176 | + }); |
| 177 | + |
| 178 | + test("mergeOAuthCatalog keeps a hand-named codex/<slug> entry with no live profile", () => { |
| 179 | + const merged = mergeOAuthCatalog( |
| 180 | + settingsWith({ "codex/mine": handNamed() }), |
| 181 | + resolved, |
| 182 | + [], |
| 183 | + [], |
| 184 | + ); |
| 185 | + expect(merged.find((p) => p.name === "codex/mine")?.apiKey).toBe( |
| 186 | + "hand-named-key", |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + test("persist round-trip keeps the hand-named key and no login token", () => { |
| 191 | + const merged = mergeOAuthCatalog( |
| 192 | + settingsWith({ "codex/mine": handNamed() }), |
| 193 | + resolved, |
| 194 | + [liveMine], |
| 195 | + [], |
| 196 | + ); |
| 197 | + const persisted = providerCatalogToSettings(merged, undefined); |
| 198 | + expect(persisted.providers["codex/mine"]?.apiKey).toBe("hand-named-key"); |
| 199 | + expect(JSON.stringify(persisted)).not.toContain("live-token"); |
| 200 | + }); |
| 201 | +}); |
0 commit comments