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 = (name: string, prices: UsagePriceTarget["prices"]): UsagePriceTarget => ({
13 environmentId: EnvironmentId.make(name),
14 label: name,
15 prices,
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("shows shared and mixed cells separately, and never treats missing settings as automatic", () => {
27 const targets = [
28 target("a", { example: price }),
29 target("b", { example: { ...price, outputCostPerMillionTokens: 10 } }),
30 ];
31 expect(usagePriceCell(targets, "example", "inputCostPerMillionTokens").value).toBe("2");
32 expect(usagePriceCell(targets, "example", "outputCostPerMillionTokens")).toEqual({
33 value: "",
34 placeholder: "Mixed",
35 });
36 expect(usagePriceCell(targets, "example", "cacheReadCostPerMillionTokens")).toEqual({
37 value: "",
38 placeholder: "Input rate",
39 });
40 expect(
41 usagePriceCell([target("a", {})], "example", "inputCostPerMillionTokens").placeholder,
42 ).toBe("Automatic");
43 expect(
44 usagePriceCell([targets[0]!, target("b", null)], "example", "inputCostPerMillionTokens")
45 .placeholder,
46 ).toBe("Unavailable");
47 });
48
49 it.each(["constructor", "toString", "__proto__"])(
50 "handles an exact model ID named %s",
51 (model) => {
52 const environment = target("a", {});
53 expect(usagePriceCell([environment], model, "inputCostPerMillionTokens")).toEqual({
54 value: "",
55 placeholder: "Automatic",
56 });
57 const result = usagePriceTableChanges(environment, [
58 {
59 ...draft({ inputCostPerMillionTokens: "2", outputCostPerMillionTokens: "8" }),
60 model,
61 isNew: true,
62 },
63 ]);
64 expect(result.changes).toEqual([{ model, price }]);
65 expect(
66 usagePriceCell([target("a", { [model]: price })], model, "inputCostPerMillionTokens").value,
67 ).toBe("2");
68 },
69 );
70
71 it("changes only edited columns while preserving each environment's other prices and models", () => {
72 const edits = [draft({ inputCostPerMillionTokens: "3" })];
73 const a = usagePriceTableChanges(
74 target("a", { example: { ...price, cacheReadCostPerMillionTokens: 0 }, untouched: price }),
75 edits,
76 );
77 const b = usagePriceTableChanges(
78 target("b", { example: { ...price, outputCostPerMillionTokens: 10 } }),
79 edits,
80 );
81 expect(a.changes).toEqual([
82 {
83 model: "example",
84 price: { ...price, inputCostPerMillionTokens: 3, cacheReadCostPerMillionTokens: 0 },
85 },
86 ]);
87 expect(b.changes).toEqual([
88 { model: "example", price: { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 10 } },
89 ]);
90 });
91
92 it("batches new rows and resets, and distinguishes blank cache rates from explicit zero", () => {
93 const plan = usagePriceTableChanges(target("a", { example: price }), [
94 { ...draft({}), removed: true },
95 {
96 id: "new:1",
97 model: " vendor/Model ",
98 isNew: true,
99 values: {
100 inputCostPerMillionTokens: "2",
101 outputCostPerMillionTokens: "8",
102 cacheReadCostPerMillionTokens: "0",
103 cacheWriteCostPerMillionTokens: "",
104 },
105 },
106 ]);
107 expect(plan.errors.size).toBe(0);
108 expect(plan.changes).toEqual([
109 { model: "example", price: null },
110 { model: "vendor/Model", price: { ...price, cacheReadCostPerMillionTokens: 0 } },
111 ]);
112 });
113
114 it("requires complete prices before turning automatic pricing into an override", () => {
115 const edits = [draft({ inputCostPerMillionTokens: "3" })];
116 expect(usagePriceTableChanges(target("a", { example: price }), edits).errors.size).toBe(0);
117 const missing = usagePriceTableChanges(target("b", {}), edits);
118 expect(missing.changes).toEqual([]);
119 expect(missing.errors.has("model:example")).toBe(true);
120 });
121
122 it("ignores empty new rows without blocking other edits", () => {
123 const environment = target("a", { example: price });
124 const empty: UsagePriceDraft = {
125 id: "new:1",
126 model: " ",
127 isNew: true,
128 values: { inputCostPerMillionTokens: " " },
129 };
130 const plan = usagePriceTableChanges(environment, [
131 draft({ inputCostPerMillionTokens: "3" }),
132 empty,
133 ]);
134 expect(plan.errors.size).toBe(0);
135 expect(plan.changes).toEqual([
136 { model: "example", price: { ...price, inputCostPerMillionTokens: 3 } },
137 ]);
138 expect(usagePriceTableChanges(environment, [empty]).changes).toEqual([]);
139 expect(
140 usagePriceTableChanges(environment, [
141 { ...empty, values: { inputCostPerMillionTokens: "0" } },
142 ]).errors.get(empty.id),
143 ).toBe("Enter a model ID.");
144 });
145
146 it.each(["Offline", "Read-only access", "Update server to edit prices"])(
147 "does not let %s destinations block a valid edit elsewhere",
148 (unavailable) => {
149 const edits = [draft({ inputCostPerMillionTokens: "3" })];
150 const writable = target("writable", { example: price });
151 const other = target("other", {});
152 expect(usagePriceTableErrors([writable, { ...other, unavailable }], edits).size).toBe(0);
153 expect(usagePriceTableErrors([writable, other], edits).has("model:example")).toBe(true);
154 },
155 );
156
157 it("does not write unchanged rates and rejects invalid edited cells", () => {
158 const environment = target("a", { example: price });
159 expect(
160 usagePriceTableChanges(environment, [draft({ inputCostPerMillionTokens: "2.00" })]).changes,
161 ).toEqual([]);
162 expect(
163 usagePriceTableChanges(environment, [draft({ cacheReadCostPerMillionTokens: "-1" })]).errors
164 .size,
165 ).toBe(1);
166 expect(
167 usagePriceTableChanges(environment, [draft({ outputCostPerMillionTokens: "" })]).errors.size,
168 ).toBe(1);
169 });
170});