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

NativeTerminalSurface.test.tsx

153 lines4.8 KB

1import {
2 applyTerminalAttachStreamEvent,
3 EMPTY_TERMINAL_BUFFER_STATE,
4 terminalOutputText,
5} from "@t3tools/client-runtime/state/terminal";
6import { act, useImperativeHandle, useLayoutEffect, type ReactNode } from "react";
7import { createRoot } from "react-dom/client";
8import type { NativeSyntheticEvent } from "react-native";
9import { expect, it, vi } from "vite-plus/test";
10​
11import type { NativeTerminalSurfaceProps } from "./nativeTerminalModule";
12​
13const mocks = vi.hoisted(() => ({
14 revision: null as number | null,
15 initialBuffers: [] as string[],
16 mounts: vi.fn(),
17 unmounts: vi.fn(),
18 write: vi.fn(async (_data: string, _reset: boolean) => {}),
19}));
20​
21function RawView({ ref: nativeRef, initialBuffer, onResize }: NativeTerminalSurfaceProps) {
22 useImperativeHandle(nativeRef, () => ({ writeOutput: mocks.write }));
23 useLayoutEffect(() => {
24 mocks.mounts();
25 return () => {
26 mocks.unmounts();
27 };
28 }, []);
29 useLayoutEffect(() => {
30 mocks.initialBuffers.push(initialBuffer);
31 }, [initialBuffer]);
32 useLayoutEffect(() => {
33 onResize?.({ nativeEvent: { cols: 80, rows: 24 } } as NativeSyntheticEvent<{
34 cols: number;
35 rows: number;
36 }>);
37 }, [onResize]);
38 return null;
40​
41vi.mock("expo", () => ({
42 requireNativeView: () => RawView,
43 requireOptionalNativeModule: () =>
44 mocks.revision === null ? {} : { outputRevision: mocks.revision },
45}));
46vi.mock("react-native", () => ({ View: ({ children }: { children: ReactNode }) => children }));
47vi.mock("../../components/ui/AppText", () => ({ AppText: () => null }));
48vi.mock("../../lib/typography", () => ({ MOBILE_TYPOGRAPHY: { label: { fontSize: 10 } } }));
49vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
50 useAppearancePreferences: () => ({ themeAppearance: "dark", themeId: "default" }),
51}));
52vi.mock("./terminalTheme", () => ({
53 buildGhosttyThemeConfig: () => "theme",
54 getMobileTerminalTheme: () => ({}),
55}));
56​
57import { TerminalSurface } from "./NativeTerminalSurface";
58​
59function setup(revision: number | null) {
60 mocks.revision = revision;
61 mocks.initialBuffers = [];
62 mocks.write.mockReset();
63 mocks.write.mockResolvedValue(undefined);
64 mocks.mounts.mockClear();
65 mocks.unmounts.mockClear();
66 const document = { nodeType: 9, addEventListener() {}, removeEventListener() {} };
67 const container = {
68 nodeType: 1,
69 tagName: "DIV",
70 namespaceURI: "http://www.w3.org/1999/xhtml",
71 ownerDocument: document,
72 addEventListener() {},
73 removeEventListener() {},
74 };
75 vi.stubGlobal("document", document);
76 vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
77 vi.stubGlobal("expo", { getViewConfig: () => ({}) });
78 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
79 const root = createRoot(container as unknown as HTMLElement);
80 return {
81 async render(
82 state: typeof EMPTY_TERMINAL_BUFFER_STATE,
83 terminalKey = "environment:thread:term-1",
84 ) {
85 await act(async () => {
86 root.render(
87 <TerminalSurface
88 terminalKey={terminalKey}
89 output={state.version === 0 ? null : state.output}
90 buffer={
91 state.version === 0
92 ? null
93 : (revision ?? 0) >= 1
94 ? ""
95 : terminalOutputText(state.output)
96 }
97 isRunning
98 onInput={() => {}}
99 onResize={() => {}}
100 />,
101 );
102 });
103 },
104 async [Symbol.asyncDispose]() {
105 await act(async () => root.unmount());
106 vi.unstubAllGlobals();
107 },
108 };
110​
111it.each([null, 1])(
112 "preserves the native view across rollover and uses legacy history on older binaries (revision %s)",
113 async (revision) => {
114 await using harness = setup(revision);
115 let state = applyTerminalAttachStreamEvent(
116 EMPTY_TERMINAL_BUFFER_STATE,
117 {
118 type: "output",
119 threadId: "thread",
120 terminalId: "term-1",
121 data: "retained",
122 },
123 8,
124 );
125 await harness.render(state);
126 state = applyTerminalAttachStreamEvent(
127 state,
128 {
129 type: "output",
130 threadId: "thread",
131 terminalId: "term-1",
132 data: "-new",
133 },
134 8,
135 );
136 await harness.render(state);
137 await harness.render(EMPTY_TERMINAL_BUFFER_STATE);
138 expect(mocks.mounts).toHaveBeenCalledTimes(1);
139 expect(mocks.unmounts).not.toHaveBeenCalled();
140 if (revision === 1) {
141 expect(mocks.initialBuffers).toEqual([""]);
142 expect(mocks.write.mock.calls).toEqual([
143 ["retained", true],
144 ["-new", false],
145 ]);
146 await harness.render(state, "another-environment:thread:term-1");
147 expect(mocks.write).toHaveBeenLastCalledWith("ined-new", true);
148 } else {
149 expect(mocks.write).not.toHaveBeenCalled();
150 expect(mocks.initialBuffers).toEqual(["retained", "ined-new"]);
151 }
152 },
153);