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

DesktopShellEnvironment.test.ts

232 lines7.3 KB

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