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

UsagePriceOverrides.browser.test.tsx

134 lines4.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3​
4import { render } from "../../../test/render";
5​
6const ENVIRONMENT_ID = vi.hoisted(() => "workstation");
7​
8vi.mock(import("~/state/presentation"), async (original) => {
9 const { Atom } = await import("effect/reactivity");
10 const actual = await original();
11 const presentation = {
12 entry: { target: { label: "workstation" } },
13 connection: { phase: "connected" },
14 serverConfig: {
15 environment: { capabilities: { usagePriceOverrides: true, usageModelAliases: true } },
16 },
17 };
18 return {
19 ...actual,
20 environmentPresentations: {
21 ...actual.environmentPresentations,
22 presentationsAtom: Atom.make(new Map([[ENVIRONMENT_ID, presentation]])) as never,
23 },
24 };
25});
26vi.mock(import("~/state/server"), async (original) => {
27 const { Atom } = await import("effect/reactivity");
28 const actual = await original();
29 const settings = Atom.make({
30 usagePriceOverrides: {
31 "priced-model": { inputCostPerMillionTokens: 1, outputCostPerMillionTokens: 4 },
32 },
33 usageModelAliases: { "my-preview": "gpt-4o" },
34 });
35 return {
36 ...actual,
37 serverEnvironment: {
38 ...actual.serverEnvironment,
39 settingsValueAtom: (() => settings) as never,
40 },
41 };
42});
43vi.mock(import("~/state/session"), async (original) => {
44 const { AsyncResult, Atom } = await import("effect/reactivity");
45 const actual = await original();
46 const session = Atom.make(
47 AsyncResult.success({ authenticated: true, scopes: ["settings:write"] }),
48 );
49 return {
50 ...actual,
51 environmentSession: {
52 ...actual.environmentSession,
53 sessionStateAtom: (() => session) as never,
54 },
55 };
56});
57vi.mock(import("~/state/use-atom-command"), async (original) => ({
58 ...(await original()),
59 useAtomCommand: (() => vi.fn()) as never,
60}));
61​
62import { UsagePriceOverrides } from "./UsagePriceOverrides";
63​
64const renderPrices = (initialModel?: string) =>
65 render(
66 <UsagePriceOverrides
67 usage={[]}
68 initialSelectedEnvironmentIds={null}
69 initialModel={initialModel}
70 onOpenChange={() => {}}
71 />,
72 );
73​
74describe("UsagePriceOverrides", () => {
75 it("waits for a price before flagging an unpriced model's new row", async () => {
76 await using _screen = await renderPrices("glm-5.3");
77 const modelId = page.getByLabelText("New model ID");
78 await expect.element(modelId).toHaveValue("glm-5.3");
79​
80 expect(page.getByRole("alert").elements()).toEqual([]);
81 await expect.element(modelId).not.toHaveAttribute("aria-invalid");
82 await expect.element(page.getByRole("button", { name: "Save changes" })).toBeDisabled();
83​
84 await userEvent.type(page.getByLabelText("Input price for glm-5.3"), "2");
85 await expect
86 .element(page.getByRole("alert"))
87 .toHaveTextContent("Output is required on workstation.");
88 await expect.element(page.getByRole("button", { name: "Save changes" })).toBeDisabled();
89 });
90​
91 it("asks for a model ID once a new row maps to a model", async () => {
92 await using _screen = await renderPrices();
93 await page.getByRole("button", { name: "Add model price" }).click();
94 const modelId = page.getByLabelText("New model ID");
95 await expect.element(modelId).toHaveValue("");
96​
97 expect(page.getByRole("alert").elements()).toEqual([]);
98 await expect.element(modelId).not.toHaveAttribute("aria-invalid");
99​
100 await userEvent.type(page.getByLabelText("Map new model to model"), "gpt-4o");
101 await expect.element(page.getByRole("alert")).toHaveTextContent("Enter a model ID.");
102 await expect.element(modelId).toHaveAttribute("aria-invalid", "true");
103 await expect.element(page.getByRole("button", { name: "Save changes" })).toBeDisabled();
104 });
105​
106 it("leaves no pending change when a mapping goes back to its saved model", async () => {
107 await using _screen = await renderPrices();
108 const mapTo = page.getByLabelText("Map my-preview to model");
109 await userEvent.clear(mapTo);
110 await userEvent.type(page.getByLabelText("Input price for my-preview"), "2");
111 await userEvent.type(mapTo, "gpt-4o");
112​
113 await expect
114 .element(page.getByRole("button", { name: "Discard changes" }))
115 .not.toBeInTheDocument();
116 expect(page.getByRole("alert").elements()).toEqual([]);
117 await expect
118 .element(page.getByRole("button", { name: "Apply to All environments" }))
119 .toBeEnabled();
120 });
121​
122 it("keeps typed prices when a priced model's new mapping is cleared", async () => {
123 await using _screen = await renderPrices();
124 const input = page.getByLabelText("Input price for priced-model");
125 await userEvent.clear(input);
126 await userEvent.type(input, "2");
127 const mapTo = page.getByLabelText("Map priced-model to model");
128 await userEvent.type(mapTo, "gpt-4o");
129 await userEvent.clear(mapTo);
130​
131 await expect.element(input).toHaveValue("2");
132 await expect.element(page.getByRole("button", { name: "Save changes" })).toBeEnabled();
133 });
134});