Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off -- The process fixture runs real shell commands with isolated config files.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Logger from "effect/Logger";
7import * as PlatformError from "effect/PlatformError";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcess from "effect/process/ChildProcess";
11import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12import * as NodeOS from "node:os";
13import { vi } from "vite-plus/test";
14
15import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
16import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
17
18vi.mock("node:os", { spy: true });
19
20const textEncoder = new TextEncoder();
21
22function envOutput(values: Readonly<Record<string, string>>): string {
23 return Object.entries(values)
24 .flatMap(([name, value]) => [
25 `__T3CODE_ENV_${name}_START__`,
26 value,
27 `__T3CODE_ENV_${name}_END__`,
28 ])
29 .join("\n");
30}
31
32function makeProcess(output: string, exitCode = 0): ChildProcessSpawner.ChildProcessHandle {
33 const stdout = output.length === 0 ? Stream.empty : Stream.make(textEncoder.encode(output));
34 return ChildProcessSpawner.makeHandle({
35 pid: ChildProcessSpawner.ProcessId(123),
36 stdout,
37 stderr: Stream.empty,
38 all: stdout,
39 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
40 isRunning: Effect.succeed(false),
41 kill: () => Effect.void,
42 stdin: Sink.drain,
43 getInputFd: () => Sink.drain,
44 getOutputFd: () => Stream.empty,
45 unref: Effect.succeed(Effect.void),
46 });
47}
48
49function withProcessEnv<A, E, R>(
50 env: NodeJS.ProcessEnv,
51 effect: Effect.Effect<A, E, R>,
52): Effect.Effect<A, E, R> {
53 return Effect.acquireUseRelease(
54 Effect.sync(() => {
55 const previous = process.env;
56 process.env = env;
57 return previous;
58 }),
59 () => effect,
60 (previous) =>
61 Effect.sync(() => {
62 process.env = previous;
63 }),
64 );
65}
66
67function runShellEnvironment(input: {
68 readonly env: NodeJS.ProcessEnv;
69 readonly platform: NodeJS.Platform;
70 readonly userShell?: string;
71 readonly handler: (
72 command: ChildProcess.Command,
73 ) => string | { output: string; exitCode: number };
74 readonly failure?: PlatformError.PlatformError;
75}) {
76 const layerEnvironment = Layer.succeed(
77 DesktopEnvironment.DesktopEnvironment,
78 DesktopEnvironment.DesktopEnvironment.of({
79 platform: input.platform,
80 } as DesktopEnvironment.DesktopEnvironment["Service"]),
81 );
82 const layerSpawner = Layer.succeed(
83 ChildProcessSpawner.ChildProcessSpawner,
84 ChildProcessSpawner.make((command) => {
85 if (input.failure !== undefined) return Effect.fail(input.failure);
86 const result = input.handler(command);
87 return Effect.succeed(
88 typeof result === "string"
89 ? makeProcess(result)
90 : makeProcess(result.output, result.exitCode),
91 );
92 }),
93 );
94
95 const program = Effect.gen(function* () {
96 const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
97 const prepared = yield* shellEnvironment.installIntoProcess;
98 assert.equal(yield* shellEnvironment.installIntoProcess, prepared);
99 return prepared;
100 }).pipe(
101 Effect.provide(
102 DesktopShellEnvironment.layer.pipe(
103 Layer.provide(Layer.mergeAll(layerEnvironment, NodeServices.layer, layerSpawner)),
104 ),
105 ),
106 );
107
108 return Effect.acquireUseRelease(
109 Effect.sync(() =>
110 vi.spyOn(NodeOS, "userInfo").mockReturnValue({
111 ...NodeOS.userInfo(),
112 shell: input.userShell ?? null,
113 }),
114 ),
115 () => withProcessEnv(input.env, program),
116 (spy) => Effect.sync(() => spy.mockRestore()),
117 );
118}
119
120describe("DesktopShellEnvironment", () => {
121 it.effect("rejects PATH output from a failed shell and tries the next candidate", () =>
122 Effect.gen(function* () {
123 const env: NodeJS.ProcessEnv = { SHELL: "/bin/failing", PATH: "/usr/bin" };
124 const commands: string[] = [];
125
126 yield* runShellEnvironment({
127 env,
128 platform: "linux",
129 userShell: "/usr/local/bin/fish",
130 handler: (command) => {
131 if (command._tag !== "StandardCommand") return "";
132 commands.push(command.command);
133 return command.command === "/bin/failing"
134 ? { output: envOutput({ PATH: "/failed/bin" }), exitCode: 7 }
135 : envOutput({ PATH: "/home/test/.local/bin:/usr/bin" });
136 },
137 }).pipe(Effect.provide(Logger.layer([])));
138
139 assert.deepEqual(commands, ["/bin/failing", "/usr/local/bin/fish"]);
140 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
141 }),
142 );
143
144 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () =>
145 Effect.gen(function* () {
146 const env: NodeJS.ProcessEnv = {
147 SHELL: "/bin/zsh",
148 PATH: "/Users/test/.local/bin:/usr/bin",
149 };
150 const commands: ChildProcess.Command[] = [];
151
152 yield* runShellEnvironment({
153 env,
154 platform: "darwin",
155 handler: (command) => {
156 commands.push(command);
157 return envOutput({
158 PATH: "/opt/homebrew/bin:/usr/bin",
159 SSH_AUTH_SOCK: "/tmp/secretive.sock",
160 HOMEBREW_PREFIX: "/opt/homebrew",
161 });
162 },
163 });
164
165 assert.equal(commands.length, 1);
166 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
167 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
168 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
169 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
170 }),
171 );
172
173 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
174 Effect.gen(function* () {
175 const env: NodeJS.ProcessEnv = {
176 SHELL: "/bin/zsh",
177 PATH: "/usr/bin",
178 LANG: "en_US.UTF-8",
179 };
180
181 yield* runShellEnvironment({
182 env,
183 platform: "darwin",
184 handler: () =>
185 envOutput({
186 PATH: "/opt/homebrew/bin:/usr/bin",
187 LC_ALL: "de_DE.UTF-8",
188 }),
189 });
190
191 assert.equal(env.LANG, "en_US.UTF-8");
192 assert.equal(env.LC_ALL, undefined);
193 }),
194 );
195
196 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
197 Effect.gen(function* () {
198 const env: NodeJS.ProcessEnv = {
199 SHELL: "/bin/zsh",
200 PATH: "/usr/bin",
201 };
202
203 yield* runShellEnvironment({
204 env,
205 platform: "darwin",
206 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
207 });
208
209 assert.equal(env.LANG, undefined);
210 assert.equal(env.LC_ALL, undefined);
211 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
212 }),
213 );
214
215 it.effect("hydrates missing CA trust variables from the login shell", () =>
216 Effect.gen(function* () {
217 const env: NodeJS.ProcessEnv = {
218 SHELL: "/bin/zsh",
219 PATH: "/usr/bin",
220 SSL_CERT_DIR: "/etc/ssl/certs",
221 };
222
223 yield* runShellEnvironment({
224 env,
225 platform: "darwin",
226 handler: () =>
227 envOutput({
228 PATH: "/opt/zerobrew/bin:/usr/bin",
229 SSL_CERT_FILE: "/opt/zerobrew/etc/ca-certificates/cacert.pem",
230 SSL_CERT_DIR: "/opt/zerobrew/etc/ca-certificates",
231 NODE_EXTRA_CA_CERTS: "/Users/me/corp-root.pem",
232 }),
233 });
234
235 assert.equal(env.SSL_CERT_FILE, "/opt/zerobrew/etc/ca-certificates/cacert.pem");
236 assert.equal(env.SSL_CERT_DIR, "/etc/ssl/certs");
237 assert.equal(env.NODE_EXTRA_CA_CERTS, "/Users/me/corp-root.pem");
238 }),
239 );
240
241 it.effect("keeps mise's PATH record with the login shell PATH", () =>
242 Effect.gen(function* () {
243 const env: NodeJS.ProcessEnv = {
244 SHELL: "/bin/bash",
245 PATH: "/usr/bin",
246 };
247
248 yield* runShellEnvironment({
249 env,
250 platform: "linux",
251 handler: () =>
252 envOutput({
253 PATH: "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
254 __MISE_DIFF: "mise-diff",
255 }),
256 });
257
258 assert.equal(env.__MISE_DIFF, "mise-diff");
259 }),
260 );
261
262 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
263 Effect.gen(function* () {
264 const env: NodeJS.ProcessEnv = {
265 SHELL: "/opt/homebrew/bin/nu",
266 PATH: "/usr/bin",
267 };
268 const commands: string[] = [];
269
270 yield* runShellEnvironment({
271 env,
272 platform: "darwin",
273 handler: (command) => {
274 if (command._tag !== "StandardCommand") return "";
275 commands.push(command.command);
276 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
277 },
278 });
279
280 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
281 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
282 }),
283 );
284
285 it.effect("loads PowerShell profile environment on Windows", () =>
286 Effect.gen(function* () {
287 const env: NodeJS.ProcessEnv = {
288 PATH: "C:\\Windows\\System32",
289 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
290 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
291 USERPROFILE: "C:\\Users\\testuser",
292 };
293
294 const prepared = yield* runShellEnvironment({
295 env,
296 platform: "win32",
297 handler: (command) => {
298 if (command._tag !== "StandardCommand") return "";
299 const loadProfile = !command.args.includes("-NoProfile");
300 return loadProfile
301 ? envOutput({
302 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
303 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
304 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
305 })
306 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
307 },
308 });
309
310 assert.isTrue(prepared);
311 assert.equal(
312 env.PATH,
313 [
314 "C:\\Profile\\Node",
315 "C:\\Windows\\System32",
316 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
317 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
318 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
319 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
320 "C:\\Users\\testuser\\.local\\bin",
321 "C:\\Users\\testuser\\.bun\\bin",
322 "C:\\Users\\testuser\\scoop\\shims",
323 "C:\\Custom\\Bin",
324 "C:",
325 ].join(";"),
326 );
327 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
328 assert.equal(
329 env.FNM_MULTISHELL_PATH,
330 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
331 );
332 }),
333 );
334
335 it.effect.each([false, true])(
336 "leaves Windows preparation incomplete when loadProfile=%s fails",
337 (loadProfile) =>
338 Effect.gen(function* () {
339 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
340 let calls = 0;
341 const prepared = yield* runShellEnvironment({
342 env,
343 platform: "win32",
344 handler: (command) => {
345 calls++;
346 if (command._tag !== "StandardCommand") return "";
347 return !command.args.includes("-NoProfile") === loadProfile
348 ? { output: "", exitCode: 1 }
349 : envOutput({ PATH: "C:\\Working" });
350 },
351 }).pipe(Effect.provide(Logger.layer([])));
352 assert.isFalse(prepared);
353 assert.equal(calls, 3);
354 assert.include(env.PATH, "C:\\Working");
355 assert.include(env.PATH, "C:\\Windows\\System32");
356 }),
357 );
358
359 it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
360 Effect.gen(function* () {
361 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
362
363 yield* runShellEnvironment({
364 env,
365 platform: "win32",
366 handler: (command) =>
367 command._tag === "StandardCommand" && command.args.includes("-NoProfile")
368 ? envOutput({ PATH: "C:\\Shell\\Bin" })
369 : "",
370 });
371
372 assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
373 }),
374 );
375
376 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
377 Effect.gen(function* () {
378 const env: NodeJS.ProcessEnv = {
379 SHELL: "/bin/zsh",
380 PATH: "/usr/bin",
381 XDG_CURRENT_DESKTOP: "wrong-launcher",
382 XDG_SESSION_DESKTOP: "wrong-launcher",
383 };
384
385 yield* runShellEnvironment({
386 env,
387 platform: "linux",
388 handler: () =>
389 envOutput({
390 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
391 XDG_CURRENT_DESKTOP: "KDE",
392 XDG_SESSION_DESKTOP: "KDE",
393 XDG_SESSION_TYPE: "wayland",
394 }),
395 });
396
397 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
398 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
399 assert.equal(env.XDG_SESSION_TYPE, "wayland");
400 }),
401 );
402});