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

ThreadDetailsMachineLoad.browser.test.tsx

87 lines3.2 KB

1import { EnvironmentId, type HostResourcesSnapshot } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import { render } from "../../../test/render";
5import { ThreadDetailsMachineLoad } from "./ThreadDetailsMachineLoad";
6​
7const environmentId = EnvironmentId.make("studio");
8const state = vi.hoisted(() => ({
9 phase: "connected",
10 sample: null as unknown,
11 presentationsAtom: Symbol("presentationsAtom"),
12 resourcesAtom: Symbol("resourcesAtom"),
13}));
14vi.mock("../../state/presentation", async (importOriginal) => {
15 const actual = await importOriginal<typeof import("../../state/presentation")>();
16 return {
17 ...actual,
18 environmentPresentations: {
19 ...actual.environmentPresentations,
20 presentationsAtom: state.presentationsAtom,
21 },
22 };
23});
24vi.mock("../../state/server", async (importOriginal) => {
25 const actual = await importOriginal<typeof import("../../state/server")>();
26 return {
27 ...actual,
28 serverEnvironment: {
29 ...actual.serverEnvironment,
30 liveHostResources: () => state.resourcesAtom,
31 },
32 };
33});
34vi.mock("../../state/entities", async (importOriginal) => ({
35 ...(await importOriginal<typeof import("../../state/entities")>()),
36 useServerConfigs: () => new Map(),
37}));
38vi.mock("@effect/atom-react", async (importOriginal) => {
39 const actual = await importOriginal<typeof import("@effect/atom-react")>();
40 return {
41 ...actual,
42 useAtomValue: (atom: unknown, select?: (value: unknown) => unknown) => {
43 if (atom === state.resourcesAtom) {
44 return state.sample === null
45 ? { _tag: "Initial", waiting: true }
46 : { _tag: "Success", value: state.sample, waiting: false };
47 }
48 if (atom === state.presentationsAtom) {
49 const presentations = new Map([[environmentId, { connection: { phase: state.phase } }]]);
50 return select ? select(presentations) : presentations;
51 }
52 return (actual.useAtomValue as (...args: ReadonlyArray<unknown>) => unknown)(atom, select);
53 },
54 };
55});
56​
57const GiB = 1024 ** 3;
58const sample = (cpuUtilization: number) =>
59 ({
60 sampledAt: 1,
61 cpuUtilization,
62 cpuCount: 8,
63 totalMemoryBytes: 16 * GiB,
64 availableMemoryBytes: 8 * GiB,
65 }) as HostResourcesSnapshot;
66​
67describe("ThreadDetailsMachineLoad", () => {
68 it("hides the sample held across a reconnect until a fresh one arrives", async () => {
69 state.phase = "connected";
70 state.sample = sample(0.42);
71 await using screen = await render(<ThreadDetailsMachineLoad environmentId={environmentId} />);
72 expect(screen.container.textContent).toContain("42%");
73​
74 state.phase = "backoff";
75 await screen.rerender(<ThreadDetailsMachineLoad environmentId={environmentId} />);
76 expect(screen.container.textContent).toBe("");
77​
78 // Reconnected, but the refresh has not answered yet: the query still holds the old sample.
79 state.phase = "connected";
80 await screen.rerender(<ThreadDetailsMachineLoad environmentId={environmentId} />);
81 expect(screen.container.textContent).toBe("");
82​
83 state.sample = sample(0.17);
84 await screen.rerender(<ThreadDetailsMachineLoad environmentId={environmentId} />);
85 expect(screen.container.textContent).toContain("17%");
86 });
87});