|
| 1 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { 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 { oauthStoreForProvider } from "./remove-provider.js"; |
| 17 | + |
| 18 | +async function withHome<T>(fn: (home: string) => Promise<T>): Promise<T> { |
| 19 | + const home = await mkdtemp(join(tmpdir(), "remove-provider-")); |
| 20 | + try { |
| 21 | + return await fn(home); |
| 22 | + } finally { |
| 23 | + await rm(home, { recursive: true, force: true }); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +describe("oauthStoreForProvider", () => { |
| 28 | + test("maps xai/ and codex/ provider names to their store and profile", () => { |
| 29 | + expect(oauthStoreForProvider("xai/work")?.profile).toBe("work"); |
| 30 | + expect(oauthStoreForProvider("codex/personal")?.profile).toBe("personal"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("returns null for API-key, keyless, and degenerate names", () => { |
| 34 | + expect(oauthStoreForProvider("openai")).toBeNull(); |
| 35 | + expect(oauthStoreForProvider("ollama")).toBeNull(); |
| 36 | + expect(oauthStoreForProvider("my-custom")).toBeNull(); |
| 37 | + expect(oauthStoreForProvider("xai/")).toBeNull(); |
| 38 | + expect(oauthStoreForProvider("codex/")).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + test("removing an xai profile leaves sibling profiles untouched", async () => { |
| 42 | + await withHome(async (home) => { |
| 43 | + const tokens = { access: "a", refresh: "r", expiresAt: 10_000_000 }; |
| 44 | + await saveXaiProfile({ name: "work", createdAt: 0, tokens }, home); |
| 45 | + await saveXaiProfile({ name: "personal", createdAt: 0, tokens }, home); |
| 46 | + |
| 47 | + const target = oauthStoreForProvider("xai/work"); |
| 48 | + expect(target?.profile).toBe("work"); |
| 49 | + expect(await target?.removeProfile(target.profile, home)).toEqual([ |
| 50 | + "work", |
| 51 | + ]); |
| 52 | + expect(await loadXaiProfile("work", home)).toBeUndefined(); |
| 53 | + expect( |
| 54 | + (await listXaiProfiles(home)).map((profile) => profile.name), |
| 55 | + ).toEqual(["personal"]); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + test("removing a codex profile leaves sibling profiles untouched", async () => { |
| 60 | + await withHome(async (home) => { |
| 61 | + const tokens = { access: "a", refresh: "r", expiresAt: 10_000_000 }; |
| 62 | + await saveCodexProfile({ name: "day", createdAt: 0, tokens }, home); |
| 63 | + await saveCodexProfile({ name: "night", createdAt: 0, tokens }, home); |
| 64 | + |
| 65 | + const target = oauthStoreForProvider("codex/night"); |
| 66 | + expect(await target?.removeProfile(target.profile, home)).toEqual([ |
| 67 | + "night", |
| 68 | + ]); |
| 69 | + expect(await loadCodexProfile("night", home)).toBeUndefined(); |
| 70 | + expect( |
| 71 | + (await listCodexProfiles(home)).map((profile) => profile.name), |
| 72 | + ).toEqual(["day"]); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + test("removing an already-absent profile is a no-op", async () => { |
| 77 | + await withHome(async (home) => { |
| 78 | + const target = oauthStoreForProvider("xai/ghost"); |
| 79 | + expect(await target?.removeProfile(target.profile, home)).toEqual([]); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + test("missing auth files are tolerated", async () => { |
| 84 | + await withHome(async (home) => { |
| 85 | + // No profiles ever saved: the store treats a missing file as empty. |
| 86 | + const xai = oauthStoreForProvider("xai/work"); |
| 87 | + expect(await xai?.removeProfile(xai.profile, home)).toEqual([]); |
| 88 | + const codex = oauthStoreForProvider("codex/work"); |
| 89 | + expect(await codex?.removeProfile(codex.profile, home)).toEqual([]); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments