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

useTerminalGridSync.test.ts

107 lines3.4 KB

1import * as NodeModule from "node:module";
2import { EMPTY_TERMINAL_BUFFER_STATE } from "@t3tools/client-runtime/state/terminal";
3import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import { act, createElement, type ReactNode } from "react";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6​
7import { useTerminalGridSync } from "./useTerminalGridSync";
8​
9const access = vi.hoisted(() => ({ environments: new Set<string>() }));
10vi.mock("../../state/session", () => ({
11 readEnvironmentScope: (environmentId: string, scope: string) =>
12 scope === "terminal:operate" && access.environments.has(environmentId),
13}));
14​
15// Mobile already depends on ReactDOM but does not install its browser type declarations.
16const { createRoot } = NodeModule.createRequire(import.meta.url)("react-dom/client") as {
17 createRoot(container: Element): {
18 render(children: ReactNode): void;
19 unmount(): void;
20 };
21};
22​
23type Input = Parameters<typeof useTerminalGridSync>[0];
24let root: ReturnType<typeof createRoot>;
25const resize = vi.fn<Input["resize"]>();
26​
27function GridProbe(input: Input) {
28 useTerminalGridSync(input);
29 return null;
31​
32function session({
33 generation = 1,
34 version = 1,
35 status = "running",
36}: {
37 readonly generation?: number;
38 readonly version?: number;
39 readonly status?: Input["terminal"]["status"];
40} = {}): Input["terminal"] {
41 return { output: { ...EMPTY_TERMINAL_BUFFER_STATE.output, generation }, version, status };
43​
44function input(): Input {
45 return {
46 environmentId: EnvironmentId.make("environment"),
47 threadId: ThreadId.make("thread"),
48 terminalId: "term-1",
49 canOperate: true,
50 terminal: session(),
51 size: { cols: 80, rows: 24 },
52 resize,
53 };
55​
56function render(value: Input) {
57 return act(() => root.render(createElement(GridProbe, value)));
59​
60beforeEach(() => {
61 resize.mockClear();
62 access.environments.clear();
63 access.environments.add("environment");
64 // The probe renders no DOM, but ReactDOM needs an event target to run real effects.
65 const document = {
66 nodeType: 9,
67 addEventListener() {},
68 removeEventListener() {},
69 };
70 const container = {
71 nodeType: 1,
72 tagName: "DIV",
73 namespaceURI: "http://www.w3.org/1999/xhtml",
74 ownerDocument: document,
75 addEventListener() {},
76 removeEventListener() {},
77 };
78 vi.stubGlobal("document", document);
79 vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
80 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
81 root = createRoot(container as unknown as HTMLElement);
82});
83​
84afterEach(async () => {
85 await act(() => root.unmount());
86 vi.unstubAllGlobals();
87});
88​
89it("replays an unchanged grid after reconnecting with a new attachment generation", async () => {
90 const current = { ...input(), size: { cols: 120, rows: 40 } };
91 await render(current);
92 expect(resize).toHaveBeenCalledOnce();
93​
94 await render({ ...current, terminal: session({ generation: 2, version: 0 }) });
95 expect(resize).toHaveBeenCalledOnce();
96​
97 await render({ ...current, terminal: session({ generation: 2 }) });
98 expect(resize).toHaveBeenCalledTimes(2);
99 expect(resize).toHaveBeenLastCalledWith({
100 environmentId: "environment",
101 input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 40 },
102 });
103​
104 // React may observe the next snapshot without rendering the empty seed first.
105 await render({ ...current, terminal: session({ generation: 3 }) });
106 expect(resize).toHaveBeenCalledTimes(3);
107});