|
| 1 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 5 | + |
| 6 | +import { |
| 7 | + listCodexProfiles, |
| 8 | + loadCodexProfile, |
| 9 | + saveCodexProfile, |
| 10 | +} from "../config/oauth-stores.js"; |
| 11 | +import { |
| 12 | + listXaiProfiles, |
| 13 | + loadXaiProfile, |
| 14 | + saveXaiProfile, |
| 15 | +} from "../config/oauth-stores.js"; |
| 16 | +import { |
| 17 | + clearSourceCredentials, |
| 18 | + registerSourceCredentialRecord, |
| 19 | +} from "../config/source-credentials.js"; |
| 20 | +import { oauthStoreForProvider } from "./remove-provider.js"; |
| 21 | + |
| 22 | +function registerOAuthProvider( |
| 23 | + providerName: string, |
| 24 | + provider: "codex" | "xai", |
| 25 | + profile: string, |
| 26 | +): void { |
| 27 | + registerSourceCredentialRecord(providerName, { |
| 28 | + provenance: { kind: "oauth", provider, profile }, |
| 29 | + material: { secret: "token" }, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +async function withHome<T>(fn: (home: string) => Promise<T>): Promise<T> { |
| 34 | + const home = await mkdtemp(join(tmpdir(), "remove-provider-")); |
| 35 | + try { |
| 36 | + return await fn(home); |
| 37 | + } finally { |
| 38 | + await rm(home, { recursive: true, force: true }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +describe("oauthStoreForProvider", () => { |
| 43 | + afterEach(() => { |
| 44 | + clearSourceCredentials(); |
| 45 | + }); |
| 46 | + |
| 47 | + test("maps xai/ and codex/ provider names to their store and profile", () => { |
| 48 | + clearSourceCredentials(); |
| 49 | + registerOAuthProvider("xai/work", "xai", "work"); |
| 50 | + registerOAuthProvider("codex/personal", "codex", "personal"); |
| 51 | + expect(oauthStoreForProvider("xai/work")?.profile).toBe("work"); |
| 52 | + expect(oauthStoreForProvider("codex/personal")?.profile).toBe("personal"); |
| 53 | + }); |
| 54 | + |
| 55 | + test("rejects namespaced custom providers without OAuth ownership", () => { |
| 56 | + clearSourceCredentials(); |
| 57 | + registerSourceCredentialRecord("codex/manual", { |
| 58 | + provenance: { kind: "api-key" }, |
| 59 | + material: { secret: "manual-key" }, |
| 60 | + }); |
| 61 | + registerSourceCredentialRecord("xai/manual", { |
| 62 | + provenance: { kind: "api-key" }, |
| 63 | + material: { secret: "manual-key" }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(oauthStoreForProvider("codex/manual")).toBeNull(); |
| 67 | + expect(oauthStoreForProvider("xai/manual")).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + test("returns null for API-key, keyless, and degenerate names", () => { |
| 71 | + expect(oauthStoreForProvider("openai")).toBeNull(); |
| 72 | + expect(oauthStoreForProvider("ollama")).toBeNull(); |
| 73 | + expect(oauthStoreForProvider("my-custom")).toBeNull(); |
| 74 | + expect(oauthStoreForProvider("xai/")).toBeNull(); |
| 75 | + expect(oauthStoreForProvider("codex/")).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + test("removing an xai profile leaves sibling profiles untouched", async () => { |
| 79 | + await withHome(async (home) => { |
| 80 | + clearSourceCredentials(); |
| 81 | + registerOAuthProvider("xai/work", "xai", "work"); |
| 82 | + const tokens = { access: "a", refresh: "r", expiresAt: 10_000_000 }; |
| 83 | + await saveXaiProfile({ name: "work", createdAt: 0, tokens }, home); |
| 84 | + await saveXaiProfile({ name: "personal", createdAt: 0, tokens }, home); |
| 85 | + |
| 86 | + const target = oauthStoreForProvider("xai/work"); |
| 87 | + expect(target?.profile).toBe("work"); |
| 88 | + expect(await target?.removeProfile(target.profile, home)).toEqual([ |
| 89 | + "work", |
| 90 | + ]); |
| 91 | + expect(await loadXaiProfile("work", home)).toBeUndefined(); |
| 92 | + expect( |
| 93 | + (await listXaiProfiles(home)).map((profile) => profile.name), |
| 94 | + ).toEqual(["personal"]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + test("removing a codex profile leaves sibling profiles untouched", async () => { |
| 99 | + await withHome(async (home) => { |
| 100 | + clearSourceCredentials(); |
| 101 | + registerOAuthProvider("codex/night", "codex", "night"); |
| 102 | + const tokens = { access: "a", refresh: "r", expiresAt: 10_000_000 }; |
| 103 | + await saveCodexProfile({ name: "day", createdAt: 0, tokens }, home); |
| 104 | + await saveCodexProfile({ name: "night", createdAt: 0, tokens }, home); |
| 105 | + |
| 106 | + const target = oauthStoreForProvider("codex/night"); |
| 107 | + expect(await target?.removeProfile(target.profile, home)).toEqual([ |
| 108 | + "night", |
| 109 | + ]); |
| 110 | + expect(await loadCodexProfile("night", home)).toBeUndefined(); |
| 111 | + expect( |
| 112 | + (await listCodexProfiles(home)).map((profile) => profile.name), |
| 113 | + ).toEqual(["day"]); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + test("removing an already-absent profile is a no-op", async () => { |
| 118 | + await withHome(async (home) => { |
| 119 | + clearSourceCredentials(); |
| 120 | + registerOAuthProvider("xai/ghost", "xai", "ghost"); |
| 121 | + const target = oauthStoreForProvider("xai/ghost"); |
| 122 | + expect(await target?.removeProfile(target.profile, home)).toEqual([]); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + test("missing auth files are tolerated", async () => { |
| 127 | + await withHome(async (home) => { |
| 128 | + clearSourceCredentials(); |
| 129 | + registerOAuthProvider("xai/work", "xai", "work"); |
| 130 | + registerOAuthProvider("codex/work", "codex", "work"); |
| 131 | + // No profiles ever saved: the store treats a missing file as empty. |
| 132 | + const xai = oauthStoreForProvider("xai/work"); |
| 133 | + expect(await xai?.removeProfile(xai.profile, home)).toEqual([]); |
| 134 | + const codex = oauthStoreForProvider("codex/work"); |
| 135 | + expect(await codex?.removeProfile(codex.profile, home)).toEqual([]); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments