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