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

DesktopShellEnvironment.test.ts

285 lines9.1 KB

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