import { afterEach, expect, test } from "bun:test"; import { mkdtemp, mkdir, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { buildGitHubCopilotCost, githubCopilotModelSlug, githubCopilot, parseGitHubCopilotPricing, parseGitHubCopilotSource, type GitHubCopilotPricingRow, } from "../src/sync/providers/github-copilot.js"; import { syncProvider, type ExistingModel } from "../src/sync/index.js"; function row(overrides: Partial): GitHubCopilotPricingRow { return { model: "GPT-5.6 Terra", provider: "openai", release_status: "GA", category: "Versatile", input: "$2.00", cached_input: "$0.20", output: "$12.00", ...overrides, }; } test("slugifies display names into catalog filenames", () => { expect(githubCopilotModelSlug("GPT-5.6 Sol[^gpt-56-sol-promo]")).toBe("gpt-5.6-sol"); expect(githubCopilotModelSlug("GPT-5 mini")).toBe("gpt-5-mini"); expect(githubCopilotModelSlug("GPT-5.3-Codex")).toBe("gpt-5.3-codex"); expect(githubCopilotModelSlug("Claude Opus 4.8 (fast mode) (preview)")).toBe("claude-opus-4.8-fast-mode-preview"); expect(githubCopilotModelSlug("MAI-Code-1.1-Flash")).toBe("mai-code-1.1-flash"); expect(githubCopilotModelSlug("Kimi K2.7 Code")).toBe("kimi-k2.7-code"); }); test("groups tier rows under one model", () => { const models = parseGitHubCopilotPricing([ row({ threshold: "≤ 272K", tier: "Default" }), row({ threshold: "> 272K", tier: "Long context", input: "$4.00", cached_input: "$0.40", output: "$18.00" }), row({ model: "Claude Sonnet 5", provider: "anthropic", input: "$2.00", output: "$10.00", cache_write: "$2.50" }), ]); expect(models.map((model) => model.slug)).toEqual(["gpt-5.6-terra", "claude-sonnet-5"]); expect(models[0]?.rows).toHaveLength(2); }); test("builds flat cost with cache_write and Not applicable handling", () => { const [model] = parseGitHubCopilotPricing([ row({ model: "GPT-5.4 mini", input: "$0.75", cached_input: "$0.075", output: "$4.50", cache_write: "Not applicable" }), ]); expect(buildGitHubCopilotCost(model!)).toEqual({ input: 0.75, output: 4.5, cache_read: 0.075, cache_write: undefined, tiers: undefined, }); }); test("builds long-context tiers from threshold rows", () => { const [model] = parseGitHubCopilotPricing([ row({ threshold: "≤ 272K", tier: "Default", cache_write: "$2.50" }), row({ threshold: "> 272K", tier: "Long context", input: "$4.00", cached_input: "$0.40", output: "$18.00", cache_write: "$5.00" }), ]); expect(buildGitHubCopilotCost(model!)).toEqual({ input: 2, output: 12, cache_read: 0.2, cache_write: 2.5, tiers: [{ tier: { type: "context", size: 272_000 }, input: 4, output: 18, cache_read: 0.4, cache_write: 5, }], }); }); test("rejects malformed tables instead of writing garbage", () => { const build = (rows: GitHubCopilotPricingRow[]) => { const models = parseGitHubCopilotPricing(rows); return models.map((model) => buildGitHubCopilotCost(model)); }; // Unknown tier label. expect(() => build([row({ tier: "Standard" })])).toThrow(/Unknown pricing tier/u); // Two default rows for one model. expect(() => build([row({}), row({})])).toThrow(/exactly one default pricing row/u); // Long-context row without a parseable threshold. expect(() => build([ row({ threshold: "≤ 272K", tier: "Default" }), row({ threshold: "272K+", tier: "Long context" }), ])).toThrow(/Unparseable long-context threshold/u); // Price strings are schema-validated before translation. expect(() => build([row({ input: "$1,000.00" })])).toThrow(); expect(() => build([row({ input: "Included" })])).toThrow(); }); function translationContext(files: Record) { return { existing: (id: string) => files[id], authored: (id: string) => files[id], }; } const authoredTerra: ExistingModel = { base_model: "openai/gpt-5.6-terra", cost: { input: 1, output: 1, cache_read: 1 }, }; test("updates cost on the authored file and preserves audio rates", () => { const [model] = parseGitHubCopilotPricing([row({ cache_write: "$2.50" })]); const translated = githubCopilot.translateModel(model!, translationContext({ "gpt-5.6-terra": { ...authoredTerra, cost: { input: 1, output: 1, reasoning: 3, cache_read: 1, input_audio: 1.5, output_audio: 6 }, }, })); expect(translated?.id).toBe("gpt-5.6-terra"); expect(translated?.model.cost).toMatchObject({ input: 2, output: 12, reasoning: 3, cache_read: 0.2, cache_write: 2.5, input_audio: 1.5, output_audio: 6, }); expect((translated?.model as ExistingModel).base_model).toBe("openai/gpt-5.6-terra"); }); test("clears authored tiers and cache_write the table no longer lists", () => { const [model] = parseGitHubCopilotPricing([row({ cache_write: "Not applicable" })]); const translated = githubCopilot.translateModel(model!, translationContext({ "gpt-5.6-terra": { ...authoredTerra, cost: { input: 1, output: 1, cache_read: 1, cache_write: 9, tiers: [{ tier: { type: "context", size: 272_000 }, input: 9, output: 9 }], }, }, })); const cost = translated?.model.cost; expect(cost?.input).toBe(2); // Stale authored values must not survive the spread; the runner strips the // explicit undefineds before writing. expect(cost?.cache_write).toBeUndefined(); expect(cost?.tiers).toBeUndefined(); }); test("resolves preview filenames", () => { const preview = parseGitHubCopilotPricing([ row({ model: "Example Model", release_status: "Public preview" }), ]); expect(githubCopilot.translateModel(preview[0]!, translationContext({ "example-model-preview": authoredTerra, }))?.id).toBe("example-model-preview"); // An exact slug match wins over the preview fallback. expect(githubCopilot.translateModel(preview[0]!, translationContext({ "example-model": authoredTerra, "example-model-preview": authoredTerra, }))?.id).toBe("example-model"); }); test("skips ignored rows silently and unmatched rows with an ID", () => { const [fastMode] = parseGitHubCopilotPricing([ row({ model: "Claude Opus 4.8 (fast mode) (preview)", provider: "anthropic" }), ]); expect(githubCopilot.translateModel(fastMode!, translationContext({}))).toBeUndefined(); expect(githubCopilot.sourceID(fastMode!)).toBeUndefined(); const [unmatched] = parseGitHubCopilotPricing([row({ model: "Brand New Model" })]); expect(githubCopilot.translateModel(unmatched!, translationContext({}))).toBeUndefined(); expect(githubCopilot.sourceID(unmatched!)).toBe("brand-new-model"); }); function retirement(name: string, date: string) { return { name, retirement_date: date, suggested_alternative: "Latest model" }; } const today = "2026-11-24"; test("marks models retired once their retirement date has passed", () => { const models = parseGitHubCopilotSource({ pricing: [row({ model: "Claude Sonnet 4", provider: "anthropic" }), row({ model: "Grok 4.5", provider: "xai" })], retirements: [ retirement("Claude Sonnet 4", "2026-05-01"), retirement("Kimi K2.7 Code", "2026-11-24"), retirement("Grok 4.5", "2026-12-15"), ], }, today); expect(models.map(({ slug, rows, retirementDate, retired }) => ({ slug, rows: rows.length, retirementDate, retired }))).toEqual([ { slug: "claude-sonnet-4", rows: 1, retirementDate: "2026-05-01", retired: true }, { slug: "grok-4.5", rows: 1, retirementDate: "2026-12-15", retired: false }, { slug: "kimi-k2.7-code", rows: 0, retirementDate: "2026-11-24", retired: true }, ]); expect(githubCopilot.retiredIDs(models)).toEqual(["claude-sonnet-4", "kimi-k2.7-code"]); }); test("a retired model is neither translated nor reported as missing, even when still priced", () => { const [model] = parseGitHubCopilotSource({ pricing: [row({ model: "Claude Sonnet 4", provider: "anthropic", release_status: "Public preview" })], retirements: [retirement("Claude Sonnet 4", "2026-05-01")], }, today); expect(githubCopilot.sourceID(model!)).toBeUndefined(); expect(githubCopilot.translateModel(model!, translationContext({ "claude-sonnet-4": authoredTerra }))).toBeUndefined(); expect(githubCopilot.retiredIDs([model!])).toEqual(["claude-sonnet-4", "claude-sonnet-4-preview"]); }); test("a model with a future retirement date is deprecated and keeps syncing prices", () => { const [priced, unpriced] = parseGitHubCopilotSource({ pricing: [row({})], retirements: [retirement("GPT-5.6 Terra", "2026-12-15"), retirement("GPT-5.4", "2026-12-15")], }, today); expect(githubCopilot.sourceID(priced!)).toBe("gpt-5.6-terra"); expect(githubCopilot.sourceID(unpriced!)).toBeUndefined(); expect(githubCopilot.retiredIDs([priced!, unpriced!])).toEqual([]); expect(githubCopilot.translateModel(priced!, translationContext({ "gpt-5.6-terra": authoredTerra }))?.model) .toMatchObject({ status: "deprecated", cost: { input: 2, output: 12, cache_read: 0.2 } }); // No longer in the pricing table: the authored prices stay. expect(githubCopilot.translateModel(unpriced!, translationContext({ "gpt-5.4": authoredTerra }))?.model) .toMatchObject({ status: "deprecated", cost: authoredTerra.cost }); expect(githubCopilot.translateModel(unpriced!, translationContext({}))).toBeUndefined(); }); test.each(["Claude Sonnet 4.6", "Claude Opus 4.8"])("keeps %s for annual-plan subscribers despite its retirement", (name) => { const models = parseGitHubCopilotSource({ pricing: [row({ model: name, provider: "anthropic" })], retirements: [retirement(name, "2026-09-01")], }, today); expect(models).toHaveLength(1); const [model] = models; expect(githubCopilot.retiredIDs(models)).toEqual([]); expect(githubCopilot.sourceID(model!)).toBe(model!.slug); const translated = githubCopilot.translateModel(model!, translationContext({ [model!.slug]: authoredTerra })); expect(translated?.id).toBe(model!.slug); expect(translated?.model.status).toBeUndefined(); }); const pricingYaml = ` - model: 'GPT-5.6 Sol[^gpt-56-sol-promo]' provider: openai release_status: GA category: Powerful threshold: '≤ 272K' tier: Default input: $2.00 cached_input: $0.20 output: $10.00 cache_write: $2.50 - model: 'GPT-5.6 Sol[^gpt-56-sol-promo]' provider: openai release_status: GA category: Powerful threshold: '> 272K' tier: 'Long context' input: $4.00 cached_input: $0.40 output: $15.00 cache_write: $5.00 `; test("parses rows straight from the docs YAML format", () => { const models = parseGitHubCopilotPricing(Bun.YAML.parse(pricingYaml)); expect(models).toHaveLength(1); expect(models[0]?.slug).toBe("gpt-5.6-sol"); expect(buildGitHubCopilotCost(models[0]!)).toEqual({ input: 2, output: 10, cache_read: 0.2, cache_write: 2.5, tiers: [{ tier: { type: "context", size: 272_000 }, input: 4, output: 15, cache_read: 0.4, cache_write: 5, }], }); }); const roots: string[] = []; afterEach(async () => { for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true }); }); async function scratchProvider(files: Record, labs: string[]) { const root = await mkdtemp(path.join(os.tmpdir(), "github-copilot-sync-v2-test-")); roots.push(root); const modelsDir = path.join(root, "providers/github-copilot/models"); await mkdir(modelsDir, { recursive: true }); await Bun.write(path.join(root, "providers/github-copilot/provider.toml"), await Bun.file("providers/github-copilot/provider.toml").text()); for (const lab of labs) { await Bun.write(path.join(root, "models", `${lab}.toml`), await Bun.file(`models/${lab}.toml`).text()); } for (const [file, text] of Object.entries(files)) await Bun.write(path.join(modelsDir, file), text); return modelsDir; } test("runner patches prices in v2 files in place and keeps MIME, flags, status and API tables", async () => { const haiku = await Bun.file("providers/github-copilot/models/claude-haiku-4.5.toml").text(); const sol = await Bun.file("providers/github-copilot/models/gpt-5.6-sol.toml").text(); const retired = 'schema = 2\nbase_model = "openai/gpt-5.5"\nstatus = "deprecated"\n\n[capabilities]\nreasoning = { supported = true, effort = ["low", "medium", "high"] }\n\n[cost.text.input]\nprice = 5\n\n[cost.text.output]\nprice = 30\n'; const modelsDir = await scratchProvider({ "claude-haiku-4.5.toml": haiku.replace("[cost.text.output]\nprice = 5", "[cost.text.output]\nprice = 6"), "gpt-5.6-sol.toml": sol.replace("when = { context = { gt = 272_000 } }\nprice = 30", "when = { context = { gt = 272_000 } }\nprice = 31"), "gpt-5.5.toml": retired, }, ["anthropic/claude-haiku-4-5", "openai/gpt-5.6-sol", "openai/gpt-5.5"]); const rows = [ row({ model: "Claude Haiku 4.5", provider: "anthropic", input: "$1.00", cached_input: "$0.10", cache_write: "$1.25", output: "$5.00" }), row({ model: "GPT-5.6 Sol", threshold: "≤ 272K", tier: "Default", input: "$4.00", cached_input: "$0.40", cache_write: "$5.00", output: "$20.00" }), row({ model: "GPT-5.6 Sol", threshold: "> 272K", tier: "Long context", input: "$8.00", cached_input: "$0.80", cache_write: "$10.00", output: "$30.00" }), ]; const provider = { ...githubCopilot, modelsDir, fetchModels: async () => ({ pricing: rows, retirements: [] }) }; expect(await syncProvider(provider)).toMatchObject({ created: 0, updated: 2, deleted: 0, unchanged: 1 }); expect(await Bun.file(path.join(modelsDir, "claude-haiku-4.5.toml")).text()).toBe(haiku); expect(await Bun.file(path.join(modelsDir, "gpt-5.6-sol.toml")).text()).toBe(sol); expect(await Bun.file(path.join(modelsDir, "gpt-5.5.toml")).text()).toBe(retired); expect(await syncProvider(provider)).toMatchObject({ created: 0, updated: 0, deleted: 0, unchanged: 3 }); }); test("a price update keeps the mid-conversation flags copied from Anthropic", async () => { const opus = await Bun.file("providers/github-copilot/models/claude-opus-4.8.toml").text(); const modelsDir = await scratchProvider({ "claude-opus-4.8.toml": opus.replace("[cost.text.output]\nprice = 25", "[cost.text.output]\nprice = 26"), }, ["anthropic/claude-opus-4-8"]); const rows = [ row({ model: "Claude Opus 4.8", provider: "anthropic", input: "$5.00", cached_input: "$0.50", cache_write: "$6.25", output: "$25.00" }), ]; const provider = { ...githubCopilot, modelsDir, fetchModels: async () => ({ pricing: rows, retirements: [] }) }; expect(await syncProvider(provider)).toMatchObject({ updated: 1 }); const synced = await Bun.file(path.join(modelsDir, "claude-opus-4.8.toml")).text(); expect(Bun.TOML.parse(synced)).toMatchObject({ api: { messages: { capabilities: { system_updates: true } } }, cost: { text: { output: { price: 25 } } }, }); expect(synced).toBe(opus); }); test("runner removes retired files, deprecates scheduled ones and keeps other unpriced files", async () => { const haiku = await Bun.file("providers/github-copilot/models/claude-haiku-4.5.toml").text(); const sol = await Bun.file("providers/github-copilot/models/gpt-5.6-sol.toml").text(); const kept = 'schema = 2\nbase_model = "openai/gpt-5.5"\n\n[capabilities]\nreasoning = { supported = true, effort = ["low", "medium", "high"] }\n\n[cost.text.input]\nprice = 5\n\n[cost.text.output]\nprice = 30\n'; const modelsDir = await scratchProvider({ "claude-haiku-4.5.toml": haiku, "gpt-5.6-sol.toml": sol, "gpt-5.5.toml": kept, "kimi-k2.7-code.toml": kept, }, ["anthropic/claude-haiku-4-5", "openai/gpt-5.6-sol", "openai/gpt-5.5"]); const rows = [ row({ model: "Claude Haiku 4.5", provider: "anthropic", input: "$1.00", cached_input: "$0.10", cache_write: "$1.25", output: "$5.00" }), row({ model: "GPT-5.6 Sol", threshold: "≤ 272K", tier: "Default", input: "$4.00", cached_input: "$0.40", cache_write: "$5.00", output: "$20.00" }), row({ model: "GPT-5.6 Sol", threshold: "> 272K", tier: "Long context", input: "$8.00", cached_input: "$0.80", cache_write: "$10.00", output: "$30.00" }), ]; const retirements = [retirement("Kimi K2.7 Code", "2026-10-02"), retirement("Claude Haiku 4.5", "2999-01-01")]; const provider = { ...githubCopilot, modelsDir, fetchModels: async () => ({ pricing: rows, retirements }) }; expect(await syncProvider(provider, { dryRun: true })).toMatchObject({ created: 0, updated: 1, deleted: 1, unchanged: 2 }); expect(await Bun.file(path.join(modelsDir, "kimi-k2.7-code.toml")).exists()).toBe(true); expect(await syncProvider(provider)).toMatchObject({ created: 0, updated: 1, deleted: 1, unchanged: 2 }); expect(await Bun.file(path.join(modelsDir, "kimi-k2.7-code.toml")).exists()).toBe(false); expect(await Bun.file(path.join(modelsDir, "gpt-5.5.toml")).text()).toBe(kept); expect(Bun.TOML.parse(await Bun.file(path.join(modelsDir, "claude-haiku-4.5.toml")).text())).toMatchObject({ status: "deprecated" }); expect(await Bun.file(path.join(modelsDir, "gpt-5.6-sol.toml")).text()).toBe(sol); expect(await syncProvider(provider)).toMatchObject({ created: 0, updated: 0, deleted: 0, unchanged: 3 }); }); test("runner removes nothing when a model fails to translate", async () => { const kept = await Bun.file("providers/github-copilot/models/gpt-5.6-sol.toml").text(); const modelsDir = await scratchProvider({ "kimi-k2.7-code.toml": kept, "gpt-5.6-sol.toml": kept }, ["openai/gpt-5.6-sol"]); const rows = [row({ model: "GPT-5.6 Sol", tier: "Standard" })]; const provider = { ...githubCopilot, modelsDir, fetchModels: async () => ({ pricing: rows, retirements: [retirement("Kimi K2.7 Code", "2026-10-02")] }) }; expect(await syncProvider(provider)).toMatchObject({ deleted: 0 }); expect(await Bun.file(path.join(modelsDir, "kimi-k2.7-code.toml")).exists()).toBe(true); }); test("runner still reports a model scheduled for retirement that has no local file", async () => { const sol = await Bun.file("providers/github-copilot/models/gpt-5.6-sol.toml").text(); const modelsDir = await scratchProvider({ "gpt-5.6-sol.toml": sol }, ["openai/gpt-5.6-sol"]); const rows = [ row({ model: "GPT-5.6 Sol", threshold: "≤ 272K", tier: "Default", input: "$4.00", cached_input: "$0.40", cache_write: "$5.00", output: "$20.00" }), row({ model: "GPT-5.6 Sol", threshold: "> 272K", tier: "Long context", input: "$8.00", cached_input: "$0.80", cache_write: "$10.00", output: "$30.00" }), row({ model: "Grok 4.5", provider: "xai" }), ]; const retirements = [retirement("Grok 4.5", "2999-01-01"), retirement("Kimi K2.7 Code", "2999-01-01")]; const provider = { ...githubCopilot, modelsDir, fetchModels: async () => ({ pricing: rows, retirements }) }; const result = await syncProvider(provider, { dryRun: true }); expect(result).toMatchObject({ created: 0, updated: 0, deleted: 0 }); // Priced and scheduled for retirement: reported. Only in the retirement table: not. expect(result.notices.join("\n")).toContain("`grok-4.5`"); expect(result.notices.join("\n")).not.toContain("kimi-k2.7-code"); });