|
| 1 | +import { afterEach, describe, expect, mock, test } from "bun:test"; |
| 2 | +import * as storeModule from "../src/oauth/store"; |
| 3 | +import * as usabilityModule from "../src/codex/account-usability"; |
| 4 | +import * as modelRowsModule from "../src/server/management/model-rows"; |
| 5 | + |
| 6 | +let accountSets: Record<string, { accounts: Array<{ id: string; needsReauth?: boolean }>; activeAccountId?: string }> = {}; |
| 7 | +let usableCodexAccounts: Set<string> = new Set(); |
| 8 | +let managementRows: Array<Record<string, unknown>> = []; |
| 9 | + |
| 10 | +mock.module("../src/oauth/store", () => ({ |
| 11 | + ...storeModule, |
| 12 | + getAccountSet: (provider: string) => accountSets[provider] ?? null, |
| 13 | +})); |
| 14 | +mock.module("../src/codex/account-usability", () => ({ |
| 15 | + ...usabilityModule, |
| 16 | + isCodexAccountUsable: (_config: unknown, accountId: string) => usableCodexAccounts.has(accountId), |
| 17 | +})); |
| 18 | +mock.module("../src/server/management/model-rows", () => ({ |
| 19 | + ...modelRowsModule, |
| 20 | + listManagementModelRows: async () => managementRows, |
| 21 | +})); |
| 22 | + |
| 23 | +import { isWebSearchAuthSlotModel, webSearchSidecarCandidates } from "../src/web-search/backends"; |
| 24 | +import { pickerVisibleSidecarCandidates } from "../src/sidecar/candidates"; |
| 25 | +import { resolveSidecarAuth } from "../src/sidecar/auth"; |
| 26 | +import { resolveSidecarBackend } from "../src/web-search"; |
| 27 | +import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/account-id"; |
| 28 | +import type { OcxConfig, OcxProviderConfig } from "../src/types"; |
| 29 | + |
| 30 | +const forward: OcxProviderConfig = { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" }; |
| 31 | +const anthropicOAuth: OcxProviderConfig = { adapter: "anthropic", baseUrl: "https://api.anthropic.com", authMode: "oauth" }; |
| 32 | + |
| 33 | +function config(overrides: Partial<OcxConfig> = {}): OcxConfig { |
| 34 | + return { port: 10100, defaultProvider: "openai", providers: { openai: forward, claude: anthropicOAuth }, ...overrides }; |
| 35 | +} |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + accountSets = {}; |
| 39 | + usableCodexAccounts = new Set(); |
| 40 | + managementRows = []; |
| 41 | +}); |
| 42 | + |
| 43 | +async function candidatesFor(cfg: OcxConfig) { |
| 44 | + const auth = resolveSidecarAuth(cfg); |
| 45 | + const all = await pickerVisibleSidecarCandidates(cfg, auth); |
| 46 | + return webSearchSidecarCandidates(cfg, auth, all); |
| 47 | +} |
| 48 | + |
| 49 | +describe("webSearchSidecarCandidates = (picker ∪ slots) ∩ active backend", () => { |
| 50 | + test("no Codex login: openai side inactive — native rows and Luna absent, Haiku present", async () => { |
| 51 | + accountSets = { claude: { accounts: [{ id: "a1" }], activeAccountId: "a1" } }; |
| 52 | + managementRows = [ |
| 53 | + { provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }, |
| 54 | + { provider: "claude", id: "claude-sonnet-5", disabled: false }, |
| 55 | + ]; |
| 56 | + const ids = (await candidatesFor(config())).map(c => c.id).sort(); |
| 57 | + expect(ids).toEqual(["claude-haiku-4-5", "claude-sonnet-5"]); |
| 58 | + }); |
| 59 | + |
| 60 | + test("hidden Luna + Codex login: present via the slot", async () => { |
| 61 | + usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID); |
| 62 | + managementRows = [{ provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }]; |
| 63 | + const cfg = config({ disabledModels: ["gpt-5.6-luna"] }); |
| 64 | + const ids = (await candidatesFor(cfg)).map(c => c.id).sort(); |
| 65 | + expect(ids).toEqual(["gpt-5.6-luna", "gpt-5.6-terra"]); |
| 66 | + }); |
| 67 | + |
| 68 | + test("rows outside both backend families are excluded even when picker-visible", async () => { |
| 69 | + usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID); |
| 70 | + accountSets = { claude: { accounts: [{ id: "a1" }], activeAccountId: "a1" } }; |
| 71 | + managementRows = [ |
| 72 | + { provider: "routed", id: "grok-4.6", disabled: false }, |
| 73 | + { provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }, |
| 74 | + ]; |
| 75 | + const ids = (await candidatesFor(config())).map(c => c.id).sort(); |
| 76 | + expect(ids).toEqual(["claude-haiku-4-5", "gpt-5.6-luna", "gpt-5.6-terra"]); |
| 77 | + }); |
| 78 | + |
| 79 | + test("keyed same-adapter anthropic provider rows stay unreachable", async () => { |
| 80 | + accountSets = { claude: { accounts: [{ id: "a1" }], activeAccountId: "a1" } }; |
| 81 | + const keyedClaude: OcxProviderConfig = { adapter: "anthropic", baseUrl: "https://api.anthropic.com", authMode: "key", apiKey: "k" }; |
| 82 | + managementRows = [{ provider: "keyed-claude", id: "claude-sonnet-5", disabled: false }]; |
| 83 | + const cfg = config({ providers: { openai: forward, claude: anthropicOAuth, "keyed-claude": keyedClaude } }); |
| 84 | + const ids = (await candidatesFor(cfg)).map(c => c.id).sort(); |
| 85 | + expect(ids).toEqual(["claude-haiku-4-5"]); |
| 86 | + }); |
| 87 | + |
| 88 | + test("no logins at all -> empty candidate set", async () => { |
| 89 | + managementRows = [{ provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }]; |
| 90 | + expect(await candidatesFor(config())).toEqual([]); |
| 91 | + }); |
| 92 | + |
| 93 | + test("account-bound selector/slug native rows are excluded (executor cannot run them)", async () => { |
| 94 | + usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID); |
| 95 | + managementRows = [ |
| 96 | + { provider: "openai", id: "work/gpt-future-unlisted", disabled: false, native: true }, |
| 97 | + { provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }, |
| 98 | + ]; |
| 99 | + const ids = (await candidatesFor(config())).map(c => c.id).sort(); |
| 100 | + expect(ids).toEqual(["gpt-5.6-luna", "gpt-5.6-terra"]); |
| 101 | + }); |
| 102 | + |
| 103 | + test("custom openai-keyed rows without the native flag are excluded", async () => { |
| 104 | + usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID); |
| 105 | + managementRows = [ |
| 106 | + { provider: "openai", id: "my-custom-gpt", disabled: false }, |
| 107 | + { provider: "openai", id: "gpt-5.6-terra", disabled: false, native: true }, |
| 108 | + ]; |
| 109 | + const ids = (await candidatesFor(config())).map(c => c.id).sort(); |
| 110 | + expect(ids).toEqual(["gpt-5.6-luna", "gpt-5.6-terra"]); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe("existing contracts stay pinned", () => { |
| 115 | + test("resolveSidecarBackend(undefined) is still openai (B4: no default change)", () => { |
| 116 | + expect(resolveSidecarBackend(undefined)).toBe("openai"); |
| 117 | + }); |
| 118 | + |
| 119 | + test("auth-slot membership helper knows exactly the two slot models", () => { |
| 120 | + expect(isWebSearchAuthSlotModel("gpt-5.6-luna")).toBe(true); |
| 121 | + expect(isWebSearchAuthSlotModel("claude-haiku-4-5")).toBe(true); |
| 122 | + expect(isWebSearchAuthSlotModel("gpt-5.6-terra")).toBe(false); |
| 123 | + }); |
| 124 | +}); |
0 commit comments