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