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("tries the account shell before the platform fallback when SHELL has no PATH", () =>
145 Effect.gen(function* () {
146 const env: NodeJS.ProcessEnv = { SHELL: "/bin/missing", PATH: "/usr/bin" };
147 const commands: string[] = [];
148
149 yield* runShellEnvironment({
150 env,
151 platform: "linux",
152 userShell: "/usr/local/bin/fish",
153 handler: (command) => {
154 if (command._tag !== "StandardCommand") return "";
155 commands.push(command.command);
156 return command.command === "/usr/local/bin/fish"
157 ? envOutput({ PATH: "/home/test/.local/bin:/usr/bin" })
158 : "";
159 },
160 });
161
162 assert.deepEqual(commands, ["/bin/missing", "/usr/local/bin/fish"]);
163 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
164 }),
165 );
166
167 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () =>
168 Effect.gen(function* () {
169 const env: NodeJS.ProcessEnv = {
170 SHELL: "/bin/zsh",
171 PATH: "/Users/test/.local/bin:/usr/bin",
172 };
173 const commands: ChildProcess.Command[] = [];
174
175 yield* runShellEnvironment({
176 env,
177 platform: "darwin",
178 handler: (command) => {
179 commands.push(command);
180 return envOutput({
181 PATH: "/opt/homebrew/bin:/usr/bin",
182 SSH_AUTH_SOCK: "/tmp/secretive.sock",
183 HOMEBREW_PREFIX: "/opt/homebrew",
184 });
185 },
186 });
187
188 assert.equal(commands.length, 1);
189 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
190 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
191 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
192 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
193 }),
194 );
195
196 it.effect("preserves inherited POSIX values when present", () =>
197 Effect.gen(function* () {
198 const env: NodeJS.ProcessEnv = {
199 SHELL: "/bin/zsh",
200 PATH: "/usr/bin",
201 SSH_AUTH_SOCK: "/tmp/inherited.sock",
202 };
203
204 yield* runShellEnvironment({
205 env,
206 platform: "darwin",
207 handler: () =>
208 envOutput({
209 PATH: "/opt/homebrew/bin:/usr/bin",
210 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
211 }),
212 });
213
214 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
215 assert.equal(env.SSH_AUTH_SOCK, "/tmp/inherited.sock");
216 }),
217 );
218
219 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
220 Effect.gen(function* () {
221 const env: NodeJS.ProcessEnv = {
222 SHELL: "/bin/zsh",
223 PATH: "/usr/bin",
224 LANG: "en_US.UTF-8",
225 };
226
227 yield* runShellEnvironment({
228 env,
229 platform: "darwin",
230 handler: () =>
231 envOutput({
232 PATH: "/opt/homebrew/bin:/usr/bin",
233 LC_ALL: "de_DE.UTF-8",
234 }),
235 });
236
237 assert.equal(env.LANG, "en_US.UTF-8");
238 assert.equal(env.LC_ALL, undefined);
239 }),
240 );
241
242 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
243 Effect.gen(function* () {
244 const env: NodeJS.ProcessEnv = {
245 SHELL: "/bin/zsh",
246 PATH: "/usr/bin",
247 };
248
249 yield* runShellEnvironment({
250 env,
251 platform: "darwin",
252 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
253 });
254
255 assert.equal(env.LANG, undefined);
256 assert.equal(env.LC_ALL, undefined);
257 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
258 }),
259 );
260
261 it.effect("hydrates missing CA trust variables from the login shell", () =>
262 Effect.gen(function* () {
263 const env: NodeJS.ProcessEnv = {
264 SHELL: "/bin/zsh",
265 PATH: "/usr/bin",
266 SSL_CERT_DIR: "/etc/ssl/certs",
267 };
268
269 yield* runShellEnvironment({
270 env,
271 platform: "darwin",
272 handler: () =>
273 envOutput({
274 PATH: "/opt/zerobrew/bin:/usr/bin",
275 SSL_CERT_FILE: "/opt/zerobrew/etc/ca-certificates/cacert.pem",
276 SSL_CERT_DIR: "/opt/zerobrew/etc/ca-certificates",
277 NODE_EXTRA_CA_CERTS: "/Users/me/corp-root.pem",
278 }),
279 });
280
281 assert.equal(env.SSL_CERT_FILE, "/opt/zerobrew/etc/ca-certificates/cacert.pem");
282 assert.equal(env.SSL_CERT_DIR, "/etc/ssl/certs");
283 assert.equal(env.NODE_EXTRA_CA_CERTS, "/Users/me/corp-root.pem");
284 }),
285 );
286
287 it.effect("keeps mise's PATH record with the login shell PATH", () =>
288 Effect.gen(function* () {
289 const env: NodeJS.ProcessEnv = {
290 SHELL: "/bin/bash",
291 PATH: "/usr/bin",
292 };
293
294 yield* runShellEnvironment({
295 env,
296 platform: "linux",
297 handler: () =>
298 envOutput({
299 PATH: "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
300 __MISE_DIFF: "mise-diff",
301 }),
302 });
303
304 assert.equal(env.__MISE_DIFF, "mise-diff");
305 }),
306 );
307
308 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
309 Effect.gen(function* () {
310 const env: NodeJS.ProcessEnv = {
311 SHELL: "/opt/homebrew/bin/nu",
312 PATH: "/usr/bin",
313 };
314 const commands: string[] = [];
315
316 yield* runShellEnvironment({
317 env,
318 platform: "darwin",
319 handler: (command) => {
320 if (command._tag !== "StandardCommand") return "";
321 commands.push(command.command);
322 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
323 },
324 });
325
326 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
327 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
328 }),
329 );
330
331 it.effect("loads PowerShell profile environment on Windows", () =>
332 Effect.gen(function* () {
333 const env: NodeJS.ProcessEnv = {
334 PATH: "C:\\Windows\\System32",
335 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
336 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
337 USERPROFILE: "C:\\Users\\testuser",
338 };
339
340 const prepared = yield* runShellEnvironment({
341 env,
342 platform: "win32",
343 handler: (command) => {
344 if (command._tag !== "StandardCommand") return "";
345 const loadProfile = !command.args.includes("-NoProfile");
346 return loadProfile
347 ? envOutput({
348 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
349 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
350 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
351 })
352 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
353 },
354 });
355
356 assert.isTrue(prepared);
357 assert.equal(
358 env.PATH,
359 [
360 "C:\\Profile\\Node",
361 "C:\\Windows\\System32",
362 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
363 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
364 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
365 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
366 "C:\\Users\\testuser\\.local\\bin",
367 "C:\\Users\\testuser\\.bun\\bin",
368 "C:\\Users\\testuser\\scoop\\shims",
369 "C:\\Custom\\Bin",
370 "C:",
371 ].join(";"),
372 );
373 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
374 assert.equal(
375 env.FNM_MULTISHELL_PATH,
376 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
377 );
378 }),
379 );
380
381 it.effect.each([false, true])(
382 "leaves Windows preparation incomplete when loadProfile=%s fails",
383 (loadProfile) =>
384 Effect.gen(function* () {
385 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
386 let calls = 0;
387 const prepared = yield* runShellEnvironment({
388 env,
389 platform: "win32",
390 handler: (command) => {
391 calls++;
392 if (command._tag !== "StandardCommand") return "";
393 return !command.args.includes("-NoProfile") === loadProfile
394 ? { output: "", exitCode: 1 }
395 : envOutput({ PATH: "C:\\Working" });
396 },
397 }).pipe(Effect.provide(Logger.layer([])));
398 assert.isFalse(prepared);
399 assert.equal(calls, 3);
400 assert.include(env.PATH, "C:\\Working");
401 assert.include(env.PATH, "C:\\Windows\\System32");
402 }),
403 );
404
405 it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
406 Effect.gen(function* () {
407 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
408
409 yield* runShellEnvironment({
410 env,
411 platform: "win32",
412 handler: (command) =>
413 command._tag === "StandardCommand" && command.args.includes("-NoProfile")
414 ? envOutput({ PATH: "C:\\Shell\\Bin" })
415 : "",
416 });
417
418 assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
419 }),
420 );
421
422 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
423 Effect.gen(function* () {
424 const env: NodeJS.ProcessEnv = {
425 SHELL: "/bin/zsh",
426 PATH: "/usr/bin",
427 XDG_CURRENT_DESKTOP: "wrong-launcher",
428 XDG_SESSION_DESKTOP: "wrong-launcher",
429 };
430
431 yield* runShellEnvironment({
432 env,
433 platform: "linux",
434 handler: () =>
435 envOutput({
436 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
437 XDG_CURRENT_DESKTOP: "KDE",
438 XDG_SESSION_DESKTOP: "KDE",
439 XDG_SESSION_TYPE: "wayland",
440 }),
441 });
442
443 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
444 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
445 assert.equal(env.XDG_SESSION_TYPE, "wayland");
446 }),
447 );
448});