Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usagePriceTargets.test.ts

129 lines4.6 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 writeUsagePrices,
6 type UsagePriceTarget,
7 type UsagePriceWriteResult,
8} from "./usagePriceTargets";
9​
10const price = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
11const target = (id: string, overrides: Partial<UsagePriceTarget> = {}): UsagePriceTarget => ({
12 environmentId: EnvironmentId.make(id),
13 label: id,
14 prices: { example: price },
15 aliases: {},
16 unavailable: null,
17 ...overrides,
18});
19​
20describe("model price writes", () => {
21 it("sends all table changes in one patch per environment and skips unchanged destinations", async () => {
22 const write = vi.fn(async () => ({ _tag: "Success" as const }));
23 const onResult = vi.fn();
24 await writeUsagePrices({
25 targets: [target("edited"), target("unchanged")],
26 changes: new Map([
27 [
28 EnvironmentId.make("edited"),
29 [
30 { model: "new-model", price },
31 { model: "example", price: null },
32 ],
33 ],
34 ]),
35 write,
36 onResult,
37 });
38 expect(write).toHaveBeenCalledExactlyOnceWith({
39 environmentId: "edited",
40 input: { patch: { usagePriceOverrides: { "new-model": price, example: null } } },
41 });
42 expect(onResult).toHaveBeenCalledWith("unchanged", { status: "saved" });
43 });
44​
45 it("saves independently, skips unavailable targets, and retries failures without rewriting successes", async () => {
46 let resolveSlow!: (result: { _tag: "Success" | "Failure" }) => void;
47 const slow = new Promise<{ _tag: "Success" | "Failure" }>((resolve) => {
48 resolveSlow = resolve;
49 });
50 let resolveFastSaved!: () => void;
51 const fastSaved = new Promise<void>((resolve) => {
52 resolveFastSaved = resolve;
53 });
54 const targets = [target("fast"), target("slow"), target("offline", { unavailable: "Offline" })];
55 const results = new Map<EnvironmentId, UsagePriceWriteResult>();
56 const write = vi.fn(async ({ environmentId }: { environmentId: EnvironmentId }) =>
57 environmentId === "slow" ? slow : { _tag: "Success" as const },
58 );
59 const change = { model: "example", price };
60 const running = writeUsagePrices({
61 targets,
62 changes: new Map(targets.map((target) => [target.environmentId, [change]])),
63 write,
64 onResult: (id, result) => {
65 results.set(id, result);
66 if (id === "fast") resolveFastSaved();
67 },
68 });
69 await fastSaved;
70 expect(results.get(EnvironmentId.make("fast"))).toEqual({ status: "saved" });
71 expect(results.get(EnvironmentId.make("offline"))).toEqual({
72 status: "failed",
73 error: "Offline",
74 });
75 expect(results.has(EnvironmentId.make("slow"))).toBe(false);
76 resolveSlow({ _tag: "Failure" });
77 await running;
78 expect(write).toHaveBeenCalledTimes(2);
79 expect(write).toHaveBeenCalledWith({
80 environmentId: "fast",
81 input: { patch: { usagePriceOverrides: { example: price } } },
82 });
83 const retry = vi.fn(async () => ({ _tag: "Success" as const }));
84 await writeUsagePrices({
85 targets: targets
86 .filter((entry) => results.get(entry.environmentId)?.status === "failed")
87 .map((entry) => ({ ...entry, unavailable: null })),
88 changes: new Map(targets.map((target) => [target.environmentId, [change]])),
89 write: retry,
90 onResult: (id, result) => {
91 results.set(id, result);
92 },
93 });
94 expect(retry.mock.calls).toHaveLength(2);
95 expect(retry).not.toHaveBeenCalledWith(expect.objectContaining({ environmentId: "fast" }));
96 expect([...results.values()].every((result) => result.status === "saved")).toBe(true);
97 });
98​
99 it("sends mappings beside prices and fails servers that cannot store them", async () => {
100 const write = vi.fn(async () => ({ _tag: "Success" as const }));
101 const onResult = vi.fn();
102 const changes = [
103 { model: "example", alias: "example-model" },
104 { model: "example", price: null },
105 ];
106 await writeUsagePrices({
107 targets: [target("current"), target("old", { aliases: null })],
108 changes: new Map([
109 [EnvironmentId.make("current"), changes],
110 [EnvironmentId.make("old"), changes],
111 ]),
112 write,
113 onResult,
114 });
115 expect(write).toHaveBeenCalledExactlyOnceWith({
116 environmentId: "current",
117 input: {
118 patch: {
119 usagePriceOverrides: { example: null },
120 usageModelAliases: { example: "example-model" },
121 },
122 },
123 });
124 expect(onResult).toHaveBeenCalledWith("old", {
125 status: "failed",
126 error: "Update server to map models",
127 });
128 });
129});