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

DesktopShellEnvironment.test.ts

441 lines13.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/process/ChildProcess";
11import * as ChildProcessSpawner from "effect/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 layerEnvironment = Layer.succeed(
74 DesktopEnvironment.DesktopEnvironment,
75 DesktopEnvironment.DesktopEnvironment.of({
76 platform: input.platform,
77 } as DesktopEnvironment.DesktopEnvironment["Service"]),
78 );
79 const layerSpawner = 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(layerEnvironment, NodeServices.layer, layerSpawner)),
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 T3CODE_TELEMETRY_ENABLED: "false",
272 }),
273 });
274​
275 assert.equal(env.PATH, "/home/linuxbrew/.linuxbrew/bin:/usr/bin");
276 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
277 assert.equal(env.T3CODE_TELEMETRY_ENABLED, "false");
278 }),
279 );
280​
281 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
282 Effect.gen(function* () {
283 const env: NodeJS.ProcessEnv = {
284 SHELL: "/opt/homebrew/bin/nu",
285 PATH: "/usr/bin",
286 };
287 const commands: string[] = [];
288​
289 yield* runShellEnvironment({
290 env,
291 platform: "darwin",
292 handler: (command) => {
293 if (command._tag !== "StandardCommand") return "";
294 commands.push(command.command);
295 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
296 },
297 });
298​
299 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
300 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
301 }),
302 );
303​
304 it.effect("loads PowerShell profile environment on Windows", () =>
305 Effect.gen(function* () {
306 const env: NodeJS.ProcessEnv = {
307 PATH: "C:\\Windows\\System32",
308 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
309 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
310 USERPROFILE: "C:\\Users\\testuser",
311 };
312​
313 yield* runShellEnvironment({
314 env,
315 platform: "win32",
316 handler: (command) => {
317 if (command._tag !== "StandardCommand") return "";
318 const loadProfile = !command.args.includes("-NoProfile");
319 return loadProfile
320 ? envOutput({
321 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
322 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
323 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
324 })
325 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
326 },
327 });
328​
329 assert.equal(
330 env.PATH,
331 [
332 "C:\\Profile\\Node",
333 "C:\\Windows\\System32",
334 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
335 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
336 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
337 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
338 "C:\\Users\\testuser\\.local\\bin",
339 "C:\\Users\\testuser\\.bun\\bin",
340 "C:\\Users\\testuser\\scoop\\shims",
341 "C:\\Custom\\Bin",
342 "C:",
343 ].join(";"),
344 );
345 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
346 assert.equal(
347 env.FNM_MULTISHELL_PATH,
348 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
349 );
350 }),
351 );
352​
353 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
354 Effect.gen(function* () {
355 const env: NodeJS.ProcessEnv = {
356 SHELL: "/bin/zsh",
357 PATH: "/usr/bin",
358 XDG_CURRENT_DESKTOP: "wrong-launcher",
359 XDG_SESSION_DESKTOP: "wrong-launcher",
360 };
361​
362 yield* runShellEnvironment({
363 env,
364 platform: "linux",
365 handler: () =>
366 envOutput({
367 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
368 XDG_CURRENT_DESKTOP: "KDE",
369 XDG_SESSION_DESKTOP: "KDE",
370 XDG_SESSION_TYPE: "wayland",
371 }),
372 });
373​
374 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
375 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
376 assert.equal(env.XDG_SESSION_TYPE, "wayland");
377 }),
378 );
379​
380 it.effect("overrides stale dbus session addresses from the login shell", () =>
381 Effect.gen(function* () {
382 const env: NodeJS.ProcessEnv = {
383 SHELL: "/bin/zsh",
384 PATH: "/usr/bin",
385 DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/stale-bus",
386 };
387​
388 yield* runShellEnvironment({
389 env,
390 platform: "linux",
391 handler: () =>
392 envOutput({
393 PATH: "/usr/bin",
394 DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1000/bus",
395 }),
396 });
397​
398 assert.equal(env.DBUS_SESSION_BUS_ADDRESS, "unix:path=/run/user/1000/bus");
399 }),
400 );
401​
402 it.effect("logs command failures with safe probe context and the exact cause", () => {
403 const env: NodeJS.ProcessEnv = {
404 SHELL: "/bin/bash",
405 PATH: "/usr/bin",
406 };
407 const cause = PlatformError.systemError({
408 _tag: "PermissionDenied",
409 module: "ChildProcess",
410 method: "spawn",
411 pathOrDescriptor: "/bin/bash",
412 });
413 const messages: Array<unknown> = [];
414 const logger = Logger.make(({ message }) => {
415 messages.push(message);
416 });
417​
418 return runShellEnvironment({
419 env,
420 platform: "linux",
421 handler: () => "",
422 failure: cause,
423 }).pipe(
424 Effect.andThen(
425 Effect.sync(() => {
426 const errors = messages
427 .flatMap((message) => (Array.isArray(message) ? message : [message]))
428 .filter(isDesktopShellEnvironmentCommandError);
429 assert.lengthOf(errors, 1);
430 assert.equal(errors[0]?.probe, "login-shell");
431 assert.equal(errors[0]?.executable, "bash");
432 assert.equal(errors[0]?.argumentCount, 2);
433 assert.notProperty(errors[0] ?? {}, "args");
434 assert.equal(errors[0]?.cause, cause);
435 assert.notInclude(errors[0]?.message ?? "", cause.message);
436 }),
437 ),
438 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
439 );
440 });
441});