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