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

terminalLaunchContext.test.ts

248 lines8.2 KB

1import * as NodeModule from "node:module";
2import { act, createElement, useEffect, type ReactNode } from "react";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { EnvironmentId, ThreadId } from "@t3tools/contracts";
5​
6import {
7 resolvePreferredThreadWorktreePath,
8 resolveTerminalOpenLocation,
9 stagePendingTerminalLaunch,
10 takePendingTerminalLaunch,
11 useInitialTerminalLaunchLocation,
12} from "./terminalLaunchContext";
13​
14describe("resolvePreferredThreadWorktreePath", () => {
15 it("prefers thread detail worktree paths over thread shell paths", () => {
16 expect(
17 resolvePreferredThreadWorktreePath({
18 threadShellWorktreePath: "/repo/root",
19 threadDetailWorktreePath: "/repo/worktrees/feature",
20 }),
21 ).toBe("/repo/worktrees/feature");
22 });
23​
24 it("falls back to the thread shell worktree path when detail is unavailable", () => {
25 expect(
26 resolvePreferredThreadWorktreePath({
27 threadShellWorktreePath: "/repo/worktrees/feature",
28 threadDetailWorktreePath: null,
29 }),
30 ).toBe("/repo/worktrees/feature");
31 });
32});
33​
34describe("resolveTerminalOpenLocation", () => {
35 it("uses the thread detail worktree path before the workspace root for a fresh mobile open", () => {
36 expect(
37 resolveTerminalOpenLocation({
38 terminalLocation: null,
39 activeSessionLocation: null,
40 workspaceRoot: "/repo/root",
41 threadShellWorktreePath: null,
42 threadDetailWorktreePath: "/repo/worktrees/feature",
43 }),
44 ).toEqual({
45 cwd: "/repo/worktrees/feature",
46 worktreePath: "/repo/worktrees/feature",
47 });
48 });
49​
50 it("preserves the running terminal snapshot cwd when attaching to an existing session", () => {
51 expect(
52 resolveTerminalOpenLocation({
53 terminalLocation: null,
54 activeSessionLocation: {
55 cwd: "/repo/worktrees/feature",
56 worktreePath: "/repo/worktrees/feature",
57 },
58 workspaceRoot: "/repo/root",
59 threadShellWorktreePath: null,
60 threadDetailWorktreePath: "/repo/worktrees/other",
61 }),
62 ).toEqual({
63 cwd: "/repo/worktrees/feature",
64 worktreePath: "/repo/worktrees/feature",
65 });
66 });
67​
68 it("preserves checkout cwd and identity from the selected terminal or active session", () => {
69 const checkout = { cwd: "/repo/root/packages/app", worktreePath: null };
70 const threadLocation = {
71 workspaceRoot: "/repo/root",
72 threadShellWorktreePath: "/repo/worktrees/feature",
73 threadDetailWorktreePath: "/repo/worktrees/feature",
74 };
75​
76 expect(
77 resolveTerminalOpenLocation({
78 ...threadLocation,
79 terminalLocation: checkout,
80 activeSessionLocation: {
81 cwd: "/repo/worktrees/other",
82 worktreePath: "/repo/worktrees/other",
83 },
84 }),
85 ).toEqual(checkout);
86​
87 expect(
88 resolveTerminalOpenLocation({
89 ...threadLocation,
90 terminalLocation: null,
91 activeSessionLocation: checkout,
92 }),
93 ).toEqual(checkout);
94 });
95});
96​
97describe("pending terminal launches", () => {
98 it("stages and consumes launch details for a specific terminal target", () => {
99 const target = {
100 environmentId: EnvironmentId.make("env-1"),
101 threadId: ThreadId.make("thread-1"),
102 terminalId: "term-2",
103 };
104​
105 stagePendingTerminalLaunch({
106 target,
107 launch: {
108 cwd: "/repo/worktrees/feature",
109 worktreePath: "/repo/worktrees/feature",
110 env: { FOO: "bar" },
111 initialInput: "pnpm dev\r",
112 },
113 });
114​
115 expect(takePendingTerminalLaunch(target)).toEqual({
116 cwd: "/repo/worktrees/feature",
117 worktreePath: "/repo/worktrees/feature",
118 env: { FOO: "bar" },
119 initialInput: "pnpm dev\r",
120 });
121 expect(takePendingTerminalLaunch(target)).toBeNull();
122 });
123​
124 it("keeps pending launches isolated per terminal target", () => {
125 const primaryTarget = {
126 environmentId: EnvironmentId.make("env-1"),
127 threadId: ThreadId.make("thread-1"),
128 terminalId: "term-2",
129 };
130 const otherTarget = {
131 environmentId: EnvironmentId.make("env-1"),
132 threadId: ThreadId.make("thread-1"),
133 terminalId: "term-3",
134 };
135​
136 stagePendingTerminalLaunch({
137 target: primaryTarget,
138 launch: {
139 cwd: "/repo/root",
140 worktreePath: null,
141 initialInput: "pnpm i\r",
142 },
143 });
144​
145 expect(takePendingTerminalLaunch(otherTarget)).toBeNull();
146 expect(takePendingTerminalLaunch(primaryTarget)).toEqual({
147 cwd: "/repo/root",
148 worktreePath: null,
149 env: undefined,
150 initialInput: "pnpm i\r",
151 });
152 });
153});
154​
155describe("initial terminal launch location", () => {
156 it("waits for session metadata before retaining a location for each route target", async () => {
157 const { createRoot } = NodeModule.createRequire(import.meta.url)("react-dom/client") as {
158 createRoot(container: Element): {
159 render(children: ReactNode): void;
160 unmount(): void;
161 };
162 };
163 const document = { nodeType: 9, addEventListener() {}, removeEventListener() {} };
164 const container = {
165 nodeType: 1,
166 tagName: "DIV",
167 namespaceURI: "http://www.w3.org/1999/xhtml",
168 ownerDocument: document,
169 addEventListener() {},
170 removeEventListener() {},
171 };
172 vi.stubGlobal("document", document);
173 vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
174 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
175 const root = createRoot(container as unknown as HTMLElement);
176 type Input = Parameters<typeof useInitialTerminalLaunchLocation>[0];
177 let location: ReturnType<typeof useInitialTerminalLaunchLocation> = null;
178 function Probe(input: Input) {
179 const selectedLocation = useInitialTerminalLaunchLocation(input);
180 useEffect(() => {
181 location = selectedLocation;
182 }, [selectedLocation]);
183 return null;
184 }
185 const threadLocation = {
186 terminalLocation: null,
187 activeSessionLocation: null,
188 workspaceRoot: "/repo/root",
189 threadShellWorktreePath: "/repo/worktrees/feature",
190 threadDetailWorktreePath: "/repo/worktrees/feature",
191 };
192 const checkout = { cwd: "/repo/root/packages/app", worktreePath: null };
193 const input: Input = {
194 targetKey: "environment:thread:term-1",
195 pendingLaunch: null,
196 hasResolvedPendingLaunch: true,
197 isAuthenticated: true,
198 canRead: true,
199 canOperate: true,
200 hasLoadedSessions: false,
201 location: threadLocation,
202 };
203 const render = (changes: Partial<Input> = {}) =>
204 act(() => root.render(createElement(Probe, { ...input, ...changes })));
205 try {
206 // Thread/project are ready before authentication and the metadata snapshot.
207 await render({ isAuthenticated: false, canRead: false, canOperate: false });
208 expect(location).toBeNull();
209 await render();
210 expect(location).toBeNull();
211 await render({
212 hasLoadedSessions: true,
213 canOperate: false,
214 location: { ...threadLocation, terminalLocation: checkout },
215 });
216 expect(location).toEqual(checkout);
217​
218 // Reconnects and subsequent thread changes must not move the selected terminal.
219 await render({ location: { ...threadLocation, threadDetailWorktreePath: "/repo/other" } });
220 expect(location).toEqual(checkout);
221​
222 const nextTarget = { targetKey: "environment:thread:term-2" };
223 await render(nextTarget);
224 expect(location).toBeNull();
225 // A loaded empty session list permits the fresh-open thread fallback.
226 await render({ ...nextTarget, hasLoadedSessions: true });
227 expect(location).toEqual({
228 cwd: "/repo/worktrees/feature",
229 worktreePath: "/repo/worktrees/feature",
230 });
231​
232 await render({ targetKey: "environment:thread:term-3", hasResolvedPendingLaunch: false });
233 expect(location).toBeNull();
234 await render({ targetKey: "environment:thread:term-3", pendingLaunch: checkout });
235 expect(location).toEqual(checkout);
236​
237 // Operate-only grants cannot load metadata, so they still use the thread default.
238 await render({ targetKey: "environment:thread:term-4", canRead: false });
239 expect(location).toEqual({
240 cwd: "/repo/worktrees/feature",
241 worktreePath: "/repo/worktrees/feature",
242 });
243 } finally {
244 await act(() => root.unmount());
245 vi.unstubAllGlobals();
246 }
247 });
248});