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

DesktopShellEnvironment.test.ts

502 lines15.2 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 Bitbucket credentials 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 T3CODE_BITBUCKET_ACCESS_TOKEN: "access-token",
169 T3CODE_BITBUCKET_EMAIL: "developer@example.com",
170 T3CODE_BITBUCKET_API_TOKEN: "api-token",
171 }),
172 });
173​
174 assert.equal(env.T3CODE_BITBUCKET_ACCESS_TOKEN, "access-token");
175 assert.equal(env.T3CODE_BITBUCKET_EMAIL, "developer@example.com");
176 assert.equal(env.T3CODE_BITBUCKET_API_TOKEN, "api-token");
177 }),
178 );
179​
180 it.effect("does not mix login-shell Bitbucket credentials into an inherited set", () =>
181 Effect.gen(function* () {
182 const env: NodeJS.ProcessEnv = {
183 SHELL: "/bin/zsh",
184 PATH: "/usr/bin",
185 T3CODE_BITBUCKET_ACCESS_TOKEN: "inherited-access-token",
186 };
187​
188 yield* runShellEnvironment({
189 env,
190 platform: "darwin",
191 handler: () =>
192 envOutput({
193 PATH: "/opt/homebrew/bin:/usr/bin",
194 T3CODE_BITBUCKET_EMAIL: "developer@example.com",
195 T3CODE_BITBUCKET_API_TOKEN: "api-token",
196 }),
197 });
198​
199 // An access token outranks email plus API token, so hydrating the pair here
200 // would leave the app authenticating with the inherited token and the shell
201 // credentials silently ignored.
202 assert.equal(env.T3CODE_BITBUCKET_ACCESS_TOKEN, "inherited-access-token");
203 assert.equal(env.T3CODE_BITBUCKET_EMAIL, undefined);
204 assert.equal(env.T3CODE_BITBUCKET_API_TOKEN, undefined);
205 }),
206 );
207​
208 it.effect("hydrates the locale from the login shell on macOS", () =>
209 Effect.gen(function* () {
210 const env: NodeJS.ProcessEnv = {
211 SHELL: "/bin/zsh",
212 PATH: "/usr/bin",
213 };
214​
215 yield* runShellEnvironment({
216 env,
217 platform: "darwin",
218 handler: () =>
219 envOutput({
220 PATH: "/opt/homebrew/bin:/usr/bin",
221 LANG: "de_DE.UTF-8",
222 }),
223 });
224​
225 assert.equal(env.LANG, "de_DE.UTF-8");
226 }),
227 );
228​
229 it.effect("preserves an inherited locale over the login shell on macOS", () =>
230 Effect.gen(function* () {
231 const env: NodeJS.ProcessEnv = {
232 SHELL: "/bin/zsh",
233 PATH: "/usr/bin",
234 LANG: "en_US.UTF-8",
235 };
236​
237 yield* runShellEnvironment({
238 env,
239 platform: "darwin",
240 handler: () =>
241 envOutput({
242 PATH: "/opt/homebrew/bin:/usr/bin",
243 LANG: "de_DE.UTF-8",
244 }),
245 });
246​
247 assert.equal(env.LANG, "en_US.UTF-8");
248 }),
249 );
250​
251 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
252 Effect.gen(function* () {
253 const env: NodeJS.ProcessEnv = {
254 SHELL: "/bin/zsh",
255 PATH: "/usr/bin",
256 LANG: "en_US.UTF-8",
257 };
258​
259 yield* runShellEnvironment({
260 env,
261 platform: "darwin",
262 handler: () =>
263 envOutput({
264 PATH: "/opt/homebrew/bin:/usr/bin",
265 LC_ALL: "de_DE.UTF-8",
266 }),
267 });
268​
269 assert.equal(env.LANG, "en_US.UTF-8");
270 assert.equal(env.LC_ALL, undefined);
271 }),
272 );
273​
274 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
275 Effect.gen(function* () {
276 const env: NodeJS.ProcessEnv = {
277 SHELL: "/bin/zsh",
278 PATH: "/usr/bin",
279 };
280​
281 yield* runShellEnvironment({
282 env,
283 platform: "darwin",
284 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
285 });
286​
287 assert.equal(env.LANG, undefined);
288 assert.equal(env.LC_ALL, undefined);
289 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
290 }),
291 );
292​
293 it.effect("does not apply the locale fallback on linux", () =>
294 Effect.gen(function* () {
295 const env: NodeJS.ProcessEnv = {
296 SHELL: "/bin/zsh",
297 PATH: "/usr/bin",
298 };
299​
300 yield* runShellEnvironment({
301 env,
302 platform: "linux",
303 handler: () => envOutput({ PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin" }),
304 });
305​
306 assert.equal(env.LANG, undefined);
307 }),
308 );
309​
310 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () =>
311 Effect.gen(function* () {
312 const env: NodeJS.ProcessEnv = {
313 SHELL: "/bin/zsh",
314 PATH: "/usr/bin",
315 };
316​
317 yield* runShellEnvironment({
318 env,
319 platform: "linux",
320 handler: () =>
321 envOutput({
322 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
323 SSH_AUTH_SOCK: "/tmp/secretive.sock",
324 }),
325 });
326​
327 assert.equal(env.PATH, "/home/linuxbrew/.linuxbrew/bin:/usr/bin");
328 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
329 }),
330 );
331​
332 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
333 Effect.gen(function* () {
334 const env: NodeJS.ProcessEnv = {
335 SHELL: "/opt/homebrew/bin/nu",
336 PATH: "/usr/bin",
337 };
338 const commands: string[] = [];
339​
340 yield* runShellEnvironment({
341 env,
342 platform: "darwin",
343 handler: (command) => {
344 if (command._tag !== "StandardCommand") return "";
345 commands.push(command.command);
346 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
347 },
348 });
349​
350 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
351 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
352 }),
353 );
354​
355 it.effect("loads PowerShell profile environment on Windows", () =>
356 Effect.gen(function* () {
357 const env: NodeJS.ProcessEnv = {
358 PATH: "C:\\Windows\\System32",
359 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
360 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
361 USERPROFILE: "C:\\Users\\testuser",
362 };
363​
364 yield* runShellEnvironment({
365 env,
366 platform: "win32",
367 handler: (command) => {
368 if (command._tag !== "StandardCommand") return "";
369 const loadProfile = !command.args.includes("-NoProfile");
370 return loadProfile
371 ? envOutput({
372 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
373 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
374 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
375 })
376 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
377 },
378 });
379​
380 assert.equal(
381 env.PATH,
382 [
383 "C:\\Profile\\Node",
384 "C:\\Windows\\System32",
385 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
386 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
387 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
388 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
389 "C:\\Users\\testuser\\.local\\bin",
390 "C:\\Users\\testuser\\.bun\\bin",
391 "C:\\Users\\testuser\\scoop\\shims",
392 "C:\\Custom\\Bin",
393 "C:",
394 ].join(";"),
395 );
396 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
397 assert.equal(
398 env.FNM_MULTISHELL_PATH,
399 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
400 );
401 }),
402 );
403​
404 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
405 Effect.gen(function* () {
406 const env: NodeJS.ProcessEnv = {
407 SHELL: "/bin/zsh",
408 PATH: "/usr/bin",
409 XDG_CURRENT_DESKTOP: "wrong-launcher",
410 XDG_SESSION_DESKTOP: "wrong-launcher",
411 };
412​
413 yield* runShellEnvironment({
414 env,
415 platform: "linux",
416 handler: () =>
417 envOutput({
418 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
419 XDG_CURRENT_DESKTOP: "KDE",
420 XDG_SESSION_DESKTOP: "KDE",
421 XDG_SESSION_TYPE: "wayland",
422 }),
423 });
424​
425 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
426 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
427 assert.equal(env.XDG_SESSION_TYPE, "wayland");
428 }),
429 );
430​
431 it.effect("overrides stale dbus session addresses from the login shell", () =>
432 Effect.gen(function* () {
433 const env: NodeJS.ProcessEnv = {
434 SHELL: "/bin/zsh",
435 PATH: "/usr/bin",
436 DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/stale-bus",
437 };
438​
439 yield* runShellEnvironment({
440 env,
441 platform: "linux",
442 handler: () =>
443 envOutput({
444 PATH: "/usr/bin",
445 DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
446 }),
447 });
448​
449 assert.equal(env.DBUS_SESSION_BUS_ADDRESS, "unix:path=/run/user/1000/bus");
450 }),
451 );
452​
453 it("resolves dbus runtime dir candidates with existence checks", () => {
454 const busPath = DesktopShellEnvironment.resolveDefaultLinuxDbusSessionBusAddress({
455 env: { XDG_RUNTIME_DIR: "/tmp/stale-runtime" },
456 uid: 1000,
457 exists: (path) => path === "/run/user/1000/bus",
458 });
459​
460 assert.equal(busPath, "unix:path=/run/user/1000/bus");
461 });
462​
463 it.effect("logs command failures with safe probe context and the exact cause", () => {
464 const env: NodeJS.ProcessEnv = {
465 SHELL: "/bin/bash",
466 PATH: "/usr/bin",
467 };
468 const cause = PlatformError.systemError({
469 _tag: "PermissionDenied",
470 module: "ChildProcess",
471 method: "spawn",
472 pathOrDescriptor: "/bin/bash",
473 });
474 const messages: Array<unknown> = [];
475 const logger = Logger.make(({ message }) => {
476 messages.push(message);
477 });
478​
479 return runShellEnvironment({
480 env,
481 platform: "linux",
482 handler: () => "",
483 failure: cause,
484 }).pipe(
485 Effect.andThen(
486 Effect.sync(() => {
487 const errors = messages
488 .flatMap((message) => (Array.isArray(message) ? message : [message]))
489 .filter(isDesktopShellEnvironmentCommandError);
490 assert.lengthOf(errors, 1);
491 assert.equal(errors[0]?.probe, "login-shell");
492 assert.equal(errors[0]?.executable, "bash");
493 assert.equal(errors[0]?.argumentCount, 2);
494 assert.notProperty(errors[0] ?? {}, "args");
495 assert.equal(errors[0]?.cause, cause);
496 assert.notInclude(errors[0]?.message ?? "", cause.message);
497 }),
498 ),
499 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
500 );
501 });
502});