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

shell.test.ts

190 lines5.8 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import {
4 extractPathFromShellOutput,
5 listLoginShellCandidates,
6 mergePathEntries,
7 readEnvironmentFromLoginShell,
8 readPathFromLaunchctl,
9 readPathFromLoginShell,
10} from "./shell";
11​
12describe("extractPathFromShellOutput", () => {
13 it("extracts the path between capture markers", () => {
14 expect(
15 extractPathFromShellOutput(
16 "__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n",
17 ),
18 ).toBe("/opt/homebrew/bin:/usr/bin");
19 });
20​
21 it("ignores shell startup noise around the capture markers", () => {
22 expect(
23 extractPathFromShellOutput(
24 "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n",
25 ),
26 ).toBe("/opt/homebrew/bin:/usr/bin");
27 });
28​
29 it("returns null when the markers are missing", () => {
30 expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull();
31 });
32});
33​
34describe("readPathFromLoginShell", () => {
35 it("uses a shell-agnostic printenv PATH probe", () => {
36 const execFile = vi.fn<
37 (
38 file: string,
39 args: ReadonlyArray<string>,
40 options: { encoding: "utf8"; timeout: number },
41 ) => string
42 >(() => "__T3CODE_ENV_PATH_START__\n/a:/b\n__T3CODE_ENV_PATH_END__\n");
43​
44 expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b");
45 expect(execFile).toHaveBeenCalledTimes(1);
46​
47 const firstCall = execFile.mock.calls[0] as
48 | [string, ReadonlyArray<string>, { encoding: "utf8"; timeout: number }]
49 | undefined;
50 expect(firstCall).toBeDefined();
51 if (!firstCall) {
52 throw new Error("Expected execFile to be called");
53 }
54​
55 const [shell, args, options] = firstCall;
56 expect(shell).toBe("/opt/homebrew/bin/fish");
57 expect(args).toHaveLength(2);
58 expect(args?.[0]).toBe("-ilc");
59 expect(args?.[1]).toContain("printenv PATH || true");
60 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_START__");
61 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_END__");
62 expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
63 });
64});
65​
66describe("readPathFromLaunchctl", () => {
67 it("returns a trimmed PATH value from launchctl", () => {
68 const execFile = vi.fn<
69 (
70 file: string,
71 args: ReadonlyArray<string>,
72 options: { encoding: "utf8"; timeout: number },
73 ) => string
74 >(() => " /opt/homebrew/bin:/usr/bin \n");
75​
76 expect(readPathFromLaunchctl(execFile)).toBe("/opt/homebrew/bin:/usr/bin");
77 expect(execFile).toHaveBeenCalledWith("/bin/launchctl", ["getenv", "PATH"], {
78 encoding: "utf8",
79 timeout: 2000,
80 });
81 });
82​
83 it("returns undefined when launchctl is unavailable", () => {
84 const execFile = vi.fn<
85 (
86 file: string,
87 args: ReadonlyArray<string>,
88 options: { encoding: "utf8"; timeout: number },
89 ) => string
90 >(() => {
91 throw new Error("spawn /bin/launchctl ENOENT");
92 });
93​
94 expect(readPathFromLaunchctl(execFile)).toBeUndefined();
95 });
96});
97​
98describe("readEnvironmentFromLoginShell", () => {
99 it("extracts multiple environment variables from a login shell command", () => {
100 const execFile = vi.fn<
101 (
102 file: string,
103 args: ReadonlyArray<string>,
104 options: { encoding: "utf8"; timeout: number },
105 ) => string
106 >(() =>
107 [
108 "__T3CODE_ENV_PATH_START__",
109 "/a:/b",
110 "__T3CODE_ENV_PATH_END__",
111 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
112 "/tmp/secretive.sock",
113 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
114 ].join("\n"),
115 );
116​
117 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
118 PATH: "/a:/b",
119 SSH_AUTH_SOCK: "/tmp/secretive.sock",
120 });
121 expect(execFile).toHaveBeenCalledTimes(1);
122 });
123​
124 it("omits environment variables that are missing or empty", () => {
125 const execFile = vi.fn<
126 (
127 file: string,
128 args: ReadonlyArray<string>,
129 options: { encoding: "utf8"; timeout: number },
130 ) => string
131 >(() =>
132 [
133 "__T3CODE_ENV_PATH_START__",
134 "/a:/b",
135 "__T3CODE_ENV_PATH_END__",
136 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
137 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
138 ].join("\n"),
139 );
140​
141 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
142 PATH: "/a:/b",
143 });
144 });
145​
146 it("preserves surrounding whitespace in captured values", () => {
147 const execFile = vi.fn<
148 (
149 file: string,
150 args: ReadonlyArray<string>,
151 options: { encoding: "utf8"; timeout: number },
152 ) => string
153 >(() =>
154 ["__T3CODE_ENV_CUSTOM_VAR_START__", " padded value ", "__T3CODE_ENV_CUSTOM_VAR_END__"].join(
155 "\n",
156 ),
157 );
158​
159 expect(readEnvironmentFromLoginShell("/bin/zsh", ["CUSTOM_VAR"], execFile)).toEqual({
160 CUSTOM_VAR: " padded value ",
161 });
162 });
163});
164​
165describe("listLoginShellCandidates", () => {
166 it("returns env shell, user shell, then the platform fallback without duplicates", () => {
167 expect(listLoginShellCandidates("darwin", " /opt/homebrew/bin/nu ", "/bin/zsh")).toEqual([
168 "/opt/homebrew/bin/nu",
169 "/bin/zsh",
170 ]);
171 });
172​
173 it("falls back to the platform default when no shells are available", () => {
174 expect(listLoginShellCandidates("linux", undefined, "")).toEqual(["/bin/bash"]);
175 });
176});
177​
178describe("mergePathEntries", () => {
179 it("prefers login-shell PATH entries and keeps inherited extras", () => {
180 expect(
181 mergePathEntries("/opt/homebrew/bin:/usr/bin", "/Users/test/.local/bin:/usr/bin", "darwin"),
182 ).toBe("/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
183 });
184​
185 it("uses the platform-specific delimiter", () => {
186 expect(mergePathEntries("C:\\Tools;C:\\Windows", "C:\\Windows;C:\\Git", "win32")).toBe(
187 "C:\\Tools;C:\\Windows;C:\\Git",
188 );
189 });
190});