|
| 1 | +import { |
| 2 | + AuthTerminalOperateScope, |
| 3 | + EnvironmentId, |
| 4 | + ThreadId, |
| 5 | + type AuthEnvironmentScope, |
| 6 | +} from "@t3tools/contracts"; |
| 7 | +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
| 8 | +import { nextTerminalAttachSeedState } from "@t3tools/client-runtime/state/terminal"; |
| 9 | +import { AsyncResult } from "effect/reactivity"; |
| 10 | +import { act, create, type ReactTestRenderer } from "react-test-renderer"; |
| 11 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 12 | +import type { |
| 13 | + GhosttyTerminalSurface, |
| 14 | + GhosttyTerminalSurfaceOptions, |
| 15 | +} from "~/terminal/ghostty/surface"; |
| 16 | +import { terminalAttachmentId } from "../terminalAttachmentIdentity"; |
| 17 | + |
| 18 | +const 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 | + |
| 32 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
| 33 | +vi.mock("../hooks/useSettings", () => ({ |
| 34 | + getClientSettings: () => DEFAULT_CLIENT_SETTINGS, |
| 35 | + useClientSettings: (select: (settings: typeof DEFAULT_CLIENT_SETTINGS) => unknown) => |
| 36 | + select(DEFAULT_CLIENT_SETTINGS), |
| 37 | +})); |
| 38 | +vi.mock("../editorPreferences", () => ({ useOpenInPreferredEditor: () => state.otherCommand })); |
| 39 | +vi.mock("../localApi", () => ({ readLocalApi: () => null })); |
| 40 | +vi.mock("../state/server", () => ({ |
| 41 | + serverEnvironment: { configValueAtom: () => "config" }, |
| 42 | +})); |
| 43 | +vi.mock("../state/preview", () => ({ previewEnvironment: { open: "open" } })); |
| 44 | +vi.mock("../state/terminal", () => ({ terminalEnvironment: { resize: "resize", write: "write" } })); |
| 45 | +vi.mock("../state/use-atom-command", () => ({ |
| 46 | + useAtomCommand: (command: string) => (command === "resize" ? state.resize : state.otherCommand), |
| 47 | +})); |
| 48 | +vi.mock("../state/terminalSessions", () => ({ |
| 49 | + useAttachedTerminalSession: () => session, |
| 50 | +})); |
| 51 | +vi.mock("~/terminal/ghostty/surface", () => ({ |
| 52 | + GhosttyTerminalSurface: { create: state.createSurface }, |
| 53 | +})); |
| 54 | +vi.mock("../state/session", async () => { |
| 55 | + const { useSyncExternalStore } = await import("react"); |
| 56 | + const readEnvironmentScope = (id: EnvironmentId | null, scope: AuthEnvironmentScope) => |
| 57 | + id !== null && |
| 58 | + (scope !== AuthTerminalOperateScope || id !== threadRef.environmentId || state.allowed); |
| 59 | + return { |
| 60 | + readEnvironmentScope, |
| 61 | + useEnvironmentScope: (id: EnvironmentId | null, scope: AuthEnvironmentScope) => |
| 62 | + useSyncExternalStore( |
| 63 | + (listener) => { |
| 64 | + state.listeners.add(listener); |
| 65 | + return () => state.listeners.delete(listener); |
| 66 | + }, |
| 67 | + () => readEnvironmentScope(id, scope), |
| 68 | + ), |
| 69 | + }; |
| 70 | +}); |
| 71 | + |
| 72 | +import { TerminalViewport } from "./ThreadTerminalDrawer"; |
| 73 | + |
| 74 | +const threadRef = { |
| 75 | + environmentId: EnvironmentId.make("secondary-terminal"), |
| 76 | + threadId: ThreadId.make("thread"), |
| 77 | +}; |
| 78 | +const session = { ...nextTerminalAttachSeedState(), status: "running" as const, version: 1 }; |
| 79 | +let renderer: ReactTestRenderer | undefined; |
| 80 | + |
| 81 | +beforeEach(() => { |
| 82 | + state.allowed = true; |
| 83 | + state.listeners.clear(); |
| 84 | + state.resize.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
| 85 | + state.otherCommand.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
| 86 | + // A surface can report its initial grid while asynchronous WASM setup is |
| 87 | + // pending. Keep that unrelated setup pending while exercising its callback. |
| 88 | + state.createSurface.mockReset().mockReturnValue(new Promise(() => undefined)); |
| 89 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 90 | + vi.stubGlobal("document", { |
| 91 | + body: {}, |
| 92 | + documentElement: { classList: { contains: () => false } }, |
| 93 | + querySelector: () => null, |
| 94 | + createElement: () => ({ getContext: () => null }), |
| 95 | + }); |
| 96 | + vi.stubGlobal("getComputedStyle", () => ({ |
| 97 | + colorScheme: "light", |
| 98 | + backgroundColor: "", |
| 99 | + color: "", |
| 100 | + getPropertyValue: () => "", |
| 101 | + })); |
| 102 | +}); |
| 103 | + |
| 104 | +afterEach(async () => { |
| 105 | + await act(async () => renderer?.unmount()); |
| 106 | + renderer = undefined; |
| 107 | + vi.unstubAllGlobals(); |
| 108 | +}); |
| 109 | + |
| 110 | +it("sends the attachment id with a resize that passes the target terminal grant recheck", async () => { |
| 111 | + await act(async () => { |
| 112 | + renderer = create( |
| 113 | + <TerminalViewport |
| 114 | + advancedTypography={false} |
| 115 | + threadRef={threadRef} |
| 116 | + threadId={threadRef.threadId} |
| 117 | + terminalId="terminal-1" |
| 118 | + terminalLabel="Terminal 1" |
| 119 | + cwd="/repo" |
| 120 | + onSessionExited={state.otherCommand} |
| 121 | + focusRequestId={0} |
| 122 | + autoFocus={false} |
| 123 | + visible={true} |
| 124 | + resizeEpoch={0} |
| 125 | + drawerHeight={200} |
| 126 | + keybindings={[]} |
| 127 | + attached |
| 128 | + restoreFocusOnReattach |
| 129 | + />, |
| 130 | + { createNodeMock: () => ({ closest: () => null, contains: () => false }) }, |
| 131 | + ); |
| 132 | + }); |
| 133 | + expect(state.createSurface).toHaveBeenCalledOnce(); |
| 134 | + const onResize = state.createSurface.mock.calls[0]![1].onResize; |
| 135 | + if (!onResize) throw new Error("The terminal did not register its resize callback."); |
| 136 | + |
| 137 | + onResize(80, 24); |
| 138 | + expect(state.resize).toHaveBeenCalledExactlyOnceWith({ |
| 139 | + environmentId: threadRef.environmentId, |
| 140 | + input: { |
| 141 | + threadId: threadRef.threadId, |
| 142 | + terminalId: "terminal-1", |
| 143 | + attachmentId: terminalAttachmentId("terminal-1"), |
| 144 | + cols: 80, |
| 145 | + rows: 24, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + // The connection updates before React commits its external-store update. |
| 150 | + // Other environments retain access; only this terminal's target is revoked. |
| 151 | + state.allowed = false; |
| 152 | + onResize(120, 40); |
| 153 | + expect(state.resize).toHaveBeenCalledTimes(1); |
| 154 | + |
| 155 | + state.allowed = true; |
| 156 | + onResize(100, 30); |
| 157 | + expect(state.resize).toHaveBeenCalledTimes(2); |
| 158 | + expect(state.resize).toHaveBeenLastCalledWith({ |
| 159 | + environmentId: threadRef.environmentId, |
| 160 | + input: { |
| 161 | + threadId: threadRef.threadId, |
| 162 | + terminalId: "terminal-1", |
| 163 | + attachmentId: terminalAttachmentId("terminal-1"), |
| 164 | + cols: 100, |
| 165 | + rows: 30, |
| 166 | + }, |
| 167 | + }); |
| 168 | +}); |
0 commit comments