Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Logger from "effect/Logger";
6import * as PlatformError from "effect/PlatformError";
7import * as Schema from "effect/Schema";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcess from "effect/unstable/process/ChildProcess";
11import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
12
13import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
14import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
15
16const textEncoder = new TextEncoder();
17
18const isDesktopShellEnvironmentCommandError = Schema.is(
19 DesktopShellEnvironment.DesktopShellEnvironmentCommandError,
20);
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): 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(0)),
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 handler: (command: ChildProcess.Command) => string;
71 readonly failure?: PlatformError.PlatformError;
72}) {
73 const environmentLayer = Layer.succeed(
74 DesktopEnvironment.DesktopEnvironment,
75 DesktopEnvironment.DesktopEnvironment.of({
76 platform: input.platform,
77 } as DesktopEnvironment.DesktopEnvironment["Service"]),
78 );
79 const spawnerLayer = Layer.succeed(
80 ChildProcessSpawner.ChildProcessSpawner,
81 ChildProcessSpawner.make((command) =>
82 input.failure === undefined
83 ? Effect.succeed(makeProcess(input.handler(command)))
84 : Effect.fail(input.failure),
85 ),
86 );
87
88 const program = Effect.gen(function* () {
89 const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
90 yield* shellEnvironment.installIntoProcess;
91 }).pipe(
92 Effect.provide(
93 DesktopShellEnvironment.layer.pipe(
94 Layer.provide(Layer.mergeAll(environmentLayer, NodeServices.layer, spawnerLayer)),
95 ),
96 ),
97 );
98
99 return withProcessEnv(input.env, program);
100}
101
102describe("DesktopShellEnvironment", () => {
103 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () =>
104 Effect.gen(function* () {
105 const env: NodeJS.ProcessEnv = {
106 SHELL: "/bin/zsh",
107 PATH: "/Users/test/.local/bin:/usr/bin",
108 };
109 const commands: ChildProcess.Command[] = [];
110
111 yield* runShellEnvironment({
112 env,
113 platform: "darwin",
114 handler: (command) => {
115 commands.push(command);
116 return envOutput({
117 PATH: "/opt/homebrew/bin:/usr/bin",
118 SSH_AUTH_SOCK: "/tmp/secretive.sock",
119 HOMEBREW_PREFIX: "/opt/homebrew",
120 });
121 },
122 });
123
124 assert.equal(commands.length, 1);
125 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
126 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
127 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
128 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
129 }),
130 );
131
132 it.effect("preserves inherited POSIX values when present", () =>
133 Effect.gen(function* () {
134 const env: NodeJS.ProcessEnv = {
135 SHELL: "/bin/zsh",
136 PATH: "/usr/bin",
137 SSH_AUTH_SOCK: "/tmp/inherited.sock",
138 };
139
140 yield* runShellEnvironment({
141 env,
142 platform: "darwin",
143 handler: () =>
144 envOutput({
145 PATH: "/opt/homebrew/bin:/usr/bin",
146 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
147 }),
148 });
149
150 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
151 assert.equal(env.SSH_AUTH_SOCK, "/tmp/inherited.sock");
152 }),
153 );
154
155 it.effect("hydrates the locale from the login shell on macOS", () =>
156 Effect.gen(function* () {
157 const env: NodeJS.ProcessEnv = {
158 SHELL: "/bin/zsh",
159 PATH: "/usr/bin",
160 };
161
162 yield* runShellEnvironment({
163 env,
164 platform: "darwin",
165 handler: () =>
166 envOutput({
167 PATH: "/opt/homebrew/bin:/usr/bin",
168 LANG: "de_DE.UTF-8",
169 }),
170 });
171
172 assert.equal(env.LANG, "de_DE.UTF-8");
173 }),
174 );
175
176 it.effect("preserves an inherited locale over the login shell on macOS", () =>
177 Effect.gen(function* () {
178 const env: NodeJS.ProcessEnv = {
179 SHELL: "/bin/zsh",
180 PATH: "/usr/bin",
181 LANG: "en_US.UTF-8",
182 };
183
184 yield* runShellEnvironment({
185 env,
186 platform: "darwin",
187 handler: () =>
188 envOutput({
189 PATH: "/opt/homebrew/bin:/usr/bin",
190 LANG: "de_DE.UTF-8",
191 }),
192 });
193
194 assert.equal(env.LANG, "en_US.UTF-8");
195 }),
196 );
197
198 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
199 Effect.gen(function* () {
200 const env: NodeJS.ProcessEnv = {
201 SHELL: "/bin/zsh",
202 PATH: "/usr/bin",
203 LANG: "en_US.UTF-8",
204 };
205
206 yield* runShellEnvironment({
207 env,
208 platform: "darwin",
209 handler: () =>
210 envOutput({
211 PATH: "/opt/homebrew/bin:/usr/bin",
212 LC_ALL: "de_DE.UTF-8",
213 }),
214 });
215
216 assert.equal(env.LANG, "en_US.UTF-8");
217 assert.equal(env.LC_ALL, undefined);
218 }),
219 );
220
221 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
222 Effect.gen(function* () {
223 const env: NodeJS.ProcessEnv = {
224 SHELL: "/bin/zsh",
225 PATH: "/usr/bin",
226 };
227
228 yield* runShellEnvironment({
229 env,
230 platform: "darwin",
231 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
232 });
233
234 assert.equal(env.LANG, undefined);
235 assert.equal(env.LC_ALL, undefined);
236 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
237 }),
238 );
239
240 it.effect("does not apply the locale fallback on linux", () =>
241 Effect.gen(function* () {
242 const env: NodeJS.ProcessEnv = {
243 SHELL: "/bin/zsh",
244 PATH: "/usr/bin",
245 };
246
247 yield* runShellEnvironment({
248 env,
249 platform: "linux",
250 handler: () => envOutput({ PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin" }),
251 });
252
253 assert.equal(env.LANG, undefined);
254 }),
255 );
256
257 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () =>
258 Effect.gen(function* () {
259 const env: NodeJS.ProcessEnv = {
260 SHELL: "/bin/zsh",
261 PATH: "/usr/bin",
262 };
263
264 yield* runShellEnvironment({
265 env,
266 platform: "linux",
267 handler: () =>
268 envOutput({
269 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
270 SSH_AUTH_SOCK: "/tmp/secretive.sock",
271 }),
272 });
273
274 assert.equal(env.PATH, "/home/linuxbrew/.linuxbrew/bin:/usr/bin");
275 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
276 }),
277 );
278
279 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
280 Effect.gen(function* () {
281 const env: NodeJS.ProcessEnv = {
282 SHELL: "/opt/homebrew/bin/nu",
283 PATH: "/usr/bin",
284 };
285 const commands: string[] = [];
286
287 yield* runShellEnvironment({
288 env,
289 platform: "darwin",
290 handler: (command) => {
291 if (command._tag !== "StandardCommand") return "";
292 commands.push(command.command);
293 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
294 },
295 });
296
297 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
298 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
299 }),
300 );
301
302 it.effect("loads PowerShell profile environment on Windows", () =>
303 Effect.gen(function* () {
304 const env: NodeJS.ProcessEnv = {
305 PATH: "C:\\Windows\\System32",
306 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
307 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
308 USERPROFILE: "C:\\Users\\testuser",
309 };
310
311 yield* runShellEnvironment({
312 env,
313 platform: "win32",
314 handler: (command) => {
315 if (command._tag !== "StandardCommand") return "";
316 const loadProfile = !command.args.includes("-NoProfile");
317 return loadProfile
318 ? envOutput({
319 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
320 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
321 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
322 })
323 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
324 },
325 });
326
327 assert.equal(
328 env.PATH,
329 [
330 "C:\\Profile\\Node",
331 "C:\\Windows\\System32",
332 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
333 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
334 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
335 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
336 "C:\\Users\\testuser\\.local\\bin",
337 "C:\\Users\\testuser\\.bun\\bin",
338 "C:\\Users\\testuser\\scoop\\shims",
339 "C:\\Custom\\Bin",
340 "C:",
341 ].join(";"),
342 );
343 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
344 assert.equal(
345 env.FNM_MULTISHELL_PATH,
346 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
347 );
348 }),
349 );
350
351 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
352 Effect.gen(function* () {
353 const env: NodeJS.ProcessEnv = {
354 SHELL: "/bin/zsh",
355 PATH: "/usr/bin",
356 XDG_CURRENT_DESKTOP: "wrong-launcher",
357 XDG_SESSION_DESKTOP: "wrong-launcher",
358 };
359
360 yield* runShellEnvironment({
361 env,
362 platform: "linux",
363 handler: () =>
364 envOutput({
365 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
366 XDG_CURRENT_DESKTOP: "KDE",
367 XDG_SESSION_DESKTOP: "KDE",
368 XDG_SESSION_TYPE: "wayland",
369 }),
370 });
371
372 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
373 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
374 assert.equal(env.XDG_SESSION_TYPE, "wayland");
375 }),
376 );
377
378 it.effect("overrides stale dbus session addresses from the login shell", () =>
379 Effect.gen(function* () {
380 const env: NodeJS.ProcessEnv = {
381 SHELL: "/bin/zsh",
382 PATH: "/usr/bin",
383 DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/stale-bus",
384 };
385
386 yield* runShellEnvironment({
387 env,
388 platform: "linux",
389 handler: () =>
390 envOutput({
391 PATH: "/usr/bin",
392 DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
393 }),
394 });
395
396 assert.equal(env.DBUS_SESSION_BUS_ADDRESS, "unix:path=/run/user/1000/bus");
397 }),
398 );
399
400 it.effect("logs command failures with safe probe context and the exact cause", () => {
401 const env: NodeJS.ProcessEnv = {
402 SHELL: "/bin/bash",
403 PATH: "/usr/bin",
404 };
405 const cause = PlatformError.systemError({
406 _tag: "PermissionDenied",
407 module: "ChildProcess",
408 method: "spawn",
409 pathOrDescriptor: "/bin/bash",
410 });
411 const messages: Array<unknown> = [];
412 const logger = Logger.make(({ message }) => {
413 messages.push(message);
414 });
415
416 return runShellEnvironment({
417 env,
418 platform: "linux",
419 handler: () => "",
420 failure: cause,
421 }).pipe(
422 Effect.andThen(
423 Effect.sync(() => {
424 const errors = messages
425 .flatMap((message) => (Array.isArray(message) ? message : [message]))
426 .filter(isDesktopShellEnvironmentCommandError);
427 assert.lengthOf(errors, 1);
428 assert.equal(errors[0]?.probe, "login-shell");
429 assert.equal(errors[0]?.executable, "bash");
430 assert.equal(errors[0]?.argumentCount, 2);
431 assert.notProperty(errors[0] ?? {}, "args");
432 assert.equal(errors[0]?.cause, cause);
433 assert.notInclude(errors[0]?.message ?? "", cause.message);
434 }),
435 ),
436 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
437 );
438 });
439});