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

ThreadTerminalDrawer.permissions.browser.test.tsx

156 lines5.3 KB

1import {
2 AuthTerminalOperateScope,
3 EnvironmentId,
4 ThreadId,
5 type AuthEnvironmentScope,
6} from "@t3tools/contracts";
7import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
8import { nextTerminalAttachSeedState } from "@t3tools/client-runtime/state/terminal";
9import { AsyncResult } from "effect/reactivity";
10import { act } from "react";
11import { createRoot, type Root } from "react-dom/client";
12import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
13import type {
14 GhosttyTerminalSurface,
15 GhosttyTerminalSurfaceOptions,
16} from "~/terminal/ghostty/surface";
17​
18const state = vi.hoisted(() => ({
19 allowed: true,
20 listeners: new Set<() => void>(),
21 resize: vi.fn(),
22 otherCommand: vi.fn(),
23 createSurface:
24 vi.fn<
25 (
26 mount: HTMLElement,
27 options: GhosttyTerminalSurfaceOptions,
28 ) => Promise<GhosttyTerminalSurface>
29 >(),
30}));
31​
32vi.mock(import("@effect/atom-react"), async (original) => ({
33 ...(await original()),
34 useAtomValue: (() => null) as never,
35}));
36vi.mock(import("../hooks/useSettings"), async (original) => ({
37 ...(await original()),
38 getClientSettings: () => DEFAULT_CLIENT_SETTINGS,
39 useClientSettings: ((select: (settings: typeof DEFAULT_CLIENT_SETTINGS) => unknown) =>
40 select(DEFAULT_CLIENT_SETTINGS)) as never,
41}));
42vi.mock(import("../editorPreferences"), async (original) => ({
43 ...(await original()),
44 useOpenInPreferredEditor: (() => state.otherCommand) as never,
45}));
46vi.mock(import("../state/terminal"), async (original) => {
47 const actual = await original();
48 return {
49 ...actual,
50 terminalEnvironment: { ...actual.terminalEnvironment, resize: "resize" as never },
51 };
52});
53vi.mock(import("../state/use-atom-command"), async (original) => ({
54 ...(await original()),
55 useAtomCommand: ((command: unknown) =>
56 command === "resize" ? state.resize : state.otherCommand) as never,
57}));
58vi.mock(import("../state/terminalSessions"), async (original) => ({
59 ...(await original()),
60 useAttachedTerminalSession: (() => session) as never,
61}));
62vi.mock(import("~/terminal/ghostty/surface"), async (original) => ({
63 ...(await original()),
64 GhosttyTerminalSurface: { create: state.createSurface } as never,
65}));
66vi.mock(import("../state/session"), async (original) => {
67 const { useSyncExternalStore } = await import("react");
68 const readEnvironmentScope = (id: EnvironmentId | null, scope: AuthEnvironmentScope) =>
69 id !== null &&
70 (scope !== AuthTerminalOperateScope || id !== threadRef.environmentId || state.allowed);
71 return {
72 ...(await original()),
73 readEnvironmentScope,
74 useEnvironmentScope: (id: EnvironmentId | null, scope: AuthEnvironmentScope) =>
75 useSyncExternalStore(
76 (listener) => {
77 state.listeners.add(listener);
78 return () => state.listeners.delete(listener);
79 },
80 () => readEnvironmentScope(id, scope),
81 ),
82 };
83});
84​
85import { TerminalViewport } from "./ThreadTerminalDrawer";
86​
87const threadRef = {
88 environmentId: EnvironmentId.make("secondary-terminal"),
89 threadId: ThreadId.make("thread"),
90};
91const session = { ...nextTerminalAttachSeedState(), status: "running" as const, version: 1 };
92let root: Root;
93let container: HTMLDivElement;
94​
95beforeEach(() => {
96 state.allowed = true;
97 state.listeners.clear();
98 state.resize.mockReset().mockResolvedValue(AsyncResult.success(undefined));
99 state.otherCommand.mockReset().mockResolvedValue(AsyncResult.success(undefined));
100 // A surface can report its initial grid while asynchronous WASM setup is
101 // pending. Keep that unrelated setup pending while exercising its callback.
102 state.createSurface.mockReset().mockReturnValue(new Promise(() => undefined));
103 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
104 container = document.createElement("div");
105 document.body.append(container);
106 root = createRoot(container);
107});
108​
109afterEach(async () => {
110 await act(async () => root.unmount());
111 container.remove();
112 vi.unstubAllGlobals();
113});
114​
115it("rechecks the target terminal grant when a retained surface callback reports a resize", async () => {
116 await act(async () =>
117 root.render(
118 <TerminalViewport
119 advancedTypography={false}
120 threadRef={threadRef}
121 threadId={threadRef.threadId}
122 terminalId="terminal-1"
123 terminalLabel="Terminal 1"
124 cwd="/repo"
125 onSessionExited={state.otherCommand}
126 focusRequestId={0}
127 autoFocus={false}
128 visible={true}
129 resizeEpoch={0}
130 drawerHeight={200}
131 keybindings={[]}
132 />,
133 ),
134 );
135 expect(state.createSurface).toHaveBeenCalledOnce();
136 const onResize = state.createSurface.mock.calls[0]![1].onResize;
137 if (!onResize) throw new Error("The terminal did not register its resize callback.");
138​
139 onResize(80, 24);
140 expect(state.resize).toHaveBeenCalledExactlyOnceWith({
141 environmentId: threadRef.environmentId,
142 input: { threadId: threadRef.threadId, terminalId: "terminal-1", cols: 80, rows: 24 },
143 });
144​
145 state.allowed = false;
146 onResize(120, 40);
147 expect(state.resize).toHaveBeenCalledTimes(1);
148​
149 state.allowed = true;
150 onResize(100, 30);
151 expect(state.resize).toHaveBeenCalledTimes(2);
152 expect(state.resize).toHaveBeenLastCalledWith({
153 environmentId: threadRef.environmentId,
154 input: { threadId: threadRef.threadId, terminalId: "terminal-1", cols: 100, rows: 30 },
155 });
156});