Open History to see the last commit
1import { describe, expect, it, vi } from "vitest";
2
3import { extractPathFromShellOutput, readPathFromLoginShell } from "./shell";
4
5describe("extractPathFromShellOutput", () => {
6 it("extracts the path between capture markers", () => {
7 expect(
8 extractPathFromShellOutput(
9 "__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n",
10 ),
11 ).toBe("/opt/homebrew/bin:/usr/bin");
12 });
13
14 it("ignores shell startup noise around the capture markers", () => {
15 expect(
16 extractPathFromShellOutput(
17 "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n",
18 ),
19 ).toBe("/opt/homebrew/bin:/usr/bin");
20 });
21
22 it("returns null when the markers are missing", () => {
23 expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull();
24 });
25});
26
27describe("readPathFromLoginShell", () => {
28 it("uses a shell-agnostic printenv PATH probe", () => {
29 const execFile = vi.fn<
30 (
31 file: string,
32 args: ReadonlyArray<string>,
33 options: { encoding: "utf8"; timeout: number },
34 ) => string
35 >(() => "__T3CODE_PATH_START__\n/a:/b\n__T3CODE_PATH_END__\n");
36
37 expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b");
38 expect(execFile).toHaveBeenCalledTimes(1);
39
40 const firstCall = execFile.mock.calls[0] as
41 | [string, ReadonlyArray<string>, { encoding: "utf8"; timeout: number }]
42 | undefined;
43 expect(firstCall).toBeDefined();
44 if (!firstCall) {
45 throw new Error("Expected execFile to be called");
46 }
47
48 const [shell, args, options] = firstCall;
49 expect(shell).toBe("/opt/homebrew/bin/fish");
50 expect(args).toHaveLength(2);
51 expect(args?.[0]).toBe("-ilc");
52 expect(args?.[1]).toContain("printenv PATH");
53 expect(args?.[1]).toContain("__T3CODE_PATH_START__");
54 expect(args?.[1]).toContain("__T3CODE_PATH_END__");
55 expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
56 });
57});