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

useTerminalLifecycle.test.ts

99 lines3.2 KB

1import * as NodeModule from "node:module";
2import { act, createElement, type ReactNode } from "react";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4​
5import { useTerminalLifecycle } from "./useTerminalLifecycle";
6​
7const { createRoot } = NodeModule.createRequire(import.meta.url)("react-dom/client") as {
8 createRoot(container: Element): {
9 render(children: ReactNode): void;
10 unmount(): void;
11 };
12};
13​
14type Input = Parameters<typeof useTerminalLifecycle>[0];
15let root: ReturnType<typeof createRoot>;
16const reopen = vi.fn<Input["reopen"]>();
17const onRunning = vi.fn<Input["onRunning"]>();
18const onExit = vi.fn<Input["onExit"]>();
19​
20function LifecycleProbe(input: Input) {
21 useTerminalLifecycle(input);
22 return null;
24​
25function input(changes: Partial<Input> = {}): Input {
26 return {
27 terminalKey: "environment:thread:term-1",
28 canOperate: true,
29 observing: false,
30 attached: true,
31 terminal: { status: "running", version: 1 },
32 reopen,
33 onRunning,
34 onExit,
35 ...changes,
36 };
38​
39function render(value: Input) {
40 return act(() => root.render(createElement(LifecycleProbe, value)));
42​
43beforeEach(() => {
44 reopen.mockReset().mockResolvedValue(true);
45 onRunning.mockClear();
46 onExit.mockClear();
47 const document = {
48 nodeType: 9,
49 addEventListener() {},
50 removeEventListener() {},
51 };
52 const container = {
53 nodeType: 1,
54 tagName: "DIV",
55 namespaceURI: "http://www.w3.org/1999/xhtml",
56 ownerDocument: document,
57 addEventListener() {},
58 removeEventListener() {},
59 };
60 vi.stubGlobal("document", document);
61 vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
62 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
63 root = createRoot(container as unknown as HTMLElement);
64});
65​
66afterEach(async () => {
67 await act(() => root.unmount());
68 vi.unstubAllGlobals();
69});
70​
71it("does not respawn a terminal that exited after operate was revoked", async () => {
72 await render(input());
73 await render(input({ canOperate: false }));
74 await render(input({ canOperate: false, terminal: { status: "exited", version: 2 } }));
75 await render(input({ terminal: { status: "exited", version: 1 } }));
76 expect(reopen).not.toHaveBeenCalled();
77 expect(onExit).not.toHaveBeenCalled();
78});
79​
80it("reopens an ended terminal on an explicit new route visit", async () => {
81 await render(input({ canOperate: false, terminal: { status: "exited", version: 1 } }));
82 await render(input({ terminalKey: "environment:thread:term-2" }));
83 await render(input({ terminal: { status: "exited", version: 1 } }));
84 expect(reopen).toHaveBeenCalledOnce();
85​
86 await render(input({ terminal: { status: "exited", version: 2 } }));
87 expect(reopen).toHaveBeenCalledOnce();
88 await render(input({ terminal: { status: "running", version: 3 } }));
89 await render(input({ terminal: { status: "exited", version: 4 } }));
90 expect(onExit).toHaveBeenCalledOnce();
91});
92​
93it("keeps a detached terminal from being mistaken for a live exit", async () => {
94 await render(input());
95 await render(input({ attached: false, terminal: { status: "closed", version: 0 } }));
96 await render(input({ terminal: { status: "exited", version: 1 } }));
97 expect(onExit).not.toHaveBeenCalled();
98 expect(reopen).toHaveBeenCalledOnce();
99});