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

shell.test.ts

470 lines14.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 extractPathFromShellOutput,
5 isCommandAvailable,
6 listLoginShellCandidates,
7 mergePathEntries,
8 mergePathValues,
9 readEnvironmentFromLoginShell,
10 readEnvironmentFromWindowsShell,
11 readPathFromLaunchctl,
12 readPathFromLoginShell,
13 resolveCommandPath,
14 resolveKnownWindowsCliDirs,
15 resolveWindowsEnvironment,
16} from "./shell.ts";
17​
18describe("extractPathFromShellOutput", () => {
19 it("extracts the path between capture markers", () => {
20 expect(
21 extractPathFromShellOutput(
22 "__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n",
23 ),
24 ).toBe("/opt/homebrew/bin:/usr/bin");
25 });
26​
27 it("ignores shell startup noise around the capture markers", () => {
28 expect(
29 extractPathFromShellOutput(
30 "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n",
31 ),
32 ).toBe("/opt/homebrew/bin:/usr/bin");
33 });
34​
35 it("returns null when the markers are missing", () => {
36 expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull();
37 });
38});
39​
40describe("readPathFromLoginShell", () => {
41 it("uses a shell-agnostic printenv PATH probe", () => {
42 const execFile = vi.fn<
43 (
44 file: string,
45 args: ReadonlyArray<string>,
46 options: { encoding: "utf8"; timeout: number },
47 ) => string
48 >(() => "__T3CODE_ENV_PATH_START__\n/a:/b\n__T3CODE_ENV_PATH_END__\n");
49​
50 expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b");
51 expect(execFile).toHaveBeenCalledTimes(1);
52​
53 const firstCall = execFile.mock.calls[0] as
54 | [string, ReadonlyArray<string>, { encoding: "utf8"; timeout: number }]
55 | undefined;
56 expect(firstCall).toBeDefined();
57 if (!firstCall) {
58 throw new Error("Expected execFile to be called");
59 }
60​
61 const [shell, args, options] = firstCall;
62 expect(shell).toBe("/opt/homebrew/bin/fish");
63 expect(args).toHaveLength(2);
64 expect(args?.[0]).toBe("-ilc");
65 expect(args?.[1]).toContain("printenv PATH || true");
66 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_START__");
67 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_END__");
68 expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
69 });
70});
71​
72describe("readPathFromLaunchctl", () => {
73 it("returns a trimmed PATH value from launchctl", () => {
74 const execFile = vi.fn<
75 (
76 file: string,
77 args: ReadonlyArray<string>,
78 options: { encoding: "utf8"; timeout: number },
79 ) => string
80 >(() => " /opt/homebrew/bin:/usr/bin \n");
81​
82 expect(readPathFromLaunchctl(execFile)).toBe("/opt/homebrew/bin:/usr/bin");
83 expect(execFile).toHaveBeenCalledWith("/bin/launchctl", ["getenv", "PATH"], {
84 encoding: "utf8",
85 timeout: 2000,
86 });
87 });
88​
89 it("returns undefined when launchctl is unavailable", () => {
90 const execFile = vi.fn<
91 (
92 file: string,
93 args: ReadonlyArray<string>,
94 options: { encoding: "utf8"; timeout: number },
95 ) => string
96 >(() => {
97 throw new Error("spawn /bin/launchctl ENOENT");
98 });
99​
100 expect(readPathFromLaunchctl(execFile)).toBeUndefined();
101 });
102});
103​
104describe("readEnvironmentFromLoginShell", () => {
105 it("extracts multiple environment variables from a login shell command", () => {
106 const execFile = vi.fn<
107 (
108 file: string,
109 args: ReadonlyArray<string>,
110 options: { encoding: "utf8"; timeout: number },
111 ) => string
112 >(() =>
113 [
114 "__T3CODE_ENV_PATH_START__",
115 "/a:/b",
116 "__T3CODE_ENV_PATH_END__",
117 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
118 "/tmp/secretive.sock",
119 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
120 ].join("\n"),
121 );
122​
123 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
124 PATH: "/a:/b",
125 SSH_AUTH_SOCK: "/tmp/secretive.sock",
126 });
127 expect(execFile).toHaveBeenCalledTimes(1);
128 });
129​
130 it("omits environment variables that are missing or empty", () => {
131 const execFile = vi.fn<
132 (
133 file: string,
134 args: ReadonlyArray<string>,
135 options: { encoding: "utf8"; timeout: number },
136 ) => string
137 >(() =>
138 [
139 "__T3CODE_ENV_PATH_START__",
140 "/a:/b",
141 "__T3CODE_ENV_PATH_END__",
142 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
143 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
144 ].join("\n"),
145 );
146​
147 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
148 PATH: "/a:/b",
149 });
150 });
151​
152 it("preserves surrounding whitespace in captured values", () => {
153 const execFile = vi.fn<
154 (
155 file: string,
156 args: ReadonlyArray<string>,
157 options: { encoding: "utf8"; timeout: number },
158 ) => string
159 >(() =>
160 ["__T3CODE_ENV_CUSTOM_VAR_START__", " padded value ", "__T3CODE_ENV_CUSTOM_VAR_END__"].join(
161 "\n",
162 ),
163 );
164​
165 expect(readEnvironmentFromLoginShell("/bin/zsh", ["CUSTOM_VAR"], execFile)).toEqual({
166 CUSTOM_VAR: " padded value ",
167 });
168 });
169});
170​
171describe("listLoginShellCandidates", () => {
172 it("returns env shell, user shell, then the platform fallback without duplicates", () => {
173 expect(listLoginShellCandidates("darwin", " /opt/homebrew/bin/nu ", "/bin/zsh")).toEqual([
174 "/opt/homebrew/bin/nu",
175 "/bin/zsh",
176 ]);
177 });
178​
179 it("falls back to the platform default when no shells are available", () => {
180 expect(listLoginShellCandidates("linux", undefined, "")).toEqual(["/bin/bash"]);
181 });
182});
183​
184describe("mergePathEntries", () => {
185 it("prefers login-shell PATH entries and keeps inherited extras", () => {
186 expect(
187 mergePathEntries("/opt/homebrew/bin:/usr/bin", "/Users/test/.local/bin:/usr/bin", "darwin"),
188 ).toBe("/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
189 });
190​
191 it("uses the platform-specific delimiter", () => {
192 expect(mergePathEntries("C:\\Tools;C:\\Windows", "C:\\Windows;C:\\Git", "win32")).toBe(
193 "C:\\Tools;C:\\Windows;C:\\Git",
194 );
195 });
196});
197​
198describe("readEnvironmentFromWindowsShell", () => {
199 it("extracts environment variables from a PowerShell command", () => {
200 const execFile = vi.fn<
201 (
202 file: string,
203 args: ReadonlyArray<string>,
204 options: { encoding: "utf8"; timeout: number },
205 ) => string
206 >(
207 () =>
208 "__T3CODE_ENV_PATH_START__\nC:\\Users\\testuser\\AppData\\Roaming\\npm\n__T3CODE_ENV_PATH_END__\n",
209 );
210​
211 expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
212 PATH: "C:\\Users\\testuser\\AppData\\Roaming\\npm",
213 });
214 expect(execFile).toHaveBeenCalledWith(
215 "pwsh.exe",
216 expect.arrayContaining(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]),
217 { encoding: "utf8", timeout: 5000 },
218 );
219 });
220​
221 it("strips CRLF delimiters from captured PowerShell values", () => {
222 const execFile = vi.fn<
223 (
224 file: string,
225 args: ReadonlyArray<string>,
226 options: { encoding: "utf8"; timeout: number },
227 ) => string
228 >(
229 () =>
230 "__T3CODE_ENV_FNM_DIR_START__\r\nC:\\Users\\testuser\\AppData\\Roaming\\fnm\r\n__T3CODE_ENV_FNM_DIR_END__\r\n",
231 );
232​
233 expect(readEnvironmentFromWindowsShell(["FNM_DIR"], execFile)).toEqual({
234 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
235 });
236 });
237​
238 it("omits -NoProfile when loadProfile is enabled", () => {
239 const execFile = vi.fn<
240 (
241 file: string,
242 args: ReadonlyArray<string>,
243 options: { encoding: "utf8"; timeout: number },
244 ) => string
245 >(() => "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n");
246​
247 expect(readEnvironmentFromWindowsShell(["PATH"], { loadProfile: true }, execFile)).toEqual({
248 PATH: "C:\\Tools",
249 });
250 expect(execFile).toHaveBeenCalledWith(
251 "pwsh.exe",
252 expect.arrayContaining(["-NoLogo", "-NonInteractive", "-Command"]),
253 { encoding: "utf8", timeout: 5000 },
254 );
255 expect(execFile.mock.calls[0]?.[1]).not.toContain("-NoProfile");
256 });
257​
258 it("falls back to Windows PowerShell when pwsh.exe is unavailable", () => {
259 const execFile = vi.fn<
260 (
261 file: string,
262 args: ReadonlyArray<string>,
263 options: { encoding: "utf8"; timeout: number },
264 ) => string
265 >((file) => {
266 if (file === "pwsh.exe") {
267 throw new Error("spawn pwsh.exe ENOENT");
268 }
269 return "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n";
270 });
271​
272 expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
273 PATH: "C:\\Tools",
274 });
275 expect(execFile).toHaveBeenNthCalledWith(1, "pwsh.exe", expect.any(Array), {
276 encoding: "utf8",
277 timeout: 5000,
278 });
279 expect(execFile).toHaveBeenNthCalledWith(2, "powershell.exe", expect.any(Array), {
280 encoding: "utf8",
281 timeout: 5000,
282 });
283 });
284});
285​
286describe("mergePathValues", () => {
287 it("dedupes case-insensitively on Windows while preserving preferred order", () => {
288 expect(
289 mergePathValues(
290 'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs"',
291 "c:\\users\\testuser\\appdata\\roaming\\npm;C:\\Windows\\System32",
292 "win32",
293 ),
294 ).toBe(
295 'C:\\Users\\testuser\\AppData\\Roaming\\npm;"C:\\Program Files\\nodejs";C:\\Windows\\System32',
296 );
297 });
298​
299 it("dedupes case-sensitively on POSIX", () => {
300 expect(mergePathValues("/usr/local/bin:/usr/bin", "/usr/bin:/USR/BIN", "linux")).toBe(
301 "/usr/local/bin:/usr/bin:/USR/BIN",
302 );
303 });
304});
305​
306describe("resolveKnownWindowsCliDirs", () => {
307 it("returns known Windows CLI install directories in priority order", () => {
308 expect(
309 resolveKnownWindowsCliDirs({
310 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
311 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
312 USERPROFILE: "C:\\Users\\testuser",
313 }),
314 ).toEqual([
315 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
316 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
317 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
318 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
319 "C:\\Users\\testuser\\.bun\\bin",
320 "C:\\Users\\testuser\\scoop\\shims",
321 ]);
322 });
323});
324​
325describe("isCommandAvailable", () => {
326 it("returns false when PATH is empty", () => {
327 expect(
328 isCommandAvailable("definitely-not-installed", {
329 platform: "win32",
330 env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
331 }),
332 ).toBe(false);
333 });
334});
335​
336describe("resolveCommandPath", () => {
337 it("returns the first executable resolved from PATH", () => {
338 expect(
339 resolveCommandPath("definitely-not-installed", {
340 platform: "win32",
341 env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
342 }),
343 ).toBeNull();
344 });
345});
346​
347describe("resolveWindowsEnvironment", () => {
348 it("returns the baseline no-profile PATH patch when node is already available", () => {
349 const readEnvironment = vi.fn(
350 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) =>
351 options?.loadProfile
352 ? { PATH: "C:\\Profile\\Bin" }
353 : { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" },
354 );
355 const commandAvailable = vi.fn(() => true);
356​
357 expect(
358 resolveWindowsEnvironment(
359 {
360 PATH: "C:\\Windows\\System32",
361 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
362 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
363 USERPROFILE: "C:\\Users\\testuser",
364 },
365 {
366 readEnvironment,
367 commandAvailable,
368 },
369 ),
370 ).toEqual({
371 PATH: [
372 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
373 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
374 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
375 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
376 "C:\\Users\\testuser\\.bun\\bin",
377 "C:\\Users\\testuser\\scoop\\shims",
378 "C:\\Shell\\Bin",
379 "C:\\Windows\\System32",
380 ].join(";"),
381 });
382 expect(readEnvironment).toHaveBeenCalledTimes(1);
383 expect(readEnvironment).toHaveBeenCalledWith(["PATH"], { loadProfile: false });
384 expect(commandAvailable).toHaveBeenCalledWith(
385 "node",
386 expect.objectContaining({
387 platform: "win32",
388 }),
389 );
390 });
391​
392 it("loads the PowerShell profile when baseline env cannot resolve node", () => {
393 const readEnvironment = vi.fn(
394 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) =>
395 options?.loadProfile
396 ? {
397 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
398 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
399 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
400 }
401 : { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" },
402 );
403 const commandAvailable = vi.fn(() => false);
404​
405 expect(
406 resolveWindowsEnvironment(
407 {
408 PATH: "C:\\Windows\\System32",
409 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
410 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
411 USERPROFILE: "C:\\Users\\testuser",
412 },
413 {
414 readEnvironment,
415 commandAvailable,
416 },
417 ),
418 ).toEqual({
419 PATH: [
420 "C:\\Profile\\Node",
421 "C:\\Windows\\System32",
422 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
423 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
424 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
425 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
426 "C:\\Users\\testuser\\.bun\\bin",
427 "C:\\Users\\testuser\\scoop\\shims",
428 "C:\\Shell\\Bin",
429 ].join(";"),
430 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
431 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
432 });
433 expect(readEnvironment).toHaveBeenNthCalledWith(1, ["PATH"], { loadProfile: false });
434 expect(readEnvironment).toHaveBeenNthCalledWith(2, ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"], {
435 loadProfile: true,
436 });
437 expect(commandAvailable).toHaveBeenCalledTimes(1);
438 });
439​
440 it("keeps the baseline env when profiled probe still does not resolve node", () => {
441 const readEnvironment = vi.fn(
442 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) =>
443 options?.loadProfile ? { FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm" } : {},
444 );
445 const commandAvailable = vi.fn(() => false);
446​
447 expect(
448 resolveWindowsEnvironment(
449 {
450 PATH: "C:\\Windows\\System32",
451 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
452 USERPROFILE: "C:\\Users\\testuser",
453 },
454 {
455 readEnvironment,
456 commandAvailable,
457 },
458 ),
459 ).toEqual({
460 PATH: [
461 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
462 "C:\\Users\\testuser\\.bun\\bin",
463 "C:\\Users\\testuser\\scoop\\shims",
464 "C:\\Windows\\System32",
465 ].join(";"),
466 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
467 });
468 expect(commandAvailable).toHaveBeenCalledTimes(1);
469 });
470});