Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 lookupRate,
5 mergeRateTables,
6 normalizeModelName,
7 parseOpenCodeRates,
8 parseRateTable,
9 type RateTable,
10} from "./usagePricing.ts";
11
12describe("parseOpenCodeRates", () => {
13 it("projects models.dev costs into per-token rates keyed by providerID/modelID", () => {
14 const table = parseOpenCodeRates({
15 "opencode-go": {
16 models: {
17 "deepseek-v4-flash": { cost: { input: 0.07, output: 0.14, cache_read: 0.0014 } },
18 },
19 },
20 opencode: {
21 models: {
22 "deepseek-v4-flash": { cost: { input: 0.14, output: 0.28, cache_read: 0.0028 } },
23 },
24 },
25 });
26
27 const go = table.get("opencode-go/deepseek-v4-flash");
28 const zen = table.get("opencode/deepseek-v4-flash");
29 expect(go?.inputCostPerToken).toBeCloseTo(0.07e-6, 12);
30 expect(go?.outputCostPerToken).toBeCloseTo(0.14e-6, 12);
31 expect(go?.cacheReadCostPerToken).toBeCloseTo(0.0014e-6, 15);
32 // The same model id under two subscriptions must not collapse.
33 expect(zen?.inputCostPerToken).toBeCloseTo(0.14e-6, 12);
34 });
35
36 it("falls back to the input rate when a model omits cache pricing", () => {
37 const table = parseOpenCodeRates({
38 "opencode-go": { models: { "kimi-k3": { cost: { input: 3, output: 15 } } } },
39 });
40
41 const rate = table.get("opencode-go/kimi-k3");
42 expect(rate?.cacheReadCostPerToken).toBe(rate?.inputCostPerToken);
43 expect(rate?.cacheCreationCostPerToken).toBe(rate?.inputCostPerToken);
44 });
45
46 it("keeps zero-cost free models priced at zero rather than unpriced", () => {
47 const table = parseOpenCodeRates({
48 opencode: { models: { "deepseek-v4-flash-free": { cost: { input: 0, output: 0 } } } },
49 });
50
51 expect(table.get("opencode/deepseek-v4-flash-free")).toEqual({
52 inputCostPerToken: 0,
53 outputCostPerToken: 0,
54 cacheReadCostPerToken: 0,
55 cacheCreationCostPerToken: 0,
56 });
57 });
58
59 it("ignores foreign providers and half-priced entries", () => {
60 const table = parseOpenCodeRates({
61 anthropic: { models: { "claude-sonnet-4-5": { cost: { input: 3, output: 15 } } } },
62 "opencode-go": { models: { half: { cost: { input: 1 } } } },
63 });
64
65 expect(table.size).toBe(0);
66 });
67});
68
69describe("lookupRate", () => {
70 const table: RateTable = new Map([
71 [
72 "opencode-go/deepseek-v4-flash",
73 {
74 inputCostPerToken: 0.07e-6,
75 outputCostPerToken: 0.14e-6,
76 cacheReadCostPerToken: 0.0014e-6,
77 cacheCreationCostPerToken: 0.07e-6,
78 },
79 ],
80 [
81 "claude-fable-5",
82 {
83 inputCostPerToken: 1e-5,
84 outputCostPerToken: 5e-5,
85 cacheReadCostPerToken: 1e-6,
86 cacheCreationCostPerToken: 1.25e-5,
87 },
88 ],
89 ]);
90
91 it("prefers the exact qualified name over the stripped fallback", () => {
92 expect(lookupRate(table, "opencode-go/deepseek-v4-flash")?.inputCostPerToken).toBeCloseTo(
93 0.07e-6,
94 12,
95 );
96 });
97
98 it("strips a provider/ prefix for bare model names", () => {
99 expect(lookupRate(table, "anthropic/claude-fable-5")?.inputCostPerToken).toBe(1e-5);
100 expect(normalizeModelName("anthropic/claude-fable-5")).toBe("claude-fable-5");
101 });
102
103 it("returns null for unknown models", () => {
104 expect(lookupRate(table, "opencode-go/nope")).toBeNull();
105 });
106});
107
108describe("mergeRateTables", () => {
109 it("overlays the opencode rates so its models win on conflict", () => {
110 const base: RateTable = new Map([
111 [
112 "deepseek-v4-flash",
113 {
114 inputCostPerToken: 0.5e-6,
115 outputCostPerToken: 1e-6,
116 cacheReadCostPerToken: 0.5e-6,
117 cacheCreationCostPerToken: 0.5e-6,
118 },
119 ],
120 ]);
121 const opencode = parseOpenCodeRates({
122 "opencode-go": { models: { "deepseek-v4-flash": { cost: { input: 0.07, output: 0.14 } } } },
123 });
124
125 const merged = mergeRateTables(base, opencode);
126 expect(merged.get("opencode-go/deepseek-v4-flash")).toEqual(
127 opencode.get("opencode-go/deepseek-v4-flash"),
128 );
129 expect(merged.get("deepseek-v4-flash")?.inputCostPerToken).toBe(0.5e-6);
130 });
131});
132
133describe("parseRateTable", () => {
134 it("strips LiteLLM provider prefixes when keying", () => {
135 const table = parseRateTable({
136 "anthropic/claude-fable-5": {
137 input_cost_per_token: 1e-5,
138 output_cost_per_token: 5e-5,
139 },
140 });
141
142 expect(table.get("claude-fable-5")).not.toBeUndefined();
143 });
144});