|
| 1 | +import * as NodeModule from "node:module"; |
| 2 | +import { EMPTY_TERMINAL_BUFFER_STATE } from "@t3tools/client-runtime/state/terminal"; |
| 3 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import { act, createElement, type ReactNode } from "react"; |
| 5 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +import { useTerminalGridSync } from "./useTerminalGridSync"; |
| 8 | + |
| 9 | +// Mobile already depends on ReactDOM but does not install its browser type declarations. |
| 10 | +const { createRoot } = NodeModule.createRequire(import.meta.url)("react-dom/client") as { |
| 11 | + createRoot(container: Element): { |
| 12 | + render(children: ReactNode): void; |
| 13 | + unmount(): void; |
| 14 | + }; |
| 15 | +}; |
| 16 | + |
| 17 | +type Input = Parameters<typeof useTerminalGridSync>[0]; |
| 18 | +let root: ReturnType<typeof createRoot>; |
| 19 | +const resize = vi.fn<Input["resize"]>(); |
| 20 | + |
| 21 | +function GridProbe(input: Input) { |
| 22 | + useTerminalGridSync(input); |
| 23 | + return null; |
| 24 | +} |
| 25 | + |
| 26 | +function session({ |
| 27 | + generation = 1, |
| 28 | + version = 1, |
| 29 | + status = "running", |
| 30 | +}: { |
| 31 | + readonly generation?: number; |
| 32 | + readonly version?: number; |
| 33 | + readonly status?: Input["terminal"]["status"]; |
| 34 | +} = {}): Input["terminal"] { |
| 35 | + return { output: { ...EMPTY_TERMINAL_BUFFER_STATE.output, generation }, version, status }; |
| 36 | +} |
| 37 | + |
| 38 | +function input(): Input { |
| 39 | + return { |
| 40 | + environmentId: EnvironmentId.make("environment"), |
| 41 | + threadId: ThreadId.make("thread"), |
| 42 | + terminalId: "term-1", |
| 43 | + canOperate: true, |
| 44 | + terminal: session(), |
| 45 | + size: { cols: 80, rows: 24 }, |
| 46 | + resize, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function render(value: Input) { |
| 51 | + return act(() => root.render(createElement(GridProbe, value))); |
| 52 | +} |
| 53 | + |
| 54 | +beforeEach(() => { |
| 55 | + resize.mockClear(); |
| 56 | + // The probe renders no DOM, but ReactDOM needs an event target to run real effects. |
| 57 | + const document = { |
| 58 | + nodeType: 9, |
| 59 | + addEventListener() {}, |
| 60 | + removeEventListener() {}, |
| 61 | + }; |
| 62 | + const container = { |
| 63 | + nodeType: 1, |
| 64 | + tagName: "DIV", |
| 65 | + namespaceURI: "http://www.w3.org/1999/xhtml", |
| 66 | + ownerDocument: document, |
| 67 | + addEventListener() {}, |
| 68 | + removeEventListener() {}, |
| 69 | + }; |
| 70 | + vi.stubGlobal("document", document); |
| 71 | + vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget }); |
| 72 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 73 | + root = createRoot(container as unknown as HTMLElement); |
| 74 | +}); |
| 75 | + |
| 76 | +afterEach(async () => { |
| 77 | + await act(() => root.unmount()); |
| 78 | + vi.unstubAllGlobals(); |
| 79 | +}); |
| 80 | + |
| 81 | +it("replays an observer's measured grid once its writable attachment snapshot arrives", async () => { |
| 82 | + let current = { ...input(), canOperate: false }; |
| 83 | + await render(current); |
| 84 | + current = { ...current, size: { cols: 120, rows: 40 } }; |
| 85 | + await render(current); |
| 86 | + expect(resize).not.toHaveBeenCalled(); |
| 87 | + |
| 88 | + current = { |
| 89 | + ...current, |
| 90 | + canOperate: true, |
| 91 | + terminal: session({ generation: 2, version: 0 }), |
| 92 | + }; |
| 93 | + await render(current); |
| 94 | + expect(resize).not.toHaveBeenCalled(); |
| 95 | + |
| 96 | + current = { ...current, terminal: session({ generation: 2 }) }; |
| 97 | + await render(current); |
| 98 | + expect(resize).toHaveBeenCalledExactlyOnceWith({ |
| 99 | + environmentId: "environment", |
| 100 | + input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 40 }, |
| 101 | + }); |
| 102 | + |
| 103 | + await render({ ...current, terminal: session({ generation: 2, version: 3 }) }); |
| 104 | + await render({ ...current, size: { ...current.size } }); |
| 105 | + expect(resize).toHaveBeenCalledOnce(); |
| 106 | + |
| 107 | + await render({ ...current, size: { cols: 120, rows: 41 } }); |
| 108 | + expect(resize).toHaveBeenCalledTimes(2); |
| 109 | + expect(resize).toHaveBeenLastCalledWith({ |
| 110 | + environmentId: "environment", |
| 111 | + input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 41 }, |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +it("replays an unchanged grid after reconnecting with a new attachment generation", async () => { |
| 116 | + const current = { ...input(), size: { cols: 120, rows: 40 } }; |
| 117 | + await render(current); |
| 118 | + expect(resize).toHaveBeenCalledOnce(); |
| 119 | + |
| 120 | + await render({ ...current, terminal: session({ generation: 2, version: 0 }) }); |
| 121 | + expect(resize).toHaveBeenCalledOnce(); |
| 122 | + |
| 123 | + await render({ ...current, terminal: session({ generation: 2 }) }); |
| 124 | + expect(resize).toHaveBeenCalledTimes(2); |
| 125 | + expect(resize).toHaveBeenLastCalledWith({ |
| 126 | + environmentId: "environment", |
| 127 | + input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 40 }, |
| 128 | + }); |
| 129 | + |
| 130 | + // React may observe the next snapshot without rendering the empty seed first. |
| 131 | + await render({ ...current, terminal: session({ generation: 3 }) }); |
| 132 | + expect(resize).toHaveBeenCalledTimes(3); |
| 133 | +}); |
| 134 | + |
| 135 | +it.each([ |
| 136 | + { name: "permission is revoked", change: { canOperate: false } }, |
| 137 | + { name: "the attachment is pending", change: { terminal: session({ version: 0 }) } }, |
| 138 | + { name: "the process has exited", change: { terminal: session({ status: "exited" }) } }, |
| 139 | +])("retains measurements without resizing while $name", async ({ change }) => { |
| 140 | + const current = input(); |
| 141 | + await render(current); |
| 142 | + expect(resize).toHaveBeenCalledOnce(); |
| 143 | + |
| 144 | + await render({ ...current, ...change, size: { cols: 120, rows: 40 } }); |
| 145 | + await render({ ...current, ...change, size: { cols: 120, rows: 41 } }); |
| 146 | + expect(resize).toHaveBeenCalledOnce(); |
| 147 | + |
| 148 | + await render({ ...current, size: { cols: 120, rows: 41 } }); |
| 149 | + expect(resize).toHaveBeenCalledTimes(2); |
| 150 | + expect(resize).toHaveBeenLastCalledWith({ |
| 151 | + environmentId: "environment", |
| 152 | + input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 41 }, |
| 153 | + }); |
| 154 | +}); |
0 commit comments