Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 usagePriceCell,
5 usagePriceTableChanges,
6 usagePriceTableErrors,
7 type UsagePriceDraft,
8} from "./usagePriceTable";
9import type { UsagePriceTarget } from "./usagePriceTargets";
10
11const price = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
12const target = (
13 name: string,
14 prices: UsagePriceTarget["prices"],
15 aliases: UsagePriceTarget["aliases"] = {},
16): UsagePriceTarget => ({
17 environmentId: EnvironmentId.make(name),
18 label: name,
19 prices,
20 aliases,
21 unavailable: null,
22});
23const draft = (values: UsagePriceDraft["values"]): UsagePriceDraft => ({
24 id: "model:example",
25 model: "example",
26 isNew: false,
27 values,
28});
29
30describe("price table edits", () => {
31 it.each(["constructor", "toString", "__proto__"])(
32 "handles an exact model ID named %s",
33 (model) => {
34 const environment = target("a", {});
35 expect(usagePriceCell([environment], model, "inputCostPerMillionTokens")).toEqual({
36 value: "",
37 placeholder: "Automatic",
38 });
39 const result = usagePriceTableChanges(environment, [
40 {
41 ...draft({ inputCostPerMillionTokens: "2", outputCostPerMillionTokens: "8" }),
42 model,
43 isNew: true,
44 },
45 ]);
46 expect(result.changes).toEqual([{ model, price }]);
47 expect(
48 usagePriceCell([target("a", { [model]: price })], model, "inputCostPerMillionTokens").value,
49 ).toBe("2");
50 },
51 );
52
53 it("changes only edited columns while preserving each environment's other prices and models", () => {
54 const edits = [draft({ inputCostPerMillionTokens: "3" })];
55 const a = usagePriceTableChanges(
56 target("a", { example: { ...price, cacheReadCostPerMillionTokens: 0 }, untouched: price }),
57 edits,
58 );
59 const b = usagePriceTableChanges(
60 target("b", { example: { ...price, outputCostPerMillionTokens: 10 } }),
61 edits,
62 );
63 expect(a.changes).toEqual([
64 {
65 model: "example",
66 price: { ...price, inputCostPerMillionTokens: 3, cacheReadCostPerMillionTokens: 0 },
67 },
68 ]);
69 expect(b.changes).toEqual([
70 { model: "example", price: { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 10 } },
71 ]);
72 });
73
74 it("batches new rows and resets, and distinguishes blank cache rates from explicit zero", () => {
75 const plan = usagePriceTableChanges(target("a", { example: price }), [
76 { ...draft({}), removed: true },
77 {
78 id: "new:1",
79 model: " vendor/Model ",
80 isNew: true,
81 values: {
82 inputCostPerMillionTokens: "2",
83 outputCostPerMillionTokens: "8",
84 cacheReadCostPerMillionTokens: "0",
85 cacheWriteCostPerMillionTokens: "",
86 },
87 },
88 ]);
89 expect(plan.errors.size).toBe(0);
90 expect(plan.changes).toEqual([
91 { model: "example", price: null },
92 { model: "vendor/Model", price: { ...price, cacheReadCostPerMillionTokens: 0 } },
93 ]);
94 });
95
96 it("requires complete prices before turning automatic pricing into an override", () => {
97 const edits = [draft({ inputCostPerMillionTokens: "3" })];
98 expect(usagePriceTableChanges(target("a", { example: price }), edits).errors.size).toBe(0);
99 const missing = usagePriceTableChanges(target("b", {}), edits);
100 expect(missing.changes).toEqual([]);
101 expect(missing.errors.has("model:example")).toBe(true);
102 });
103
104 it("mapping a model drops its own price and resetting removes the mapping", () => {
105 const priced = target("a", { example: price });
106 expect(usagePriceTableChanges(priced, [{ ...draft({}), alias: " example-model " }])).toEqual({
107 changes: [
108 { model: "example", alias: "example-model" },
109 { model: "example", price: null },
110 ],
111 errors: new Map(),
112 });
113 const mapped = target("b", {}, { example: "example-model" });
114 expect(usagePriceTableChanges(mapped, [{ ...draft({}), removed: true }]).changes).toEqual([
115 { model: "example", alias: null },
116 ]);
117 expect(
118 usagePriceTableChanges(mapped, [{ ...draft({}), alias: "example" }]).errors.get(
119 "model:example",
120 ),
121 ).toBe("Map to a different model.");
122 });
123
124 it("requires clearing a mapping before a price edit can apply to every environment", () => {
125 const mapped = target("mapped", {}, { example: "example-model" });
126 const priced = target("priced", { example: price });
127 const edit = draft({ inputCostPerMillionTokens: "3", outputCostPerMillionTokens: "8" });
128 expect(usagePriceTableErrors([mapped, priced], [edit]).get(edit.id)).toBe(
129 'mapped maps this model. Remove it from "Apply to" or clear its mapping first.',
130 );
131 expect(usagePriceTableChanges(mapped, [edit]).changes).toEqual([]);
132
133 const cleared = { ...edit, alias: "" };
134 expect(usagePriceTableErrors([mapped, priced], [cleared]).size).toBe(0);
135 expect(usagePriceTableChanges(mapped, [cleared]).changes).toEqual([
136 { model: "example", alias: null },
137 { model: "example", price: { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 8 } },
138 ]);
139 expect(usagePriceTableChanges(priced, [cleared]).changes).toEqual([
140 { model: "example", price: { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 8 } },
141 ]);
142 });
143});
144
145describe("model mapping validation", () => {
146 const mapping = (model: string, alias: string): UsagePriceDraft => ({
147 id: `model:${model}`,
148 model,
149 alias,
150 isNew: false,
151 values: {},
152 });
153
154 it("rejects a cycle created by several mappings in one save", () => {
155 const edits = [mapping("a", "b"), mapping("b", "c"), mapping("c", "a")];
156 const result = usagePriceTableChanges(target("local", {}), edits);
157 expect([...result.errors.keys()]).toEqual(["model:a", "model:b", "model:c"]);
158 });
159
160 it("checks each environment's saved chain when an edited mapping closes it", () => {
161 const edits = [mapping("c", "a")];
162 const acyclic = target("one", {}, { a: "b" });
163 const cyclic = target("two", {}, { a: "b", b: "c" });
164 expect(usagePriceTableChanges(acyclic, edits).errors.size).toBe(0);
165 expect(usagePriceTableErrors([acyclic, cyclic], edits).get("model:c")).toBe(
166 "Model mappings cannot form a cycle on two.",
167 );
168 });
169
170 it("validates the final batch so a mapping can be reversed in either row order", () => {
171 const environment = target("local", {}, { constructor: "__proto__" });
172 const clear = mapping("constructor", "");
173 const reverse = mapping("__proto__", "constructor");
174 for (const edits of [
175 [reverse, clear],
176 [clear, reverse],
177 ]) {
178 const result = usagePriceTableChanges(environment, edits);
179 expect(result.errors.size).toBe(0);
180 expect(result.changes).toEqual(
181 edits.map(({ model, alias }) => ({ model, alias: alias || null })),
182 );
183 }
184 });
185});