Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Logger from "effect/Logger";
5import * as PlatformError from "effect/PlatformError";
6import * as Schema from "effect/Schema";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as ChildProcess from "effect/unstable/process/ChildProcess";
10import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
11
12import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
13import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
14
15const textEncoder = new TextEncoder();
16
17const isDesktopShellEnvironmentCommandError = Schema.is(
18 DesktopShellEnvironment.DesktopShellEnvironmentCommandError,
19);
20
21function envOutput(values: Readonly<Record<string, string>>): string {
22 return Object.entries(values)
23 .flatMap(([name, value]) => [`__V12_ENV_${name}_START__`, value, `__V12_ENV_${name}_END__`])
24 .join("\n");
25}
26
27function makeProcess(output: string): ChildProcessSpawner.ChildProcessHandle {
28 const stdout = output.length === 0 ? Stream.empty : Stream.make(textEncoder.encode(output));
29 return ChildProcessSpawner.makeHandle({
30 pid: ChildProcessSpawner.ProcessId(123),
31 stdout,
32 stderr: Stream.empty,
33 all: stdout,
34 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
35 isRunning: Effect.succeed(false),
36 kill: () => Effect.void,
37 stdin: Sink.drain,
38 getInputFd: () => Sink.drain,
39 getOutputFd: () => Stream.empty,
40 unref: Effect.succeed(Effect.void),
41 });
42}
43
44function withProcessEnv<A, E, R>(
45 env: NodeJS.ProcessEnv,
46 effect: Effect.Effect<A, E, R>,
47): Effect.Effect<A, E, R> {
48 return Effect.acquireUseRelease(
49 Effect.sync(() => {
50 const previous = process.env;
51 process.env = env;
52 return previous;
53 }),
54 () => effect,
55 (previous) =>
56 Effect.sync(() => {
57 process.env = previous;
58 }),
59 );
60}
61
62function runShellEnvironment(input: {
63 readonly env: NodeJS.ProcessEnv;
64 readonly platform: NodeJS.Platform;
65 readonly handler: (command: ChildProcess.Command) => string;
66 readonly failure?: PlatformError.PlatformError;
67}) {
68 const environmentLayer = Layer.succeed(
69 DesktopEnvironment.DesktopEnvironment,
70 DesktopEnvironment.DesktopEnvironment.of({
71 platform: input.platform,
72 } as DesktopEnvironment.DesktopEnvironment["Service"]),
73 );
74 const spawnerLayer = Layer.succeed(
75 ChildProcessSpawner.ChildProcessSpawner,
76 ChildProcessSpawner.make((command) =>
77 input.failure === undefined
78 ? Effect.succeed(makeProcess(input.handler(command)))
79 : Effect.fail(input.failure),
80 ),
81 );
82
83 const program = Effect.gen(function* () {
84 const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
85 yield* shellEnvironment.installIntoProcess;
86 }).pipe(
87 Effect.provide(
88 DesktopShellEnvironment.layer.pipe(
89 Layer.provide(Layer.mergeAll(environmentLayer, spawnerLayer)),
90 ),
91 ),
92 );
93
94 return withProcessEnv(input.env, program);
95}
96
97describe("DesktopShellEnvironment", () => {
98 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () =>
99 Effect.gen(function* () {
100 const env: NodeJS.ProcessEnv = {
101 SHELL: "/bin/zsh",
102 PATH: "/Users/test/.local/bin:/usr/bin",
103 };
104 const commands: ChildProcess.Command[] = [];
105
106 yield* runShellEnvironment({
107 env,
108 platform: "darwin",
109 handler: (command) => {
110 commands.push(command);
111 return envOutput({
112 PATH: "/opt/homebrew/bin:/usr/bin",
113 SSH_AUTH_SOCK: "/tmp/secretive.sock",
114 HOMEBREW_PREFIX: "/opt/homebrew",
115 });
116 },
117 });
118
119 assert.equal(commands.length, 1);
120 assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh");
121 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
122 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
123 assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew");
124 }),
125 );
126
127 it.effect("preserves inherited POSIX values when present", () =>
128 Effect.gen(function* () {
129 const env: NodeJS.ProcessEnv = {
130 SHELL: "/bin/zsh",
131 PATH: "/usr/bin",
132 SSH_AUTH_SOCK: "/tmp/inherited.sock",
133 };
134
135 yield* runShellEnvironment({
136 env,
137 platform: "darwin",
138 handler: () =>
139 envOutput({
140 PATH: "/opt/homebrew/bin:/usr/bin",
141 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
142 }),
143 });
144
145 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
146 assert.equal(env.SSH_AUTH_SOCK, "/tmp/inherited.sock");
147 }),
148 );
149
150 it.effect("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () =>
151 Effect.gen(function* () {
152 const env: NodeJS.ProcessEnv = {
153 SHELL: "/bin/zsh",
154 PATH: "/usr/bin",
155 };
156
157 yield* runShellEnvironment({
158 env,
159 platform: "linux",
160 handler: () =>
161 envOutput({
162 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
163 SSH_AUTH_SOCK: "/tmp/secretive.sock",
164 }),
165 });
166
167 assert.equal(env.PATH, "/home/linuxbrew/.linuxbrew/bin:/usr/bin");
168 assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock");
169 }),
170 );
171
172 it.effect("falls back to launchctl PATH on macOS when shell probing does not return one", () =>
173 Effect.gen(function* () {
174 const env: NodeJS.ProcessEnv = {
175 SHELL: "/opt/homebrew/bin/nu",
176 PATH: "/usr/bin",
177 };
178 const commands: string[] = [];
179
180 yield* runShellEnvironment({
181 env,
182 platform: "darwin",
183 handler: (command) => {
184 if (command._tag !== "StandardCommand") return "";
185 commands.push(command.command);
186 return command.command === "/bin/launchctl" ? "/opt/homebrew/bin:/usr/bin" : "";
187 },
188 });
189
190 assert.deepEqual(commands, ["/opt/homebrew/bin/nu", "/bin/zsh", "/bin/launchctl"]);
191 assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin");
192 }),
193 );
194
195 it.effect("loads PowerShell profile environment on Windows", () =>
196 Effect.gen(function* () {
197 const env: NodeJS.ProcessEnv = {
198 PATH: "C:\\Windows\\System32",
199 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
200 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
201 USERPROFILE: "C:\\Users\\testuser",
202 };
203
204 yield* runShellEnvironment({
205 env,
206 platform: "win32",
207 handler: (command) => {
208 if (command._tag !== "StandardCommand") return "";
209 const loadProfile = !command.args.includes("-NoProfile");
210 return loadProfile
211 ? envOutput({
212 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
213 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
214 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
215 })
216 : envOutput({ PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" });
217 },
218 });
219
220 assert.equal(
221 env.PATH,
222 [
223 "C:\\Profile\\Node",
224 "C:\\Windows\\System32",
225 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
226 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
227 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
228 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
229 "C:\\Users\\testuser\\.bun\\bin",
230 "C:\\Users\\testuser\\scoop\\shims",
231 "C:\\Custom\\Bin",
232 ].join(";"),
233 );
234 assert.equal(env.FNM_DIR, "C:\\Users\\testuser\\AppData\\Roaming\\fnm");
235 assert.equal(
236 env.FNM_MULTISHELL_PATH,
237 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
238 );
239 }),
240 );
241
242 it.effect("logs command failures with safe probe context and the exact cause", () => {
243 const env: NodeJS.ProcessEnv = {
244 SHELL: "/bin/bash",
245 PATH: "/usr/bin",
246 };
247 const cause = PlatformError.systemError({
248 _tag: "PermissionDenied",
249 module: "ChildProcess",
250 method: "spawn",
251 pathOrDescriptor: "/bin/bash",
252 });
253 const messages: Array<unknown> = [];
254 const logger = Logger.make(({ message }) => {
255 messages.push(message);
256 });
257
258 return runShellEnvironment({
259 env,
260 platform: "linux",
261 handler: () => "",
262 failure: cause,
263 }).pipe(
264 Effect.andThen(
265 Effect.sync(() => {
266 const errors = messages
267 .flatMap((message) => (Array.isArray(message) ? message : [message]))
268 .filter(isDesktopShellEnvironmentCommandError);
269 assert.lengthOf(errors, 1);
270 assert.equal(errors[0]?.probe, "login-shell");
271 assert.equal(errors[0]?.executable, "bash");
272 assert.equal(errors[0]?.argumentCount, 2);
273 assert.notProperty(errors[0] ?? {}, "args");
274 assert.equal(errors[0]?.cause, cause);
275 assert.notInclude(errors[0]?.message ?? "", cause.message);
276 }),
277 ),
278 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
279 );
280 });
281});