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

DesktopShellEnvironment.test.ts

448 lines13.4 KB

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");
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 });
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 );
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);
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:\\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 ].join(";"),
341 );
342 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
343 assert.equal(
344 env.FNM_MULTISHELL_PATH,
345 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
346 );
347 }),
348 );
349​
350 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
351 Effect.gen(function* () {
352 const env: NodeJS.ProcessEnv = {
353 SHELL: "/bin/zsh",
354 PATH: "/usr/bin",
355 XDG_CURRENT_DESKTOP: "wrong-launcher",
356 XDG_SESSION_DESKTOP: "wrong-launcher",
357 };
358​
359 yield* runShellEnvironment({
360 env,
361 platform: "linux",
362 handler: () =>
363 envOutput({
364 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
365 XDG_CURRENT_DESKTOP: "KDE",
366 XDG_SESSION_DESKTOP: "KDE",
367 XDG_SESSION_TYPE: "wayland",
368 }),
369 });
370​
371 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
372 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
373 assert.equal(env.XDG_SESSION_TYPE, "wayland");
374 }),
375 );
376​
377 it.effect("overrides stale dbus session addresses from the login shell", () =>
378 Effect.gen(function* () {
379 const env: NodeJS.ProcessEnv = {
380 SHELL: "/bin/zsh",
381 PATH: "/usr/bin",
382 DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/stale-bus",
383 };
384​
385 yield* runShellEnvironment({
386 env,
387 platform: "linux",
388 handler: () =>
389 envOutput({
390 PATH: "/usr/bin",
391 DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
392 }),
393 });
394​
395 assert.equal(env.DBUS_SESSION_BUS_ADDRESS, "unix:path=/run/user/1000/bus");
396 }),
397 );
398​
399 it("resolves dbus runtime dir candidates with existence checks", () => {
400 const busPath = DesktopShellEnvironment.resolveDefaultLinuxDbusSessionBusAddress({
401 env: { XDG_RUNTIME_DIR: "/tmp/stale-runtime" },
402 uid: 1000,
403 exists: (path) => path === "/run/user/1000/bus",
404 });
405​
406 assert.equal(busPath, "unix:path=/run/user/1000/bus");
407 });
408​
409 it.effect("logs command failures with safe probe context and the exact cause", () => {
410 const env: NodeJS.ProcessEnv = {
411 SHELL: "/bin/bash",
412 PATH: "/usr/bin",
413 };
414 const cause = PlatformError.systemError({
415 _tag: "PermissionDenied",
416 module: "ChildProcess",
417 method: "spawn",
418 pathOrDescriptor: "/bin/bash",
419 });
420 const messages: Array<unknown> = [];
421 const logger = Logger.make(({ message }) => {
422 messages.push(message);
423 });
424​
425 return runShellEnvironment({
426 env,
427 platform: "linux",
428 handler: () => "",
429 failure: cause,
430 }).pipe(
431 Effect.andThen(
432 Effect.sync(() => {
433 const errors = messages
434 .flatMap((message) => (Array.isArray(message) ? message : [message]))
435 .filter(isDesktopShellEnvironmentCommandError);
436 assert.lengthOf(errors, 1);
437 assert.equal(errors[0]?.probe, "login-shell");
438 assert.equal(errors[0]?.executable, "bash");
439 assert.equal(errors[0]?.argumentCount, 2);
440 assert.notProperty(errors[0] ?? {}, "args");
441 assert.equal(errors[0]?.cause, cause);
442 assert.notInclude(errors[0]?.message ?? "", cause.message);
443 }),
444 ),
445 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
446 );
447 });
448});