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

usagePriceTargets.test.ts

143 lines5.2 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 unavailable: null,
16 ...overrides,
17});
18​
19describe("model price writes", () => {
20 it("sends all table changes in one patch per environment and skips unchanged destinations", async () => {
21 const write = vi.fn(async () => ({ _tag: "Success" as const }));
22 const onResult = vi.fn();
23 await writeUsagePrices({
24 targets: [target("edited"), target("unchanged")],
25 changes: new Map([
26 [
27 EnvironmentId.make("edited"),
28 [
29 { model: "new-model", price },
30 { model: "example", price: null },
31 ],
32 ],
33 ]),
34 write,
35 onResult,
36 });
37 expect(write).toHaveBeenCalledExactlyOnceWith({
38 environmentId: "edited",
39 input: { patch: { usagePriceOverrides: { "new-model": price, example: null } } },
40 });
41 expect(onResult).toHaveBeenCalledWith("unchanged", { status: "saved" });
42 });
43​
44 it("saves independently, skips unavailable targets, and retries failures without rewriting successes", async () => {
45 let resolveSlow!: (result: { _tag: "Success" | "Failure" }) => void;
46 const slow = new Promise<{ _tag: "Success" | "Failure" }>((resolve) => {
47 resolveSlow = resolve;
48 });
49 let resolveFastSaved!: () => void;
50 const fastSaved = new Promise<void>((resolve) => {
51 resolveFastSaved = resolve;
52 });
53 const targets = [target("fast"), target("slow"), target("offline", { unavailable: "Offline" })];
54 const results = new Map<EnvironmentId, UsagePriceWriteResult>();
55 const write = vi.fn(async ({ environmentId }: { environmentId: EnvironmentId }) =>
56 environmentId === "slow" ? slow : { _tag: "Success" as const },
57 );
58 const change = { model: "example", price };
59 const running = writeUsagePrices({
60 targets,
61 changes: new Map(targets.map((target) => [target.environmentId, [change]])),
62 write,
63 onResult: (id, result) => {
64 results.set(id, result);
65 if (id === "fast") resolveFastSaved();
66 },
67 });
68 await fastSaved;
69 expect(results.get(EnvironmentId.make("fast"))).toEqual({ status: "saved" });
70 expect(results.get(EnvironmentId.make("offline"))).toEqual({
71 status: "failed",
72 error: "Offline",
73 });
74 expect(results.has(EnvironmentId.make("slow"))).toBe(false);
75 resolveSlow({ _tag: "Failure" });
76 await running;
77 expect(write).toHaveBeenCalledTimes(2);
78 expect(write).toHaveBeenCalledWith({
79 environmentId: "fast",
80 input: { patch: { usagePriceOverrides: { example: price } } },
81 });
82 const retry = vi.fn(async () => ({ _tag: "Success" as const }));
83 await writeUsagePrices({
84 targets: targets
85 .filter((entry) => results.get(entry.environmentId)?.status === "failed")
86 .map((entry) => ({ ...entry, unavailable: null })),
87 changes: new Map(targets.map((target) => [target.environmentId, [change]])),
88 write: retry,
89 onResult: (id, result) => {
90 results.set(id, result);
91 },
92 });
93 expect(retry.mock.calls).toHaveLength(2);
94 expect(retry).not.toHaveBeenCalledWith(expect.objectContaining({ environmentId: "fast" }));
95 expect([...results.values()].every((result) => result.status === "saved")).toBe(true);
96 });
97​
98 it("resets only the chosen model on the selected destination", async () => {
99 const write = vi.fn(async () => ({ _tag: "Success" as const }));
100 const onResult = vi.fn();
101 await writeUsagePrices({
102 targets: [target("selected")],
103 changes: new Map([[EnvironmentId.make("selected"), [{ model: "example", price: null }]]]),
104 write,
105 onResult,
106 });
107 expect(write).toHaveBeenCalledExactlyOnceWith({
108 environmentId: "selected",
109 input: { patch: { usagePriceOverrides: { example: null } } },
110 });
111 expect(onResult).toHaveBeenCalledWith("selected", { status: "saved" });
112 });
113​
114 it("reports thrown writes and permission/version restrictions without aborting other saves", async () => {
115 const onResult = vi.fn();
116 const write = vi.fn(async () => {
117 throw new Error("connection lost");
118 });
119 await writeUsagePrices({
120 targets: [
121 target("lost"),
122 target("denied", { unavailable: "Read-only access" }),
123 target("old", { unavailable: "Update server to edit prices" }),
124 ],
125 changes: new Map([[EnvironmentId.make("lost"), [{ model: "example", price }]]]),
126 write,
127 onResult,
128 });
129 expect(write).toHaveBeenCalledTimes(1);
130 expect(onResult).toHaveBeenCalledWith("lost", {
131 status: "failed",
132 error: "Could not save. Try again.",
133 });
134 expect(onResult).toHaveBeenCalledWith("denied", {
135 status: "failed",
136 error: "Read-only access",
137 });
138 expect(onResult).toHaveBeenCalledWith("old", {
139 status: "failed",
140 error: "Update server to edit prices",
141 });
142 });
143});