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

DesktopShellEnvironment.test.ts

751 lines23.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import * as Clock from "effect/Clock";
3import { assert, describe, it } from "@effect/vitest";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import * as Logger from "effect/Logger";
9import * as PlatformError from "effect/PlatformError";
10import * as Schema from "effect/Schema";
11import * as Sink from "effect/Sink";
12import * as Stream from "effect/Stream";
13import * as ChildProcess from "effect/process/ChildProcess";
14import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15import * as TestClock from "effect/testing/TestClock";
16import * as NodeOS from "node:os";
17import { vi } from "vite-plus/test";
18​
19import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
20import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
21​
22vi.mock("node:os", { spy: true });
23​
24const textEncoder = new TextEncoder();
25​
26const isDesktopShellEnvironmentCommandError = Schema.is(
27 DesktopShellEnvironment.DesktopShellEnvironmentCommandError,
28);
29​
30function envOutput(values: Readonly<Record<string, string>>): string {
31 return Object.entries(values)
32 .flatMap(([name, value]) => [
33 `__T3CODE_ENV_${name}_START__`,
34 value,
35 `__T3CODE_ENV_${name}_END__`,
36 ])
37 .join("\n");
39​
40function makeProcess(output: string, exitCode = 0): ChildProcessSpawner.ChildProcessHandle {
41 const stdout = output.length === 0 ? Stream.empty : Stream.make(textEncoder.encode(output));
42 return ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(123),
44 stdout,
45 stderr: Stream.empty,
46 all: stdout,
47 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
48 isRunning: Effect.succeed(false),
49 kill: () => Effect.void,
50 stdin: Sink.drain,
51 getInputFd: () => Sink.drain,
52 getOutputFd: () => Stream.empty,
53 unref: Effect.succeed(Effect.void),
54 });
56​
57function withProcessEnv<A, E, R>(
58 env: NodeJS.ProcessEnv,
59 effect: Effect.Effect<A, E, R>,
60): Effect.Effect<A, E, R> {
61 return Effect.acquireUseRelease(
62 Effect.sync(() => {
63 const previous = process.env;
64 process.env = env;
65 return previous;
66 }),
67 () => effect,
68 (previous) =>
69 Effect.sync(() => {
70 process.env = previous;
71 }),
72 );
74​
75function runShellEnvironment(input: {
76 readonly env: NodeJS.ProcessEnv;
77 readonly platform: NodeJS.Platform;
78 readonly userShell?: string;
79 readonly handler: (
80 command: ChildProcess.Command,
81 ) =>
82 | string
83 | { output: string; exitCode: number }
84 | Effect.Effect<ChildProcessSpawner.ChildProcessHandle, PlatformError.PlatformError>;
85 readonly failure?: PlatformError.PlatformError;
86}) {
87 const layerEnvironment = Layer.succeed(
88 DesktopEnvironment.DesktopEnvironment,
89 DesktopEnvironment.DesktopEnvironment.of({
90 platform: input.platform,
91 } as DesktopEnvironment.DesktopEnvironment["Service"]),
92 );
93 const layerSpawner = Layer.succeed(
94 ChildProcessSpawner.ChildProcessSpawner,
95 ChildProcessSpawner.make((command) => {
96 if (input.failure !== undefined) return Effect.fail(input.failure);
97 const result = input.handler(command);
98 if (Effect.isEffect(result)) return result;
99 return Effect.succeed(
100 typeof result === "string"
101 ? makeProcess(result)
102 : makeProcess(result.output, result.exitCode),
103 );
104 }),
105 );
106​
107 const program = Effect.gen(function* () {
108 const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
109 const prepared = yield* shellEnvironment.installIntoProcess;
110 assert.equal(yield* shellEnvironment.installIntoProcess, prepared);
111 return prepared;
112 }).pipe(
113 Effect.provide(
114 DesktopShellEnvironment.layer.pipe(
115 Layer.provide(Layer.mergeAll(layerEnvironment, NodeServices.layer, layerSpawner)),
116 ),
117 ),
118 );
119​
120 return Effect.acquireUseRelease(
121 Effect.sync(() =>
122 vi.spyOn(NodeOS, "userInfo").mockReturnValue({
123 ...NodeOS.userInfo(),
124 shell: input.userShell ?? null,
125 }),
126 ),
127 () => withProcessEnv(input.env, program),
128 (spy) => Effect.sync(() => spy.mockRestore()),
129 );
131​
132describe("DesktopShellEnvironment", () => {
133 it.effect("rejects PATH output from a failed shell and tries the next candidate", () =>
134 Effect.gen(function* () {
135 const env: NodeJS.ProcessEnv = { SHELL: "/bin/failing", PATH: "/usr/bin" };
136 const commands: string[] = [];
137​
138 yield* runShellEnvironment({
139 env,
140 platform: "linux",
141 userShell: "/usr/local/bin/fish",
142 handler: (command) => {
143 if (command._tag !== "StandardCommand") return "";
144 commands.push(command.command);
145 return command.command === "/bin/failing"
146 ? { output: envOutput({ PATH: "/failed/bin" }), exitCode: 7 }
147 : envOutput({ PATH: "/home/test/.local/bin:/usr/bin" });
148 },
149 }).pipe(Effect.provide(Logger.layer([])));
150​
151 assert.deepEqual(commands, ["/bin/failing", "/usr/local/bin/fish"]);
152 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
153 }),
154 );
155​
156 it.effect.each(["darwin", "linux"] as const)(
157 "uses the account shell when SHELL is missing on %s",
158 (platform) =>
159 Effect.gen(function* () {
160 const env: NodeJS.ProcessEnv = { PATH: "/usr/bin" };
161 const commands: string[] = [];
162​
163 yield* runShellEnvironment({
164 env,
165 platform,
166 userShell: "/usr/local/bin/fish",
167 handler: (command) => {
168 if (command._tag !== "StandardCommand") return "";
169 commands.push(command.command);
170 return envOutput({ PATH: "/home/test/.local/bin:/usr/bin" });
171 },
172 });
173​
174 assert.deepEqual(commands, ["/usr/local/bin/fish"]);
175 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
176 }),
177 );
178​
179 it.effect("tries the account shell before the platform fallback when SHELL has no PATH", () =>
180 Effect.gen(function* () {
181 const env: NodeJS.ProcessEnv = { SHELL: "/bin/missing", PATH: "/usr/bin" };
182 const commands: string[] = [];
183​
184 yield* runShellEnvironment({
185 env,
186 platform: "linux",
187 userShell: "/usr/local/bin/fish",
188 handler: (command) => {
189 if (command._tag !== "StandardCommand") return "";
190 commands.push(command.command);
191 return command.command === "/usr/local/bin/fish"
192 ? envOutput({ PATH: "/home/test/.local/bin:/usr/bin" })
193 : "";
194 },
195 });
196​
197 assert.deepEqual(commands, ["/bin/missing", "/usr/local/bin/fish"]);
198 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
199 }),
200 );
201​
202 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () =>
203 Effect.gen(function* () {
204 const env: NodeJS.ProcessEnv = {
205 SHELL: "/bin/zsh",
206 PATH: "/Users/test/.local/bin:/usr/bin",
207 };
208 const commands: ChildProcess.Command[] = [];
209​
210 yield* runShellEnvironment({
211 env,
212 platform: "darwin",
213 handler: (command) => {
214 commands.push(command);
215 return envOutput({
216 PATH: "/opt/homebrew/bin:/usr/bin",
217 SSH_AUTH_SOCK: "/tmp/secretive.sock",
218 HOMEBREW_PREFIX: "/opt/homebrew",
219 });
220 },
221 });
222​
223 assert.equal(commands.length, 1);
224 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
225 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
226 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
227 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
228 }),
229 );
230​
231 it.effect("preserves inherited POSIX values when present", () =>
232 Effect.gen(function* () {
233 const env: NodeJS.ProcessEnv = {
234 SHELL: "/bin/zsh",
235 PATH: "/usr/bin",
236 SSH_AUTH_SOCK: "/tmp/inherited.sock",
237 };
238​
239 yield* runShellEnvironment({
240 env,
241 platform: "darwin",
242 handler: () =>
243 envOutput({
244 PATH: "/opt/homebrew/bin:/usr/bin",
245 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
246 }),
247 });
248​
249 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
250 assert.equal(env.SSH_AUTH_SOCK, "/tmp/inherited.sock");
251 }),
252 );
253​
254 it.effect("hydrates the locale from the login shell on macOS", () =>
255 Effect.gen(function* () {
256 const env: NodeJS.ProcessEnv = {
257 SHELL: "/bin/zsh",
258 PATH: "/usr/bin",
259 };
260​
261 yield* runShellEnvironment({
262 env,
263 platform: "darwin",
264 handler: () =>
265 envOutput({
266 PATH: "/opt/homebrew/bin:/usr/bin",
267 LANG: "de_DE.UTF-8",
268 }),
269 });
270​
271 assert.equal(env.LANG, "de_DE.UTF-8");
272 }),
273 );
274​
275 it.effect("preserves an inherited locale over the login shell on macOS", () =>
276 Effect.gen(function* () {
277 const env: NodeJS.ProcessEnv = {
278 SHELL: "/bin/zsh",
279 PATH: "/usr/bin",
280 LANG: "en_US.UTF-8",
281 };
282​
283 yield* runShellEnvironment({
284 env,
285 platform: "darwin",
286 handler: () =>
287 envOutput({
288 PATH: "/opt/homebrew/bin:/usr/bin",
289 LANG: "de_DE.UTF-8",
290 }),
291 });
292​
293 assert.equal(env.LANG, "en_US.UTF-8");
294 }),
295 );
296​
297 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
298 Effect.gen(function* () {
299 const env: NodeJS.ProcessEnv = {
300 SHELL: "/bin/zsh",
301 PATH: "/usr/bin",
302 LANG: "en_US.UTF-8",
303 };
304​
305 yield* runShellEnvironment({
306 env,
307 platform: "darwin",
308 handler: () =>
309 envOutput({
310 PATH: "/opt/homebrew/bin:/usr/bin",
311 LC_ALL: "de_DE.UTF-8",
312 }),
313 });
314​
315 assert.equal(env.LANG, "en_US.UTF-8");
316 assert.equal(env.LC_ALL, undefined);
317 }),
318 );
319​
320 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
321 Effect.gen(function* () {
322 const env: NodeJS.ProcessEnv = {
323 SHELL: "/bin/zsh",
324 PATH: "/usr/bin",
325 };
326​
327 yield* runShellEnvironment({
328 env,
329 platform: "darwin",
330 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
331 });
332​
333 assert.equal(env.LANG, undefined);
334 assert.equal(env.LC_ALL, undefined);
335 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
336 }),
337 );
338​
339 it.effect("does not apply the locale fallback on linux", () =>
340 Effect.gen(function* () {
341 const env: NodeJS.ProcessEnv = {
342 SHELL: "/bin/zsh",
343 PATH: "/usr/bin",
344 };
345​
346 yield* runShellEnvironment({
347 env,
348 platform: "linux",
349 handler: () => envOutput({ PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin" }),
350 });
351​
352 assert.equal(env.LANG, undefined);
353 }),
354 );
355​
356 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () =>
357 Effect.gen(function* () {
358 const env: NodeJS.ProcessEnv = {
359 SHELL: "/bin/zsh",
360 PATH: "/usr/bin",
361 };
362​
363 yield* runShellEnvironment({
364 env,
365 platform: "linux",
366 handler: () =>
367 envOutput({
368 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
369 SSH_AUTH_SOCK: "/tmp/secretive.sock",
370 T3CODE_TELEMETRY_ENABLED: "false",
371 }),
372 });
373​
374 assert.equal(env.PATH, "/home/linuxbrew/.linuxbrew/bin:/usr/bin");
375 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
376 assert.equal(env.T3CODE_TELEMETRY_ENABLED, "false");
377 }),
378 );
379​
380 it.effect("hydrates missing CA trust variables from the login shell", () =>
381 Effect.gen(function* () {
382 const env: NodeJS.ProcessEnv = {
383 SHELL: "/bin/zsh",
384 PATH: "/usr/bin",
385 SSL_CERT_DIR: "/etc/ssl/certs",
386 };
387​
388 yield* runShellEnvironment({
389 env,
390 platform: "darwin",
391 handler: () =>
392 envOutput({
393 PATH: "/opt/zerobrew/bin:/usr/bin",
394 SSL_CERT_FILE: "/opt/zerobrew/etc/ca-certificates/cacert.pem",
395 SSL_CERT_DIR: "/opt/zerobrew/etc/ca-certificates",
396 NODE_EXTRA_CA_CERTS: "/Users/me/corp-root.pem",
397 }),
398 });
399​
400 assert.equal(env.SSL_CERT_FILE, "/opt/zerobrew/etc/ca-certificates/cacert.pem");
401 assert.equal(env.SSL_CERT_DIR, "/etc/ssl/certs");
402 assert.equal(env.NODE_EXTRA_CA_CERTS, "/Users/me/corp-root.pem");
403 }),
404 );
405​
406 it.effect("keeps mise's PATH record with the login shell PATH", () =>
407 Effect.gen(function* () {
408 const env: NodeJS.ProcessEnv = {
409 SHELL: "/bin/bash",
410 PATH: "/usr/bin",
411 };
412​
413 yield* runShellEnvironment({
414 env,
415 platform: "linux",
416 handler: () =>
417 envOutput({
418 PATH: "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
419 __MISE_DIFF: "mise-diff",
420 }),
421 });
422​
423 assert.equal(env.__MISE_DIFF, "mise-diff");
424 }),
425 );
426​
427 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
428 Effect.gen(function* () {
429 const env: NodeJS.ProcessEnv = {
430 SHELL: "/opt/homebrew/bin/nu",
431 PATH: "/usr/bin",
432 };
433 const commands: string[] = [];
434​
435 yield* runShellEnvironment({
436 env,
437 platform: "darwin",
438 handler: (command) => {
439 if (command._tag !== "StandardCommand") return "";
440 commands.push(command.command);
441 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
442 },
443 });
444​
445 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
446 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
447 }),
448 );
449​
450 it.effect("loads PowerShell profile environment on Windows", () =>
451 Effect.gen(function* () {
452 const env: NodeJS.ProcessEnv = {
453 PATH: "C:\\Windows\\System32",
454 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
455 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
456 USERPROFILE: "C:\\Users\\testuser",
457 };
458​
459 const prepared = yield* runShellEnvironment({
460 env,
461 platform: "win32",
462 handler: (command) => {
463 if (command._tag !== "StandardCommand") return "";
464 const loadProfile = !command.args.includes("-NoProfile");
465 return loadProfile
466 ? envOutput({
467 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
468 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
469 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
470 })
471 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
472 },
473 });
474​
475 assert.isTrue(prepared);
476 assert.equal(
477 env.PATH,
478 [
479 "C:\\Profile\\Node",
480 "C:\\Windows\\System32",
481 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
482 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
483 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
484 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
485 "C:\\Users\\testuser\\.local\\bin",
486 "C:\\Users\\testuser\\.bun\\bin",
487 "C:\\Users\\testuser\\scoop\\shims",
488 "C:\\Custom\\Bin",
489 "C:",
490 ].join(";"),
491 );
492 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
493 assert.equal(
494 env.FNM_MULTISHELL_PATH,
495 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
496 );
497 }),
498 );
499​
500 it.effect("waits at most 5 seconds in total when the PowerShell profile hangs", () =>
501 Effect.gen(function* () {
502 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
503 const profileShells: string[] = [];
504 const started = yield* Deferred.make<void>();
505 const completed = yield* Deferred.make<void>();
506 const baseClock = yield* Clock.Clock;
507 let wallTime = 0;
508 const clockWithBackwardWallJump: Clock.Clock = {
509 ...baseClock,
510 currentTimeMillisUnsafe: () => wallTime,
511 currentTimeMillis: Effect.sync(() => wallTime),
512 };
513​
514 const install = yield* Effect.forkChild(
515 runShellEnvironment({
516 env,
517 platform: "win32",
518 handler: (command) => {
519 if (command._tag !== "StandardCommand") return "";
520 if (command.args.includes("-NoProfile")) {
521 return envOutput({ PATH: "C:\\NoProfile\\Bin;C:\\Windows\\System32" });
522 }
523 profileShells.push(command.command);
524 return Deferred.succeed(started, undefined).pipe(
525 Effect.as(
526 ChildProcessSpawner.makeHandle({
527 ...makeProcess(""),
528 stdout: Stream.never,
529 all: Stream.never,
530 exitCode: Effect.never,
531 isRunning: Effect.succeed(true),
532 }),
533 ),
534 );
535 },
536 }).pipe(
537 Effect.tap(() => Deferred.succeed(completed, undefined)),
538 Effect.provide(Logger.layer([])),
539 Effect.provideService(Clock.Clock, clockWithBackwardWallJump),
540 ),
541 );
542 yield* Deferred.await(started);
543 wallTime = -60_000;
544 yield* TestClock.adjust("5 seconds");
545 assert.isTrue(yield* Deferred.isDone(completed), "finishes within the shared budget");
546 assert.isFalse(yield* Fiber.join(install));
547 assert.deepEqual(profileShells, ["pwsh.exe"]);
548 assert.equal(env.PATH, "C:\\NoProfile\\Bin;C:\\Windows\\System32");
549 }),
550 );
551​
552 it.effect("falls back to Windows PowerShell when pwsh.exe is not installed", () =>
553 Effect.gen(function* () {
554 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
555 const missing = PlatformError.systemError({
556 _tag: "NotFound",
557 module: "ChildProcess",
558 method: "spawn",
559 pathOrDescriptor: "pwsh.exe",
560 });
561​
562 const prepared = yield* runShellEnvironment({
563 env,
564 platform: "win32",
565 handler: (command) => {
566 if (command._tag !== "StandardCommand") return "";
567 if (command.command === "pwsh.exe") return Effect.fail(missing);
568 return command.args.includes("-NoProfile")
569 ? envOutput({ PATH: "C:\\Windows\\System32" })
570 : envOutput({ PATH: "C:\\Profile\\Node;C:\\Windows\\System32" });
571 },
572 }).pipe(Effect.provide(Logger.layer([])));
573​
574 assert.isTrue(prepared);
575 assert.equal(env.PATH, "C:\\Profile\\Node;C:\\Windows\\System32");
576 }),
577 );
578​
579 it.effect.each([false, true])(
580 "leaves Windows preparation incomplete when loadProfile=%s fails",
581 (loadProfile) =>
582 Effect.gen(function* () {
583 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
584 let calls = 0;
585 const prepared = yield* runShellEnvironment({
586 env,
587 platform: "win32",
588 handler: (command) => {
589 calls++;
590 if (command._tag !== "StandardCommand") return "";
591 return !command.args.includes("-NoProfile") === loadProfile
592 ? { output: "", exitCode: 1 }
593 : envOutput({ PATH: "C:\\Working" });
594 },
595 }).pipe(Effect.provide(Logger.layer([])));
596 assert.isFalse(prepared);
597 assert.equal(calls, 3);
598 assert.include(env.PATH, "C:\\Working");
599 assert.include(env.PATH, "C:\\Windows\\System32");
600 }),
601 );
602​
603 it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
604 Effect.gen(function* () {
605 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
606​
607 yield* runShellEnvironment({
608 env,
609 platform: "win32",
610 handler: (command) =>
611 command._tag === "StandardCommand" && command.args.includes("-NoProfile")
612 ? envOutput({ PATH: "C:\\Shell\\Bin" })
613 : "",
614 });
615​
616 assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
617 }),
618 );
619​
620 it.effect("drops earlier AppImage launches' mount dirs from the search paths on linux", () =>
621 Effect.gen(function* () {
622 const current = "/var/tmp/.mount_cur";
623 const env: NodeJS.ProcessEnv = {
624 SHELL: "/bin/zsh",
625 APPIMAGE: "/home/me/T3-Code.AppImage",
626 APPDIR: `${current}/`,
627 PATH: `${current}/usr/bin:/tmp/.mount_old/usr/bin:/usr/bin`,
628 LD_LIBRARY_PATH: `/tmp/.mount_old/usr/lib:${current}/usr/lib::/opt/lib`,
629 XDG_DATA_DIRS: "/tmp/.mount_old/usr/share:/var/tmp/.mount_cur2/usr/share",
630 };
631​
632 yield* runShellEnvironment({
633 env,
634 platform: "linux",
635 handler: () => envOutput({ PATH: "/tmp/.mount_older/usr/bin:/home/me/.local/bin" }),
636 });
637​
638 assert.equal(env.PATH, `/home/me/.local/bin:${current}/usr/bin:/usr/bin`);
639 assert.equal(env.LD_LIBRARY_PATH, `${current}/usr/lib::/opt/lib`);
640 assert.equal(env.XDG_DATA_DIRS, undefined);
641 }),
642 );
643​
644 it.effect("keeps AppImage-looking search paths outside an AppImage launch", () =>
645 Effect.gen(function* () {
646 const env: NodeJS.ProcessEnv = {
647 SHELL: "/bin/zsh",
648 PATH: "/tmp/.mount_old/usr/bin:/usr/bin",
649 LD_LIBRARY_PATH: "/tmp/.mount_old/usr/lib",
650 };
651​
652 yield* runShellEnvironment({
653 env,
654 platform: "linux",
655 handler: () => envOutput({ PATH: "/usr/bin" }),
656 });
657​
658 assert.equal(env.PATH, "/usr/bin:/tmp/.mount_old/usr/bin");
659 assert.equal(env.LD_LIBRARY_PATH, "/tmp/.mount_old/usr/lib");
660 }),
661 );
662​
663 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
664 Effect.gen(function* () {
665 const env: NodeJS.ProcessEnv = {
666 SHELL: "/bin/zsh",
667 PATH: "/usr/bin",
668 XDG_CURRENT_DESKTOP: "wrong-launcher",
669 XDG_SESSION_DESKTOP: "wrong-launcher",
670 };
671​
672 yield* runShellEnvironment({
673 env,
674 platform: "linux",
675 handler: () =>
676 envOutput({
677 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
678 XDG_CURRENT_DESKTOP: "KDE",
679 XDG_SESSION_DESKTOP: "KDE",
680 XDG_SESSION_TYPE: "wayland",
681 }),
682 });
683​
684 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
685 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
686 assert.equal(env.XDG_SESSION_TYPE, "wayland");
687 }),
688 );
689​
690 it.effect("overrides stale dbus session addresses from the login shell", () =>
691 Effect.gen(function* () {
692 const env: NodeJS.ProcessEnv = {
693 SHELL: "/bin/zsh",
694 PATH: "/usr/bin",
695 DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/stale-bus",
696 };
697​
698 yield* runShellEnvironment({
699 env,
700 platform: "linux",
701 handler: () =>
702 envOutput({
703 PATH: "/usr/bin",
704 DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
705 }),
706 });
707​
708 assert.equal(env.DBUS_SESSION_BUS_ADDRESS, "unix:path=/run/user/1000/bus");
709 }),
710 );
711​
712 it.effect("logs command failures with safe probe context and the exact cause", () => {
713 const env: NodeJS.ProcessEnv = {
714 SHELL: "/bin/bash",
715 PATH: "/usr/bin",
716 };
717 const cause = PlatformError.systemError({
718 _tag: "PermissionDenied",
719 module: "ChildProcess",
720 method: "spawn",
721 pathOrDescriptor: "/bin/bash",
722 });
723 const messages: Array<unknown> = [];
724 const logger = Logger.make(({ message }) => {
725 messages.push(message);
726 });
727​
728 return runShellEnvironment({
729 env,
730 platform: "linux",
731 handler: () => "",
732 failure: cause,
733 }).pipe(
734 Effect.andThen(
735 Effect.sync(() => {
736 const errors = messages
737 .flatMap((message) => (Array.isArray(message) ? message : [message]))
738 .filter(isDesktopShellEnvironmentCommandError);
739 assert.lengthOf(errors, 1);
740 assert.equal(errors[0]?.probe, "login-shell");
741 assert.equal(errors[0]?.executable, "bash");
742 assert.equal(errors[0]?.argumentCount, 2);
743 assert.notProperty(errors[0] ?? {}, "args");
744 assert.equal(errors[0]?.cause, cause);
745 assert.notInclude(errors[0]?.message ?? "", cause.message);
746 }),
747 ),
748 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
749 );
750 });
751});