Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { usagePriceCell, usagePriceTableChanges, type UsagePriceDraft } from "./usagePriceTable";
4import type { UsagePriceTarget } from "./usagePriceTargets";
5
6const price = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
7const target = (
8 name: string,
9 prices: UsagePriceTarget["prices"],
10 aliases: UsagePriceTarget["aliases"] = {},
11): UsagePriceTarget => ({
12 environmentId: EnvironmentId.make(name),
13 label: name,
14 prices,
15 aliases,
16 unavailable: null,
17});
18const draft = (values: UsagePriceDraft["values"]): UsagePriceDraft => ({
19 id: "model:example",
20 model: "example",
21 isNew: false,
22 values,
23});
24
25describe("price table edits", () => {
26 it.each(["constructor", "toString", "__proto__"])(
27 "handles an exact model ID named %s",
28 (model) => {
29 const environment = target("a", {});
30 expect(usagePriceCell([environment], model, "inputCostPerMillionTokens")).toEqual({
31 value: "",
32 placeholder: "Automatic",
33 });
34 const result = usagePriceTableChanges(environment, [
35 {
36 ...draft({ inputCostPerMillionTokens: "2", outputCostPerMillionTokens: "8" }),
37 model,
38 isNew: true,
39 },
40 ]);
41 expect(result.changes).toEqual([{ model, price }]);
42 expect(
43 usagePriceCell([target("a", { [model]: price })], model, "inputCostPerMillionTokens").value,
44 ).toBe("2");
45 },
46 );
47
48 it("changes only edited columns while preserving each environment's other prices and models", () => {
49 const edits = [draft({ inputCostPerMillionTokens: "3" })];
50 const a = usagePriceTableChanges(
51 target("a", { example: { ...price, cacheReadCostPerMillionTokens: 0 }, untouched: price }),
52 edits,
53 );
54 const b = usagePriceTableChanges(
55 target("b", { example: { ...price, outputCostPerMillionTokens: 10 } }),
56 edits,
57 );
58 expect(a.changes).toEqual([
59 {
60 model: "example",
61 price: { ...price, inputCostPerMillionTokens: 3, cacheReadCostPerMillionTokens: 0 },
62 },
63 ]);
64 expect(b.changes).toEqual([
65 { model: "example", price: { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 10 } },
66 ]);
67 });
68
69 it("batches new rows and resets, and distinguishes blank cache rates from explicit zero", () => {
70 const plan = usagePriceTableChanges(target("a", { example: price }), [
71 { ...draft({}), removed: true },
72 {
73 id: "new:1",
74 model: " vendor/Model ",
75 isNew: true,
76 values: {
77 inputCostPerMillionTokens: "2",
78 outputCostPerMillionTokens: "8",
79 cacheReadCostPerMillionTokens: "0",
80 cacheWriteCostPerMillionTokens: "",
81 },
82 },
83 ]);
84 expect(plan.errors.size).toBe(0);
85 expect(plan.changes).toEqual([
86 { model: "example", price: null },
87 { model: "vendor/Model", price: { ...price, cacheReadCostPerMillionTokens: 0 } },
88 ]);
89 });
90
91 it("requires complete prices before turning automatic pricing into an override", () => {
92 const edits = [draft({ inputCostPerMillionTokens: "3" })];
93 expect(usagePriceTableChanges(target("a", { example: price }), edits).errors.size).toBe(0);
94 const missing = usagePriceTableChanges(target("b", {}), edits);
95 expect(missing.changes).toEqual([]);
96 expect(missing.errors.has("model:example")).toBe(true);
97 });
98
99 it("mapping a model drops its own price and resetting removes the mapping", () => {
100 const priced = target("a", { example: price });
101 expect(usagePriceTableChanges(priced, [{ ...draft({}), alias: " example-model " }])).toEqual({
102 changes: [
103 { model: "example", alias: "example-model" },
104 { model: "example", price: null },
105 ],
106 errors: new Map(),
107 });
108 const mapped = target("b", {}, { example: "example-model" });
109 expect(usagePriceTableChanges(mapped, [{ ...draft({}), removed: true }]).changes).toEqual([
110 { model: "example", alias: null },
111 ]);
112 expect(
113 usagePriceTableChanges(mapped, [{ ...draft({}), alias: "example" }]).errors.get(
114 "model:example",
115 ),
116 ).toBe("Map to a different model.");
117 });
118});