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

DesktopShellEnvironment.test.ts

1,190 lines45.4 KB

1// @effect-diagnostics nodeBuiltinImport:off -- The process fixture runs real shell commands with isolated config files.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as Duration from "effect/Duration";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Deferred from "effect/Deferred";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as Logger from "effect/Logger";
12import * as PlatformError from "effect/PlatformError";
13import * as Sink from "effect/Sink";
14import * as Stream from "effect/Stream";
15import * as Schema from "effect/Schema";
16import * as Option from "effect/Option";
17import * as ChildProcess from "effect/process/ChildProcess";
18import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
19import * as TestClock from "effect/testing/TestClock";
20import * as NodeOS from "node:os";
21import { vi } from "vite-plus/test";
22​
23import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
24import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
25​
26vi.mock("node:os", { spy: true });
27​
28const textEncoder = new TextEncoder();
29const cacheCodec = Schema.fromJsonString(
30 Schema.Struct({
31 version: Schema.Number,
32 key: Schema.String,
33 environment: Schema.Record(Schema.String, Schema.NullOr(Schema.String)),
34 }),
35);
36const encodeCache = Schema.encodeSync(cacheCodec);
37const decodeCache = Schema.decodeUnknownEffect(cacheCodec);
38​
39function envOutput(values: Readonly<Record<string, string>>): string {
40 return Object.entries(values)
41 .flatMap(([name, value]) => [
42 `__T3CODE_ENV_${name}_START__`,
43 value,
44 `__T3CODE_ENV_${name}_END__`,
45 ])
46 .join("\n");
48​
49function makeProcess(output: string, exitCode = 0): ChildProcessSpawner.ChildProcessHandle {
50 const stdout = output.length === 0 ? Stream.empty : Stream.make(textEncoder.encode(output));
51 return ChildProcessSpawner.makeHandle({
52 pid: ChildProcessSpawner.ProcessId(123),
53 stdout,
54 stderr: Stream.empty,
55 all: stdout,
56 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
57 isRunning: Effect.succeed(false),
58 kill: () => Effect.void,
59 stdin: Sink.drain,
60 getInputFd: () => Sink.drain,
61 getOutputFd: () => Stream.empty,
62 unref: Effect.succeed(Effect.void),
63 });
65​
66function makeSlowProcess(
67 output: string,
68 delay: Duration.Input,
69): ChildProcessSpawner.ChildProcessHandle {
70 const stdout = Stream.fromEffect(Effect.as(Effect.sleep(delay), textEncoder.encode(output)));
71 return ChildProcessSpawner.makeHandle({
72 pid: ChildProcessSpawner.ProcessId(125),
73 stdout,
74 stderr: Stream.empty,
75 all: stdout,
76 exitCode: Effect.as(Effect.sleep(delay), ChildProcessSpawner.ExitCode(0)),
77 isRunning: Effect.succeed(true),
78 kill: () => Effect.void,
79 stdin: Sink.drain,
80 getInputFd: () => Sink.drain,
81 getOutputFd: () => Stream.empty,
82 unref: Effect.succeed(Effect.void),
83 });
85​
86function makeHungProcess(): ChildProcessSpawner.ChildProcessHandle {
87 return ChildProcessSpawner.makeHandle({
88 pid: ChildProcessSpawner.ProcessId(124),
89 stdout: Stream.never,
90 stderr: Stream.never,
91 all: Stream.never,
92 exitCode: Effect.never,
93 isRunning: Effect.succeed(true),
94 kill: () => Effect.never,
95 stdin: Sink.drain,
96 getInputFd: () => Sink.drain,
97 getOutputFd: () => Stream.empty,
98 unref: Effect.succeed(Effect.void),
99 });
101​
102function withProcessEnv<A, E, R>(
103 env: NodeJS.ProcessEnv,
104 effect: Effect.Effect<A, E, R>,
105): Effect.Effect<A, E, R> {
106 return Effect.acquireUseRelease(
107 Effect.sync(() => {
108 const previous = process.env;
109 process.env = env;
110 return previous;
111 }),
112 () => effect,
113 (previous) =>
114 Effect.sync(() => {
115 process.env = previous;
116 }),
117 );
119​
120function runShellEnvironment(input: {
121 readonly env: NodeJS.ProcessEnv;
122 readonly platform: NodeJS.Platform;
123 readonly userShell?: string;
124 readonly stateDir?: string;
125 readonly use?: (
126 service: DesktopShellEnvironment.DesktopShellEnvironment["Service"],
127 ) => Effect.Effect<boolean, never, import("effect/Scope").Scope>;
128 readonly handler: (
129 command: ChildProcess.Command,
130 ) => string | { output: string; exitCode: number };
131 readonly failure?: PlatformError.PlatformError;
132 readonly processSpawner?: ChildProcessSpawner.ChildProcessSpawner["Service"];
133}) {
134 const layerEnvironment = Layer.effect(
135 DesktopEnvironment.DesktopEnvironment,
136 Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const stateDir = input.stateDir ?? (yield* fs.makeTempDirectoryScoped());
139 return DesktopEnvironment.DesktopEnvironment.of({
140 platform: input.platform,
141 stateDir,
142 homeDirectory: "/test-home",
143 } as DesktopEnvironment.DesktopEnvironment["Service"]);
144 }),
145 ).pipe(Layer.provide(NodeServices.layer));
146 const layerSpawner = Layer.succeed(
147 ChildProcessSpawner.ChildProcessSpawner,
148 ChildProcessSpawner.make((command) => {
149 if (input.processSpawner) return input.processSpawner.spawn(command);
150 if (input.failure !== undefined) return Effect.fail(input.failure);
151 const result = input.handler(command);
152 return Effect.succeed(
153 typeof result === "string"
154 ? makeProcess(result)
155 : makeProcess(result.output, result.exitCode),
156 );
157 }),
158 );
159​
160 const program = Effect.gen(function* () {
161 const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
162 if (input.use) return yield* input.use(shellEnvironment);
163 const prepared = yield* shellEnvironment.awaitDiscovery;
164 assert.equal(yield* shellEnvironment.awaitDiscovery, prepared);
165 return prepared;
166 }).pipe(
167 Effect.provide(
168 DesktopShellEnvironment.layer.pipe(
169 Layer.provide(Layer.mergeAll(layerEnvironment, NodeServices.layer, layerSpawner)),
170 ),
171 ),
172 );
173​
174 return Effect.acquireUseRelease(
175 Effect.sync(() =>
176 vi.spyOn(NodeOS, "userInfo").mockReturnValue({
177 ...NodeOS.userInfo(),
178 shell: input.userShell ?? null,
179 }),
180 ),
181 () => withProcessEnv(input.env, program),
182 (spy) => Effect.sync(() => spy.mockRestore()),
183 );
185​
186describe("DesktopShellEnvironment", () => {
187 it.effect.each([
188 { platform: "linux" as const, runtime: { APPDIR: "/opt/t3.AppDir/" }, scrub: true },
189 { platform: "linux" as const, runtime: { APPIMAGE: "/opt/T3.AppImage" }, scrub: true },
190 { platform: "linux" as const, runtime: {}, scrub: false },
191 { platform: "darwin" as const, runtime: { APPDIR: "/opt/t3.AppDir/" }, scrub: false },
192 ])(
193 "keeps AppImage mounts out of cached and discovered PATH: %j",
194 ({ platform, runtime, scrub }) =>
195 Effect.gen(function* () {
196 const fs = yield* FileSystem.FileSystem;
197 const stateDir = yield* fs.makeTempDirectoryScoped();
198 const cachePath = `${stateDir}/shell-environment-cache.json`;
199 const inheritedPath =
200 "/tmp/.mount_T3-Codold123/usr/bin:/launcher/bin:/opt/t3.AppDir/usr/bin";
201 const cachedPath = "/cached/bin:/tmp/.mount_T3-Codcache1/usr/bin";
202 const discoveredPath = "/fresh/bin:/tmp/.mount_T3-Codprobe1/usr/bin";
203 const retainedPath = "/tmp/tools:/tmp/.mount_Other123/usr/bin:/opt/t3.AppDir-backup/bin";
204 const cleanInherited = scrub
205 ? `/launcher/bin${runtime.APPDIR ? "" : ":/opt/t3.AppDir/usr/bin"}`
206 : inheritedPath;
207 const cleanCached = scrub ? "/cached/bin" : cachedPath;
208 const cleanDiscovered = scrub ? "/fresh/bin" : discoveredPath;
209 const env: NodeJS.ProcessEnv = {
210 ...runtime,
211 SHELL: "/bin/bash",
212 PATH: `${inheritedPath}:${retainedPath}`,
213 };
214 yield* fs.writeFileString(
215 cachePath,
216 encodeCache({
217 version: 1,
218 key: JSON.stringify([platform, "/test-home", "/bin/bash", ""]),
219 environment: { PATH: cachedPath, __MISE_DIFF: "cached-mise" },
220 }),
221 );
222 const started = yield* Deferred.make<void>();
223 const release = yield* Deferred.make<void>();
224 const probePath = yield* Deferred.make<string | undefined>();
225 yield* runShellEnvironment({
226 env,
227 platform,
228 stateDir,
229 handler: () => "",
230 processSpawner: ChildProcessSpawner.make((command) =>
231 Effect.gen(function* () {
232 const value =
233 command._tag === "StandardCommand" ? command.options.env?.PATH : undefined;
234 const path = typeof value === "string" ? value : undefined;
235 yield* Deferred.succeed(probePath, path);
236 yield* Deferred.succeed(started, undefined);
237 yield* Deferred.await(release);
238 return makeProcess(
239 envOutput({ PATH: `${discoveredPath}:${path}`, __MISE_DIFF: "fresh-mise" }),
240 );
241 }),
242 ),
243 use: (service) =>
244 Effect.gen(function* () {
245 yield* service.installIntoProcess;
246 const expectedCached = `${cleanCached}:${cleanInherited}:${retainedPath}`;
247 assert.equal(env.PATH, expectedCached);
248 assert.equal(env.__MISE_DIFF, "cached-mise");
249 const cached = yield* Stream.runHead(service.updates);
250 assert.equal(Option.getOrThrow(cached).environment.PATH, expectedCached);
251 yield* Deferred.await(started);
252 assert.equal(yield* Deferred.await(probePath), `${cleanInherited}:${retainedPath}`);
253 yield* Deferred.succeed(release, undefined);
254 yield* service.awaitDiscovery;
255 const expectedFresh = `${cleanDiscovered}:${cleanInherited}:${retainedPath}`;
256 assert.equal(env.PATH, expectedFresh);
257 assert.equal(env.__MISE_DIFF, "fresh-mise");
258 const fresh = yield* Stream.runHead(service.updates);
259 assert.equal(Option.getOrThrow(fresh).environment.PATH, expectedFresh);
260 return true;
261 }),
262 });
263 const cached = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
264 assert.equal(cached.environment.PATH, env.PATH);
265 assert.equal(cached.environment.__MISE_DIFF, "fresh-mise");
266 }).pipe(Effect.provide(NodeServices.layer)),
267 );
268​
269 it.effect.each([
270 { platform: "darwin" as const, env: { PATH: "/usr/bin" }, expectedPath: "/usr/bin" },
271 {
272 platform: "linux" as const,
273 env: { APPDIR: "/tmp/.mount_T3-Codnew123", PATH: ":/tmp/.mount_T3-Codold123/bin::/usr/bin:" },
274 expectedPath: "::/usr/bin:",
275 },
276 {
277 platform: "linux" as const,
278 env: { APPDIR: "/tmp/.mount_T3-Codnew123", PATH: "/tmp/.mount_T3-Codnew123/bin" },
279 expectedPath: undefined,
280 },
281 ])(
282 "does not wait for discovery when the cache is missing: %j",
283 ({ platform, env: initial, expectedPath }) =>
284 Effect.gen(function* () {
285 const started = yield* Deferred.make<void>();
286 const release = yield* Deferred.make<void>();
287 const env: NodeJS.ProcessEnv = { SHELL: "/test-shell", ...initial };
288 let probePath: string | undefined;
289 yield* runShellEnvironment({
290 env,
291 platform,
292 handler: () => "",
293 processSpawner: ChildProcessSpawner.make((command) =>
294 Effect.gen(function* () {
295 const value =
296 command._tag === "StandardCommand" ? command.options.env?.PATH : undefined;
297 probePath = typeof value === "string" ? value : undefined;
298 yield* Deferred.succeed(started, undefined);
299 yield* Deferred.await(release);
300 return makeProcess(envOutput({ PATH: "/fresh/bin" }));
301 }),
302 ),
303 use: (service) =>
304 Effect.gen(function* () {
305 const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
306 yield* Deferred.await(started);
307 assert.isDefined(startup.pollUnsafe());
308 assert.equal(env.PATH, expectedPath);
309 assert.equal(probePath, expectedPath);
310 yield* Deferred.succeed(release, undefined);
311 return yield* service.awaitDiscovery;
312 }),
313 });
314 }),
315 );
316​
317 it.effect(
318 "reuses cached paths immediately and streams fresh discovery without keeping removed paths",
319 () =>
320 Effect.gen(function* () {
321 const fs = yield* FileSystem.FileSystem;
322 const stateDir = yield* fs.makeTempDirectoryScoped();
323 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner.pipe(
324 Effect.provide(NodeServices.layer),
325 );
326 const executable = yield* HostProcess.ExecutablePath;
327 const first = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
328 yield* runShellEnvironment({
329 env: first,
330 stateDir,
331 platform: "darwin",
332 handler: () =>
333 envOutput({
334 PATH: "/old/bin:/usr/bin",
335 __MISE_DIFF: "old-mise",
336 SSH_AUTH_SOCK: "/old-session.sock",
337 JAVA_HOME: "/old/java",
338 PNPM_HOME: "/old/pnpm",
339 SSL_CERT_FILE: "/old/cert.pem",
340 }),
341 });
342 const cachePath = `${stateDir}/shell-environment-cache.json`;
343 const cached = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
344 assert.equal(cached.environment.PATH, "/old/bin:/usr/bin");
345 assert.equal(cached.environment.PNPM_HOME, "/old/pnpm");
346 assert.notProperty(cached.environment, "SSH_AUTH_SOCK");
347 const started = yield* Deferred.make<void>();
348 const release = yield* Deferred.make<void>();
349 const env: NodeJS.ProcessEnv = {
350 SHELL: "/bin/zsh",
351 PATH: "/launcher/bin",
352 SSH_AUTH_SOCK: "/current-session.sock",
353 };
354 const source = [
355 "const values = { PATH: '/fresh/bin:' + process.env.PATH, __MISE_DIFF: 'fresh-mise', JAVA_HOME: process.env.JAVA_HOME, SSL_CERT_FILE: process.env.SSL_CERT_FILE };",
356 "for (const [name, value] of Object.entries(values)) {",
357 " if (value !== undefined) process.stdout.write('__T3CODE_ENV_' + name + '_START__\\n' + value + '\\n__T3CODE_ENV_' + name + '_END__\\n');",
358 "}",
359 ].join("\n");
360 let calls = 0;
361 yield* runShellEnvironment({
362 env,
363 stateDir,
364 platform: "darwin",
365 handler: () => "",
366 processSpawner: ChildProcessSpawner.make((command) => {
367 calls++;
368 return Deferred.succeed(started, undefined).pipe(
369 Effect.andThen(Deferred.await(release)),
370 Effect.andThen(
371 command._tag === "StandardCommand"
372 ? spawner.spawn(ChildProcess.make(executable, ["-e", source], command.options))
373 : Effect.succeed(makeProcess("")),
374 ),
375 );
376 }),
377 use: (service) =>
378 Effect.gen(function* () {
379 yield* service.installIntoProcess;
380 yield* service.installIntoProcess;
381 assert.equal(env.PATH, "/old/bin:/usr/bin:/launcher/bin");
382 assert.equal(env.SSH_AUTH_SOCK, "/current-session.sock");
383 const replay = yield* Stream.runHead(service.updates);
384 assert.isTrue(Option.isSome(replay));
385 if (Option.isSome(replay))
386 assert.equal(replay.value.environment.__MISE_DIFF, "old-mise");
387 yield* Deferred.await(started);
388 assert.equal(calls, 1);
389 yield* Deferred.succeed(release, undefined);
390 yield* service.awaitDiscovery;
391 assert.equal(env.PATH, "/fresh/bin:/launcher/bin");
392 assert.equal(env.__MISE_DIFF, "fresh-mise");
393 assert.isUndefined(env.JAVA_HOME);
394 assert.isUndefined(env.PNPM_HOME);
395 assert.isUndefined(env.SSL_CERT_FILE);
396 const fresh = yield* Stream.runHead(service.updates);
397 assert.isTrue(Option.isSome(fresh));
398 if (Option.isSome(fresh)) {
399 assert.equal(fresh.value.environment.PATH, env.PATH);
400 assert.isTrue(fresh.value.discovered);
401 }
402 return true;
403 }),
404 });
405 const refreshed = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
406 assert.equal(refreshed.environment.PATH, "/fresh/bin:/launcher/bin");
407 assert.notProperty(refreshed.environment, "JAVA_HOME");
408 assert.notProperty(refreshed.environment, "PNPM_HOME");
409 assert.notProperty(refreshed.environment, "SSL_CERT_FILE");
410 }).pipe(Effect.provide(NodeServices.layer)),
411 );
412​
413 it.effect.each([
414 "not json",
415 '{"version":2,"key":"old","environment":{"PATH":"/bad/bin"}}',
416 '{"version":1,"key":"other-shell","environment":{"PATH":"/bad/bin"}}',
417 ])("ignores an unusable cache and still starts before discovery: %s", (contents) =>
418 Effect.gen(function* () {
419 const fs = yield* FileSystem.FileSystem;
420 const stateDir = yield* fs.makeTempDirectoryScoped();
421 yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents);
422 const started = yield* Deferred.make<void>();
423 const release = yield* Deferred.make<void>();
424 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
425 yield* runShellEnvironment({
426 env,
427 stateDir,
428 platform: "darwin",
429 handler: () => "",
430 processSpawner: ChildProcessSpawner.make(() =>
431 Deferred.succeed(started, undefined).pipe(
432 Effect.andThen(Deferred.await(release)),
433 Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))),
434 ),
435 ),
436 use: (service) =>
437 Effect.gen(function* () {
438 const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
439 yield* Deferred.await(started);
440 assert.isDefined(startup.pollUnsafe());
441 assert.equal(env.PATH, "/usr/bin");
442 yield* Deferred.succeed(release, undefined);
443 return yield* service.awaitDiscovery;
444 }),
445 });
446 assert.equal(env.PATH, "/fresh/bin:/usr/bin");
447 }).pipe(Effect.provide(NodeServices.layer)),
448 );
449​
450 it.effect(
451 "retains the last successful cache on refresh failure and ignores session values in the cache",
452 () =>
453 Effect.gen(function* () {
454 const fs = yield* FileSystem.FileSystem;
455 const stateDir = yield* fs.makeTempDirectoryScoped();
456 const contents = encodeCache({
457 version: 1,
458 key: '["darwin","/test-home","/bin/zsh",""]',
459 environment: {
460 PATH: "/cached/bin",
461 __MISE_DIFF: "cached-mise",
462 JAVA_HOME: "/cached/java",
463 PNPM_HOME: "/cached/pnpm",
464 NODE_EXTRA_CA_CERTS: "/cached/ca.pem",
465 SSL_CERT_FILE: "/cached/cert.pem",
466 SSL_CERT_DIR: "/cached/certs",
467 T3CODE_TELEMETRY_ENABLED: "false",
468 SSH_AUTH_SOCK: "/stale.sock",
469 NODE_OPTIONS: "--bad",
470 },
471 });
472 const cachePath = `${stateDir}/shell-environment-cache.json`;
473 yield* fs.writeFileString(cachePath, contents);
474 const env: NodeJS.ProcessEnv = {
475 SHELL: "/bin/zsh",
476 PATH: "/usr/bin",
477 __MISE_DIFF: "launcher-mise",
478 JAVA_HOME: "/launcher/java",
479 NODE_EXTRA_CA_CERTS: "/launcher/ca.pem",
480 SSL_CERT_FILE: "",
481 SSH_AUTH_SOCK: "/current.sock",
482 };
483 const prepared = yield* runShellEnvironment({
484 env,
485 stateDir,
486 platform: "darwin",
487 handler: () => "",
488 use: (service) =>
489 Effect.gen(function* () {
490 yield* service.installIntoProcess;
491 const replay = yield* Stream.runHead(service.updates);
492 assert.isTrue(Option.isSome(replay));
493 if (Option.isSome(replay)) {
494 assert.equal(replay.value.environment.JAVA_HOME, "/launcher/java");
495 assert.equal(replay.value.environment.NODE_EXTRA_CA_CERTS, "/launcher/ca.pem");
496 }
497 return yield* service.awaitDiscovery;
498 }),
499 });
500 assert.isFalse(prepared);
501 assert.equal(env.PATH, "/cached/bin:/usr/bin");
502 assert.equal(env.__MISE_DIFF, "cached-mise");
503 assert.equal(env.JAVA_HOME, "/launcher/java");
504 assert.equal(env.PNPM_HOME, "/cached/pnpm");
505 assert.equal(env.NODE_EXTRA_CA_CERTS, "/launcher/ca.pem");
506 assert.equal(env.SSL_CERT_FILE, "");
507 assert.equal(env.SSL_CERT_DIR, "/cached/certs");
508 assert.equal(env.T3CODE_TELEMETRY_ENABLED, "false");
509 assert.equal(env.SSH_AUTH_SOCK, "/current.sock");
510 assert.isUndefined(env.NODE_OPTIONS);
511 assert.equal(yield* fs.readFileString(cachePath), contents);
512 }).pipe(Effect.provide(NodeServices.layer)),
513 );
514​
515 it.effect("cancels background discovery when the application scope closes", () =>
516 Effect.gen(function* () {
517 const started = yield* Deferred.make<void>();
518 const stopped = yield* Deferred.make<void>();
519 yield* runShellEnvironment({
520 env: { SHELL: "/bin/zsh", PATH: "/usr/bin" },
521 platform: "darwin",
522 handler: () => "",
523 processSpawner: ChildProcessSpawner.make(() =>
524 Deferred.succeed(started, undefined).pipe(
525 Effect.andThen(Effect.never),
526 Effect.ensuring(Deferred.succeed(stopped, undefined)),
527 ),
528 ),
529 use: (service) =>
530 Effect.gen(function* () {
531 yield* service.installIntoProcess;
532 yield* Deferred.await(started);
533 return true;
534 }),
535 });
536 yield* Deferred.await(stopped);
537 }),
538 );
539​
540 it.effect(
541 "captures the login-shell PATH when startup writes more than a pipe buffer to stderr",
542 () =>
543 Effect.gen(function* () {
544 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner.pipe(
545 Effect.provide(NodeServices.layer),
546 );
547 const executable = yield* HostProcess.ExecutablePath;
548 const env: NodeJS.ProcessEnv = { SHELL: "/test-shell", PATH: "/usr/bin" };
549 const commands: string[] = [];
550 const source = [
551 "setTimeout(() => process.exit(99), 3000).unref();",
552 "process.stderr.write(Buffer.alloc(2 * 1024 * 1024, 120), () => {",
553 `process.stdout.write(${JSON.stringify(envOutput({ PATH: "/shell-only/bin:/usr/bin" }))});`,
554 "});",
555 ].join("\n");
556​
557 yield* runShellEnvironment({
558 env,
559 platform: "linux",
560 handler: () => "",
561 processSpawner: ChildProcessSpawner.make((command) => {
562 if (command._tag !== "StandardCommand") return Effect.succeed(makeProcess(""));
563 commands.push(command.command);
564 return command.command === "/test-shell"
565 ? spawner.spawn(ChildProcess.make(executable, ["-e", source], command.options))
566 : Effect.succeed(makeProcess(""));
567 }),
568 }).pipe(Effect.provide(Logger.layer([])));
569​
570 assert.equal(env.PATH, "/shell-only/bin:/usr/bin");
571 assert.deepEqual(commands, ["/test-shell"]);
572 }),
573 );
574​
575 it.effect("rejects PATH output from a failed shell and tries the next candidate", () =>
576 Effect.gen(function* () {
577 const env: NodeJS.ProcessEnv = { SHELL: "/bin/failing", PATH: "/usr/bin" };
578 const commands: string[] = [];
579​
580 const discovery = (service: DesktopShellEnvironment.DesktopShellEnvironment["Service"]) =>
581 Effect.all([service.awaitDiscovery, service.awaitUserShellDiscovery]);
582 const answered = yield* runShellEnvironment({
583 env,
584 platform: "linux",
585 userShell: "/usr/local/bin/fish",
586 use: (service) => discovery(service).pipe(Effect.map(([, fromUserShell]) => fromUserShell)),
587 handler: (command) => {
588 if (command._tag !== "StandardCommand") return "";
589 commands.push(command.command);
590 return command.command === "/bin/failing"
591 ? { output: envOutput({ PATH: "/failed/bin" }), exitCode: 7 }
592 : envOutput({ PATH: "/home/test/.local/bin:/usr/bin" });
593 },
594 }).pipe(Effect.provide(Logger.layer([])));
595​
596 assert.deepEqual(commands, ["/bin/failing", "/usr/local/bin/fish"]);
597 assert.equal(env.PATH, "/home/test/.local/bin:/usr/bin");
598 // The account's own login shell answered.
599 assert.isTrue(answered);
600​
601 // Only the platform fallback answered: its PATH can miss folders the user's shell adds.
602 const fallbackOnly = yield* runShellEnvironment({
603 env: { SHELL: "/usr/bin/fish", PATH: "/usr/bin" },
604 platform: "linux",
605 use: (service) =>
606 discovery(service).pipe(
607 Effect.map(([prepared, fromUserShell]) => prepared && !fromUserShell),
608 ),
609 handler: (command) =>
610 command._tag === "StandardCommand" && command.command === "/usr/bin/fish"
611 ? { output: "", exitCode: 7 }
612 : envOutput({ PATH: "/usr/bin" }),
613 }).pipe(Effect.provide(Logger.layer([])));
614 assert.isTrue(fallbackOnly);
615 }),
616 );
617​
618 it.effect("hydrates PATH, SSH_AUTH_SOCK, and DO_NOT_TRACK from the login shell on macOS", () =>
619 Effect.gen(function* () {
620 const env: NodeJS.ProcessEnv = {
621 SHELL: "/bin/zsh",
622 PATH: " /Users/test/.local/bin:/usr/bin",
623 };
624 const commands: ChildProcess.Command[] = [];
625​
626 const prepared = yield* runShellEnvironment({
627 env,
628 platform: "darwin",
629 handler: (command) => {
630 commands.push(command);
631 return envOutput({
632 PATH: "/opt/homebrew/bin:/usr/bin:/opt/bin ",
633 SSH_AUTH_SOCK: "/tmp/secretive.sock",
634 HOMEBREW_PREFIX: "/opt/homebrew",
635 DO_NOT_TRACK: "1",
636 });
637 },
638 });
639​
640 assert.equal(commands.length, 1);
641 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
642 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/opt/bin : /Users/test/.local/bin");
643 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
644 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
645 assert.equal(env.DO_NOT_TRACK, "1");
646 assert.isTrue(prepared);
647 }),
648 );
649​
650 it.effect("keeps login-shell PATH entries that contain the capture end marker", () =>
651 Effect.gen(function* () {
652 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
653​
654 yield* runShellEnvironment({
655 env,
656 platform: "darwin",
657 handler: () => envOutput({ PATH: "/opt/__T3CODE_ENV_PATH_END__/bin:/usr/bin" }),
658 });
659​
660 assert.equal(env.PATH, "/opt/__T3CODE_ENV_PATH_END__/bin:/usr/bin");
661 }),
662 );
663​
664 it.effect("does not mix login-shell locale categories into an inherited locale", () =>
665 Effect.gen(function* () {
666 const env: NodeJS.ProcessEnv = {
667 SHELL: "/bin/zsh",
668 PATH: "/usr/bin",
669 LANG: "en_US.UTF-8",
670 };
671​
672 yield* runShellEnvironment({
673 env,
674 platform: "darwin",
675 handler: () =>
676 envOutput({
677 PATH: "/opt/homebrew/bin:/usr/bin",
678 LC_ALL: "de_DE.UTF-8",
679 }),
680 });
681​
682 assert.equal(env.LANG, "en_US.UTF-8");
683 assert.equal(env.LC_ALL, undefined);
684 }),
685 );
686​
687 it.effect("falls back to a UTF-8 LC_CTYPE when no locale is available on macOS", () =>
688 Effect.gen(function* () {
689 const env: NodeJS.ProcessEnv = {
690 SHELL: "/bin/zsh",
691 PATH: "/usr/bin",
692 };
693​
694 yield* runShellEnvironment({
695 env,
696 platform: "darwin",
697 handler: () => envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" }),
698 });
699​
700 assert.equal(env.LANG, undefined);
701 assert.equal(env.LC_ALL, undefined);
702 assert.equal(env.LC_CTYPE, "en_US.UTF-8");
703 }),
704 );
705​
706 it.effect("hydrates missing CA trust variables from the login shell", () =>
707 Effect.gen(function* () {
708 const env: NodeJS.ProcessEnv = {
709 SHELL: "/bin/zsh",
710 PATH: "/usr/bin",
711 SSL_CERT_DIR: "/etc/ssl/certs",
712 };
713​
714 yield* runShellEnvironment({
715 env,
716 platform: "darwin",
717 handler: () =>
718 envOutput({
719 PATH: "/opt/zerobrew/bin:/usr/bin",
720 SSL_CERT_FILE: "/opt/zerobrew/etc/ca-certificates/cacert.pem",
721 SSL_CERT_DIR: "/opt/zerobrew/etc/ca-certificates",
722 NODE_EXTRA_CA_CERTS: "/Users/me/corp-root.pem",
723 }),
724 });
725​
726 assert.equal(env.SSL_CERT_FILE, "/opt/zerobrew/etc/ca-certificates/cacert.pem");
727 assert.equal(env.SSL_CERT_DIR, "/etc/ssl/certs");
728 assert.equal(env.NODE_EXTRA_CA_CERTS, "/Users/me/corp-root.pem");
729 }),
730 );
731​
732 it.effect("hydrates a missing Azure CLI config directory from the login shell", () =>
733 Effect.gen(function* () {
734 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
735​
736 yield* runShellEnvironment({
737 env,
738 platform: "darwin",
739 handler: () =>
740 envOutput({
741 PATH: "/usr/bin",
742 AZURE_CONFIG_DIR: "/Users/me/.local/share/azure",
743 }),
744 });
745​
746 assert.equal(env.AZURE_CONFIG_DIR, "/Users/me/.local/share/azure");
747 }),
748 );
749​
750 it.effect.each([
751 {
752 name: "hydrates both Android SDK names when neither is inherited",
753 inherited: {},
754 expected: { ANDROID_HOME: "/shell/android-home", ANDROID_SDK_ROOT: "/shell/android-root" },
755 },
756 {
757 // The device host reads ANDROID_HOME before ANDROID_SDK_ROOT, so a shell
758 // ANDROID_HOME next to an inherited ANDROID_SDK_ROOT would change the SDK.
759 name: "keeps an inherited ANDROID_SDK_ROOT selected over a login-shell ANDROID_HOME",
760 inherited: { ANDROID_SDK_ROOT: "/inherited/android-sdk" },
761 expected: { ANDROID_HOME: undefined, ANDROID_SDK_ROOT: "/inherited/android-sdk" },
762 },
763 ])("$name", ({ inherited, expected }) =>
764 Effect.gen(function* () {
765 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin", ...inherited };
766​
767 yield* runShellEnvironment({
768 env,
769 platform: "darwin",
770 handler: () =>
771 envOutput({
772 PATH: "/usr/bin",
773 ANDROID_HOME: "/shell/android-home",
774 ANDROID_SDK_ROOT: "/shell/android-root",
775 }),
776 });
777​
778 assert.equal(env.ANDROID_HOME, expected.ANDROID_HOME);
779 assert.equal(env.ANDROID_SDK_ROOT, expected.ANDROID_SDK_ROOT);
780 }),
781 );
782​
783 it.effect("keeps mise's PATH record with the login shell PATH", () =>
784 Effect.gen(function* () {
785 const env: NodeJS.ProcessEnv = {
786 SHELL: "/bin/bash",
787 PATH: "/usr/bin",
788 };
789​
790 yield* runShellEnvironment({
791 env,
792 platform: "linux",
793 handler: () =>
794 envOutput({
795 PATH: "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
796 __MISE_DIFF: "mise-diff",
797 }),
798 });
799​
800 assert.equal(env.__MISE_DIFF, "mise-diff");
801 }),
802 );
803​
804 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
805 Effect.gen(function* () {
806 const env: NodeJS.ProcessEnv = {
807 SHELL: "/opt/homebrew/bin/nu",
808 PATH: "/usr/bin",
809 };
810 const commands: string[] = [];
811​
812 const prepared = yield* runShellEnvironment({
813 env,
814 platform: "darwin",
815 handler: (command) => {
816 if (command._tag !== "StandardCommand") return "";
817 commands.push(command.command);
818 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
819 },
820 });
821​
822 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
823 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
824 assert.isFalse(prepared);
825 }),
826 );
827​
828 it.effect("leaves macOS preparation incomplete when no login shell returns a PATH", () =>
829 Effect.gen(function* () {
830 const prepared = yield* runShellEnvironment({
831 env: { SHELL: "/bin/zsh", PATH: "/usr/bin:/bin:/usr/sbin:/sbin" },
832 platform: "darwin",
833 handler: () => "",
834 });
835​
836 assert.isFalse(prepared);
837 }),
838 );
839​
840 it.effect("retries the user's shell when only the fallback shell answered", () =>
841 Effect.gen(function* () {
842 const env: NodeJS.ProcessEnv = { SHELL: "/usr/bin/fish", PATH: "/usr/bin" };
843 const commands: string[] = [];
844 let fishReady = false;
845 yield* runShellEnvironment({
846 env,
847 platform: "linux",
848 handler: (command) => {
849 if (command._tag !== "StandardCommand") return "";
850 commands.push(command.command);
851 if (command.command !== "/usr/bin/fish") return envOutput({ PATH: "/usr/bin" });
852 return fishReady
853 ? envOutput({ PATH: "/fish/bin:/usr/bin" })
854 : { output: "", exitCode: 7 };
855 },
856 use: (service) =>
857 Effect.gen(function* () {
858 assert.isTrue(yield* service.awaitDiscovery);
859 assert.equal(env.PATH, "/usr/bin");
860 // The user's shell may still opt out of telemetry, so discovery has not settled.
861 assert.isFalse(yield* service.discoveryFinished);
862 // The Linux probe checks the D-Bus socket on the real filesystem, which the clock does not await.
863 const published = yield* service.updates.pipe(
864 Stream.filter((snapshot) => snapshot.environment.PATH === "/fish/bin:/usr/bin"),
865 Stream.runHead,
866 Effect.forkChild({ startImmediately: true }),
867 );
868​
869 fishReady = true;
870 yield* TestClock.adjust("30 seconds");
871 assert.isTrue(Option.isSome(yield* Fiber.join(published)));
872 assert.equal(env.PATH, "/fish/bin:/usr/bin");
873 return true;
874 }),
875 }).pipe(Effect.provide(Logger.layer([])));
876 assert.deepEqual(commands, ["/usr/bin/fish", "/bin/bash", "/usr/bin/fish"]);
877​
878 // With no user shell to wait for, the fallback's answer is final.
879 commands.length = 0;
880 yield* runShellEnvironment({
881 env: { PATH: "/usr/bin" },
882 platform: "linux",
883 handler: (command) => {
884 if (command._tag === "StandardCommand") commands.push(command.command);
885 return envOutput({ PATH: "/usr/bin" });
886 },
887 use: (service) =>
888 Effect.gen(function* () {
889 assert.isTrue(yield* service.awaitDiscovery);
890 yield* TestClock.adjust("5 minutes");
891 return true;
892 }),
893 }).pipe(Effect.provide(Logger.layer([])));
894 assert.deepEqual(commands, ["/bin/bash"]);
895 }),
896 );
897​
898 it.effect("gives background retries time for a locked or slow shell, then publishes", () =>
899 Effect.gen(function* () {
900 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
901 // Startup fails fast, the first retry meets a shell blocked on a lock, the second a slow one.
902 const spawned = yield* Effect.forEach([0, 1, 2], () => Deferred.make<void>());
903 const stopped = yield* Deferred.make<ChildProcess.KillOptions>();
904 let attempts = 0;
905 yield* runShellEnvironment({
906 env,
907 platform: "darwin",
908 handler: () => "",
909 processSpawner: ChildProcessSpawner.make((command) => {
910 if (command._tag !== "StandardCommand" || command.command !== "/bin/zsh") {
911 return Effect.succeed(makeProcess(""));
912 }
913 const attempt = attempts++;
914 const spawn = Deferred.succeed(spawned[attempt]!, undefined);
915 if (attempt === 0) return spawn.pipe(Effect.as(makeProcess("")));
916 if (attempt === 1) {
917 return spawn.pipe(
918 Effect.andThen(
919 Effect.acquireRelease(Effect.succeed(makeHungProcess()), () =>
920 Deferred.succeed(stopped, command.options),
921 ),
922 ),
923 );
924 }
925 return spawn.pipe(
926 Effect.as(makeSlowProcess(envOutput({ PATH: "/fresh/bin" }), "20 seconds")),
927 );
928 }),
929 use: (service) =>
930 Effect.gen(function* () {
931 assert.isFalse(yield* service.awaitDiscovery);
932 const published = yield* service.updates.pipe(
933 Stream.drop(1),
934 Stream.runHead,
935 Effect.forkChild({ startImmediately: true }),
936 );
937​
938 yield* TestClock.adjust("30 seconds");
939 yield* Deferred.await(spawned[1]!);
940 // pyenv waits up to 60 s on its rehash lock before the shell continues.
941 yield* TestClock.adjust("1 minute");
942 assert.isFalse(yield* Deferred.isDone(stopped));
943 yield* TestClock.adjust("1 minute");
944 const stop = yield* Deferred.await(stopped);
945 // pyenv releases that lock in an EXIT trap, which SIGTERM runs and SIGKILL skips.
946 assert.equal(stop.killSignal, "SIGTERM");
947 assert.isAtLeast(Duration.toMillis(stop.forceKillAfter ?? 0), 5_000);
948​
949 yield* TestClock.adjust("30 seconds");
950 yield* Deferred.await(spawned[2]!);
951 yield* TestClock.adjust("20 seconds");
952 const snapshot = yield* Fiber.join(published);
953 assert.equal(env.PATH, "/fresh/bin:/usr/bin");
954 assert.isTrue(Option.isSome(snapshot) && snapshot.value.discovered);
955 assert.equal(
956 Option.isSome(snapshot) ? snapshot.value.environment.PATH : undefined,
957 "/fresh/bin:/usr/bin",
958 );
959 return true;
960 }),
961 });
962 }),
963 );
964​
965 it.effect("recovers the login-shell PATH when the first probe outlasts the timeout", () =>
966 Effect.gen(function* () {
967 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin:/bin:/usr/sbin:/sbin" };
968 const output = envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" });
969 let shellProbes = 0;
970 yield* runShellEnvironment({
971 env,
972 platform: "darwin",
973 handler: () => "",
974 processSpawner: ChildProcessSpawner.make((command) => {
975 if (command._tag !== "StandardCommand" || command.command !== "/bin/zsh") {
976 return Effect.succeed(makeProcess(""));
977 }
978 shellProbes += 1;
979 if (shellProbes > 1) return Effect.succeed(makeProcess(output));
980 // A cold start: the first login shell answers, but only after 7s.
981 const slow = Effect.sleep("7 seconds");
982 const stdout = Stream.fromEffect(slow.pipe(Effect.as(textEncoder.encode(output))));
983 return Effect.succeed(
984 ChildProcessSpawner.makeHandle({
985 pid: ChildProcessSpawner.ProcessId(125),
986 stdout,
987 stderr: Stream.empty,
988 all: stdout,
989 exitCode: slow.pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
990 isRunning: Effect.succeed(true),
991 kill: () => Effect.void,
992 stdin: Sink.drain,
993 getInputFd: () => Sink.drain,
994 getOutputFd: () => Stream.empty,
995 unref: Effect.succeed(Effect.void),
996 }),
997 );
998 }),
999 use: (service) =>
1000 Effect.gen(function* () {
1001 yield* service.installIntoProcess;
1002 yield* TestClock.adjust("10 minutes");
1003 return true;
1004 }),
1005 }).pipe(Effect.provide(Logger.layer([])));
1006​
1007 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin");
1008 }),
1009 );
1010​
1011 it.effect("loads PowerShell profile environment on Windows", () =>
1012 Effect.gen(function* () {
1013 const env: NodeJS.ProcessEnv = {
1014 PATH: "C:\\Windows\\System32",
1015 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
1016 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
1017 USERPROFILE: "C:\\Users\\testuser",
1018 };
1019​
1020 const scripts: Array<string> = [];
1021 const prepared = yield* runShellEnvironment({
1022 env,
1023 platform: "win32",
1024 handler: (command) => {
1025 if (command._tag !== "StandardCommand") return "";
1026 scripts.push(String(command.args.at(-1)));
1027 const loadProfile = !command.args.includes("-NoProfile");
1028 return loadProfile
1029 ? envOutput({
1030 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
1031 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
1032 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
1033 })
1034 : envOutput({ PATH: 'C:\\Custom\\Bin;C:";C:\\Windows\\System32' });
1035 },
1036 });
1037​
1038 assert.isTrue(prepared);
1039 assert.equal(
1040 env.PATH,
1042 "C:\\Profile\\Node",
1043 "C:\\Windows\\System32",
1044 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
1045 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
1046 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
1047 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
1048 "C:\\Users\\testuser\\.local\\bin",
1049 "C:\\Users\\testuser\\.bun\\bin",
1050 "C:\\Users\\testuser\\scoop\\shims",
1051 "C:\\Custom\\Bin",
1052 "C:",
1053 ].join(";"),
1054 );
1055 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
1056 assert.equal(
1057 env.FNM_MULTISHELL_PATH,
1058 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
1059 );
1060 assert.isNotEmpty(scripts);
1061 for (const script of scripts) {
1062 assert.include(
1063 script,
1064 "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)",
1065 );
1067 }),
1068 );
1069​
1070 it.effect.each([false, true])(
1071 "leaves Windows preparation incomplete when loadProfile=%s fails",
1072 (loadProfile) =>
1073 Effect.gen(function* () {
1074 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
1075 let calls = 0;
1076 const prepared = yield* runShellEnvironment({
1077 env,
1078 platform: "win32",
1079 handler: (command) => {
1080 calls++;
1081 if (command._tag !== "StandardCommand") return "";
1082 return !command.args.includes("-NoProfile") === loadProfile
1083 ? { output: "", exitCode: 1 }
1084 : envOutput({ PATH: "C:\\Working" });
1085 },
1086 }).pipe(Effect.provide(Logger.layer([])));
1087 assert.isFalse(prepared);
1088 assert.equal(calls, 3);
1089 assert.include(env.PATH, "C:\\Working");
1090 assert.include(env.PATH, "C:\\Windows\\System32");
1091 }),
1092 );
1093​
1094 it.effect("gives up on a Windows shell that survives the kill", () =>
1095 Effect.gen(function* () {
1096 const commands: Array<string> = [];
1097 // Both PowerShell probes spawn pwsh, time out, then spawn powershell. Each probe's timeout
1098 // is armed before its spawn runs, so waiting for the spawns keeps the clock in step.
1099 const spawned = yield* Effect.forEach([0, 1, 2, 3], () => Deferred.make<void>());
1100 const fiber = yield* runShellEnvironment({
1101 env: { PATH: "C:\\Windows", APPDATA: "C:\\Roaming" },
1102 platform: "win32",
1103 handler: () => "",
1104 processSpawner: ChildProcessSpawner.make((command) => {
1105 const spawn = spawned[commands.length];
1106 commands.push(command._tag === "StandardCommand" ? command.command : "");
1107 return (spawn ? Deferred.succeed(spawn, undefined) : Effect.void).pipe(
1108 Effect.andThen(
1109 Effect.acquireRelease(Effect.succeed(makeHungProcess()), () => Effect.never),
1110 ),
1111 );
1112 }),
1113 }).pipe(Effect.forkChild({ startImmediately: true }));
1114​
1115 yield* Effect.forEach(spawned.slice(0, 2), Deferred.await);
1116 yield* TestClock.adjust("5 seconds");
1117 yield* Effect.forEach(spawned.slice(2), Deferred.await);
1118 yield* TestClock.adjust("5 seconds");
1119 yield* Fiber.join(fiber);
1120 assert.deepEqual([...new Set(commands)], ["pwsh.exe", "powershell.exe"]);
1121 }),
1122 );
1123​
1124 it.effect("applies saved Windows user telemetry opt-outs missing from the launcher", () =>
1125 Effect.gen(function* () {
1126 const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };
1127​
1128 yield* runShellEnvironment({
1129 env,
1130 platform: "win32",
1131 handler: (command) =>
1132 command._tag === "StandardCommand" &&
1133 String(command.args.at(-1)).includes("T3CODE_TELEMETRY_ENABLED")
1134 ? envOutput({
1135 PATH: "C:\\Windows\\System32",
1136 T3CODE_TELEMETRY_ENABLED: "false",
1137 DO_NOT_TRACK: "1",
1138 })
1139 : envOutput({ PATH: "C:\\Windows\\System32" }),
1140 });
1141​
1142 assert.equal(env.T3CODE_TELEMETRY_ENABLED, "false");
1143 assert.equal(env.DO_NOT_TRACK, "1");
1144 }),
1145 );
1146​
1147 it.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
1148 Effect.gen(function* () {
1149 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows\\System32" };
1150​
1151 yield* runShellEnvironment({
1152 env,
1153 platform: "win32",
1154 handler: (command) =>
1155 command._tag === "StandardCommand" && command.args.includes("-NoProfile")
1156 ? envOutput({ PATH: "C:\\Shell\\Bin" })
1157 : "",
1158 });
1159​
1160 assert.deepEqual(env, { PATH: "C:\\Shell\\Bin;C:\\Windows\\System32" });
1161 }),
1162 );
1163​
1164 it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
1165 Effect.gen(function* () {
1166 const env: NodeJS.ProcessEnv = {
1167 SHELL: "/bin/zsh",
1168 PATH: "/usr/bin",
1169 XDG_CURRENT_DESKTOP: "wrong-launcher",
1170 XDG_SESSION_DESKTOP: "wrong-launcher",
1171 };
1172​
1173 yield* runShellEnvironment({
1174 env,
1175 platform: "linux",
1176 handler: () =>
1177 envOutput({
1178 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
1179 XDG_CURRENT_DESKTOP: "KDE",
1180 XDG_SESSION_DESKTOP: "KDE",
1181 XDG_SESSION_TYPE: "wayland",
1182 }),
1183 });
1184​
1185 assert.equal(env.XDG_CURRENT_DESKTOP, "KDE");
1186 assert.equal(env.XDG_SESSION_DESKTOP, "KDE");
1187 assert.equal(env.XDG_SESSION_TYPE, "wayland");
1188 }),
1189 );
1190});