mirror of
https://github.com/anomalyco/models.dev.git
synced 2026-12-02 03:41:53 +08:00
* fix(sync): leave false mid-conversation flags out * fix(aimlapi): drop capabilities tables left empty
314 lines
12 KiB
TypeScript
314 lines
12 KiB
TypeScript
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,
|
|
type GitHubCopilotPricingRow,
|
|
} from "../src/sync/providers/github-copilot.js";
|
|
import { syncProvider, type ExistingModel } from "../src/sync/index.js";
|
|
|
|
function row(overrides: Partial<GitHubCopilotPricingRow>): 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<string, ExistingModel>) {
|
|
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");
|
|
});
|
|
|
|
test.each([
|
|
"Claude Sonnet 4",
|
|
"Claude Sonnet 4.5",
|
|
"Claude Opus 4.5",
|
|
"Claude Opus 4.6",
|
|
"Gemini 3.1 Pro",
|
|
"GPT-4.1",
|
|
"GPT-5.2",
|
|
"GPT-5.2-Codex",
|
|
"MAI-Code-1-Flash",
|
|
"Raptor mini",
|
|
])("ignores retired %s for translation and missing-model discovery", (name) => {
|
|
const [model] = parseGitHubCopilotPricing([row({ model: name, release_status: "Public preview" })]);
|
|
expect(githubCopilot.sourceID(model!)).toBeUndefined();
|
|
expect(githubCopilot.translateModel(model!, translationContext({}))).toBeUndefined();
|
|
expect(githubCopilot.translateModel(model!, translationContext({
|
|
[model!.slug]: authoredTerra,
|
|
}))).toBeUndefined();
|
|
expect(githubCopilot.translateModel(model!, translationContext({
|
|
[`${model!.slug}-preview`]: authoredTerra,
|
|
}))).toBeUndefined();
|
|
});
|
|
|
|
test("keeps Sonnet 4.6 eligible for annual-plan subscribers", () => {
|
|
const [model] = parseGitHubCopilotPricing([row({ model: "Claude Sonnet 4.6", provider: "anthropic" })]);
|
|
expect(githubCopilot.sourceID(model!)).toBe("claude-sonnet-4.6");
|
|
expect(githubCopilot.translateModel(model!, translationContext({
|
|
"claude-sonnet-4.6": authoredTerra,
|
|
}))?.id).toBe("claude-sonnet-4.6");
|
|
});
|
|
|
|
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<string, string>, 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 = await Bun.file("providers/github-copilot/models/gpt-5.5.toml").text();
|
|
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 () => rows };
|
|
|
|
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 () => rows };
|
|
|
|
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({
|
|
capabilities: { tools: { supported: true, updates: true }, system_updates: true },
|
|
cost: { text: { output: { price: 25 } } },
|
|
});
|
|
expect(synced).toBe(opus);
|
|
});
|