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

os-jank.test.ts

231 lines7.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it as effectIt } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import { CommandAvailability, WindowsShellEnvironment } from "@t3tools/shared/shell";
5import * as Effect from "effect/Effect";
6// @effect-diagnostics-next-line nodeBuiltinImport:off -- Observe the existing synchronous shell probe without launching the user's login shell.
7import * as NodeChildProcess from "node:child_process";
8import * as NodeOS from "node:os";
9import { assert, it, vi } from "vite-plus/test";
10​
11import { fixPath, hydratePosixHome, refreshWindowsPath } from "./os-jank.ts";
12​
13vi.mock("node:child_process", { spy: true });
14​
15it("hydrates HOME independently of a blank process HOME", () => {
16 const originalHome = process.env.HOME;
17 const env: NodeJS.ProcessEnv = { HOME: " " };
18​
19 try {
20 process.env.HOME = " ";
21 hydratePosixHome(env);
22 } finally {
23 if (originalHome === undefined) {
24 delete process.env.HOME;
25 } else {
26 process.env.HOME = originalHome;
27 }
28 }
29​
30 assert.equal(env.HOME, NodeOS.userInfo().homedir);
31});
32​
33it("preserves an explicitly configured HOME", () => {
34 const env: NodeJS.ProcessEnv = { HOME: "/custom/home" };
35​
36 hydratePosixHome(env, () => {
37 throw new Error("HOME lookup should not run");
38 });
39​
40 assert.equal(env.HOME, "/custom/home");
41});
42​
43effectIt.effect("hydrates PATH when the desktop handoff is absent", () =>
44 Effect.acquireUseRelease(
45 Effect.sync(() =>
46 vi
47 .spyOn(NodeChildProcess, "execFileSync")
48 .mockReturnValue("__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n"),
49 ),
50 (probe) =>
51 Effect.gen(function* () {
52 const env: NodeJS.ProcessEnv = { SHELL: "/bin/bash", PATH: "/usr/bin" };
53​
54 yield* fixPath().pipe(
55 Effect.provideService(HostProcess.Platform, "linux"),
56 Effect.provideService(HostProcess.Environment, env),
57 Effect.provide(NodeServices.layer),
58 );
59​
60 assert.equal(probe.mock.calls.length, 1);
61 assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
62 }),
63 (probe) => Effect.sync(() => probe.mockRestore()),
64 ),
65);
66​
67const timedOut = (file: string) =>
68 Object.assign(new Error(`spawnSync ${file} ETIMEDOUT`), { code: "ETIMEDOUT" });
69​
70effectIt.effect("waits longer for a login shell that times out at startup", () =>
71 Effect.acquireUseRelease(
72 Effect.sync(() =>
73 vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file, _args, options) => {
74 // This profile takes 8 seconds while the machine is under load.
75 if ((options?.timeout ?? 0) < 8_000) throw timedOut(String(file));
76 return "__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n";
77 }),
78 ),
79 () =>
80 Effect.gen(function* () {
81 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
82​
83 yield* fixPath().pipe(
84 Effect.provideService(HostProcess.Platform, "linux"),
85 Effect.provideService(HostProcess.Environment, env),
86 Effect.provide(NodeServices.layer),
87 );
88​
89 assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
90 }),
91 (probe) => Effect.sync(() => probe.mockRestore()),
92 ),
93);
94​
95effectIt.effect("waits longer for only one hung login shell", () =>
96 Effect.acquireUseRelease(
97 Effect.sync(() =>
98 vi.spyOn(NodeChildProcess, "execFileSync").mockImplementation((file) => {
99 throw timedOut(String(file));
100 }),
101 ),
102 (probe) =>
103 Effect.gen(function* () {
104 const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
105​
106 yield* fixPath().pipe(
107 Effect.provideService(HostProcess.Platform, "linux"),
108 Effect.provideService(HostProcess.Environment, env),
109 Effect.provide(NodeServices.layer),
110 );
111​
112 const timeouts = probe.mock.calls.map(([, , options]) => options?.timeout);
113 assert.equal(timeouts.filter((timeout) => (timeout ?? 0) > 5_000).length, 1);
114 assert.equal(env.PATH, "/usr/bin");
115 }),
116 (probe) => Effect.sync(() => probe.mockRestore()),
117 ),
118);
119​
120effectIt.effect("keeps mise's PATH record with the login shell PATH", () =>
121 Effect.acquireUseRelease(
122 Effect.sync(() =>
123 vi
124 .spyOn(NodeChildProcess, "execFileSync")
125 .mockReturnValue(
126 [
127 "__T3CODE_ENV_PATH_START__",
128 "/home/me/.local/bin:/mise/installs/claude/latest:/mise/shims",
129 "__T3CODE_ENV_PATH_END__",
130 "__T3CODE_ENV___MISE_DIFF_START__",
131 "mise-diff",
132 "__T3CODE_ENV___MISE_DIFF_END__",
133 ].join("\n"),
134 ),
135 ),
136 () =>
137 Effect.gen(function* () {
138 const env: NodeJS.ProcessEnv = { SHELL: "/bin/bash", PATH: "/usr/bin" };
139​
140 yield* fixPath().pipe(
141 Effect.provideService(HostProcess.Platform, "linux"),
142 Effect.provideService(HostProcess.Environment, env),
143 Effect.provide(NodeServices.layer),
144 );
145​
146 assert.equal(env.__MISE_DIFF, "mise-diff");
147 }),
148 (probe) => Effect.sync(() => probe.mockRestore()),
149 ),
150);
151​
152effectIt.effect(
153 "hydrates missing JAVA_HOME and feature flags from the login shell and keeps explicit ones",
154 () =>
155 Effect.acquireUseRelease(
156 Effect.sync(() =>
157 vi
158 .spyOn(NodeChildProcess, "execFileSync")
159 .mockReturnValue(
160 [
161 "__T3CODE_ENV_PATH_START__",
162 "/opt/homebrew/bin",
163 "__T3CODE_ENV_PATH_END__",
164 "__T3CODE_ENV_JAVA_HOME_START__",
165 "/opt/homebrew/opt/openjdk@17",
166 "__T3CODE_ENV_JAVA_HOME_END__",
167 "__T3CODE_ENV_T3CODE_FEATURE_FLAGS_START__",
168 "tanstack_source",
169 "__T3CODE_ENV_T3CODE_FEATURE_FLAGS_END__",
170 ].join("\n"),
171 ),
172 ),
173 () =>
174 Effect.gen(function* () {
175 const missing: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
176 const explicit: NodeJS.ProcessEnv = {
177 SHELL: "/bin/zsh",
178 PATH: "/usr/bin",
179 JAVA_HOME: "/custom/jdk",
180 T3CODE_FEATURE_FLAGS: "",
181 };
182​
183 for (const env of [missing, explicit]) {
184 yield* fixPath().pipe(
185 Effect.provideService(HostProcess.Platform, "darwin"),
186 Effect.provideService(HostProcess.Environment, env),
187 Effect.provide(NodeServices.layer),
188 );
189 }
190​
191 assert.equal(missing.JAVA_HOME, "/opt/homebrew/opt/openjdk@17");
192 assert.equal(explicit.JAVA_HOME, "/custom/jdk");
193 assert.equal(missing.T3CODE_FEATURE_FLAGS, "tanstack_source");
194 assert.equal(explicit.T3CODE_FEATURE_FLAGS, "");
195 }),
196 (probe) => Effect.sync(() => probe.mockRestore()),
197 ),
198);
199​
200effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second key", () =>
201 Effect.gen(function* () {
202 const env: NodeJS.ProcessEnv = { Path: "C:\\Windows" };
203​
204 yield* fixPath().pipe(
205 Effect.provideService(HostProcess.Platform, "win32"),
206 Effect.provideService(HostProcess.Environment, env),
207 Effect.provideService(WindowsShellEnvironment, () => Effect.succeed({ PATH: "C:\\Tools" })),
208 Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
209 Effect.provide(NodeServices.layer),
210 );
211​
212 assert.deepEqual(env, { PATH: "C:\\Tools;C:\\Windows" });
213 }),
214);
215​
216effectIt.effect("adds a directory an installer wrote to the registry after launch", () =>
217 Effect.gen(function* () {
218 const env: NodeJS.ProcessEnv = { Path: "C:\\Tools;C:\\Windows" };
219 const installed = "C:\\Users\\me\\AppData\\Local\\cursor-agent";
220​
221 yield* refreshWindowsPath().pipe(
222 Effect.provideService(HostProcess.Platform, "win32"),
223 Effect.provideService(HostProcess.Environment, env),
224 Effect.provideService(WindowsShellEnvironment, () =>
225 Effect.succeed({ PATH: `C:\\Windows;${installed}` }),
226 ),
227 );
228​
229 assert.deepEqual(env, { PATH: `C:\\Tools;C:\\Windows;${installed}` });
230 }),
231);