Open History to see the last commit
1import { describe, it } from "@effect/vitest";
2import { expect } from "vite-plus/test";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as TestClock from "effect/testing/TestClock";
10import { ChildProcessSpawner } from "effect/unstable/process";
11
12import {
13 buildWslNodeEnvPreamble,
14 DesktopWslDistroListError,
15 formatMissingToolsReason,
16 formatNodePtyProbeFailureReason,
17 formatWslLibcCompatibilityReason,
18 formatWslShellTransportFailureReason,
19 MIN_WSL_GLIBC_VERSION,
20 parseGlibcVersion,
21 parseLibcFamily,
22 parseBundledWslNodePreparationOutput,
23 parseNodePath,
24 parseNodeVersion,
25 parseResolvedPath,
26 parseToolchainReport,
27 parseWslTcpPortAllocationOutput,
28 probeWslDistros,
29} from "./DesktopWslEnvironment.ts";
30
31const encoder = new TextEncoder();
32
33const makeDistroListSpawner = (result: { readonly stdout?: string; readonly exitCode?: number }) =>
34 ChildProcessSpawner.make(() =>
35 Effect.succeed(
36 ChildProcessSpawner.makeHandle({
37 pid: ChildProcessSpawner.ProcessId(1),
38 exitCode:
39 result.exitCode === undefined
40 ? Effect.never
41 : Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
42 isRunning: Effect.succeed(result.exitCode === undefined),
43 kill: () => Effect.void,
44 unref: Effect.succeed(Effect.void),
45 stdin: Sink.drain,
46 stdout: Stream.make(encoder.encode(result.stdout ?? "")),
47 stderr: Stream.empty,
48 all: Stream.empty,
49 getInputFd: () => Sink.drain,
50 getOutputFd: () => Stream.empty,
51 }),
52 ),
53 );
54
55describe("probeWslDistros", () => {
56 it.effect("preserves a successful empty distro list", () =>
57 Effect.gen(function* () {
58 const distros = yield* probeWslDistros;
59 expect(distros).toEqual([]);
60 }).pipe(
61 Effect.provideService(
62 ChildProcessSpawner.ChildProcessSpawner,
63 makeDistroListSpawner({ stdout: "", exitCode: 0 }),
64 ),
65 ),
66 );
67
68 it.effect("fails when the distro-list command exits unsuccessfully", () =>
69 Effect.gen(function* () {
70 const error = yield* probeWslDistros.pipe(Effect.flip);
71 expect(error).toBeInstanceOf(DesktopWslDistroListError);
72 expect(error.message).toContain("exited with code 1");
73 }).pipe(
74 Effect.provideService(
75 ChildProcessSpawner.ChildProcessSpawner,
76 makeDistroListSpawner({ exitCode: 1 }),
77 ),
78 ),
79 );
80
81 it.effect("fails when the distro-list command times out", () => {
82 const layer = Layer.merge(
83 TestClock.layer(),
84 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, makeDistroListSpawner({})),
85 );
86 return Effect.gen(function* () {
87 const fiber = yield* probeWslDistros.pipe(Effect.flip, Effect.forkScoped);
88 yield* Effect.yieldNow;
89 yield* TestClock.adjust(Duration.seconds(8));
90 const error = yield* Fiber.join(fiber);
91 expect(error).toBeInstanceOf(DesktopWslDistroListError);
92 expect(error.message).toContain("timed out");
93 }).pipe(Effect.provide(layer));
94 });
95});
96
97describe("formatNodePtyProbeFailureReason", () => {
98 it("identifies a packaged build that omitted the Linux node-pty prebuild", () => {
99 const reason = formatNodePtyProbeFailureReason(4);
100
101 expect(reason).toContain("packaged Linux node-pty binary was not included");
102 expect(reason).toContain("--wsl-prebuild");
103 });
104
105 it("leaves other node-pty load failures to the compatibility diagnostic", () => {
106 expect(formatNodePtyProbeFailureReason(1)).toBeNull();
107 });
108});
109
110
111describe("bundled WSL Node preparation output", () => {
112 it("parses an absolute cached runtime path and exact Node version", () => {
113 expect(
114 parseBundledWslNodePreparationOutput(
115 "bundledNodePath:/home/test user/.cache/t3code/wsl-runtime/node-abc\nbundledNodeVersion:24.13.1\n",
116 ),
117 ).toEqual({
118 nodePath: "/home/test user/.cache/t3code/wsl-runtime/node-abc",
119 nodeVersion: "24.13.1",
120 });
121 });
122
123 it("rejects relative paths, missing fields, and malformed versions", () => {
124 expect(
125 parseBundledWslNodePreparationOutput(
126 "bundledNodePath:relative/node\nbundledNodeVersion:24.13.1\n",
127 ),
128 ).toBeNull();
129 expect(
130 parseBundledWslNodePreparationOutput(
131 "bundledNodePath:/home/test/node\nbundledNodeVersion:24.x\n",
132 ),
133 ).toBeNull();
134 expect(parseBundledWslNodePreparationOutput("bundledNodeVersion:24.13.1\n")).toBeNull();
135 });
136});
137
138describe("WSL libc compatibility", () => {
139 it("parses glibc identity and version from preflight output", () => {
140 const stdout = "libcFamily:glibc\nglibcVersion:2.35\nnodePath:/usr/bin/node\n";
141 expect(parseLibcFamily(stdout)).toBe("glibc");
142 expect(parseGlibcVersion(stdout)).toBe("2.35");
143 expect(MIN_WSL_GLIBC_VERSION).toBe("2.35");
144 });
145
146 it("rejects a glibc distro below the packaged runtime floor", () => {
147 const reason = formatWslLibcCompatibilityReason("Ubuntu-20.04", "glibc", "2.31");
148 expect(reason).toContain("Ubuntu-20.04");
149 expect(reason).toContain("glibc 2.31");
150 expect(reason).toContain("requires glibc 2.35 or newer");
151 });
152
153 it("accepts the floor and newer glibc versions", () => {
154 expect(formatWslLibcCompatibilityReason("Ubuntu", "glibc", "2.35")).toBeNull();
155 expect(formatWslLibcCompatibilityReason("Ubuntu", "glibc", "2.39")).toBeNull();
156 });
157
158 it("rejects musl but lets an unknown libc reach the real native load probe", () => {
159 expect(formatWslLibcCompatibilityReason("Alpine", "musl", null)).toContain("musl libc");
160 expect(formatWslLibcCompatibilityReason("Custom", "unknown", null)).toBeNull();
161 });
162
163 it("does not accept malformed glibc version text", () => {
164 expect(parseGlibcVersion("glibcVersion:2.35-custom")).toBeNull();
165 });
166});
167
168describe("formatWslShellTransportFailureReason", () => {
169 it("distinguishes timeouts and spawn failures from normal shell exit codes", () => {
170 expect(formatWslShellTransportFailureReason("timeout")).toContain("timed out");
171 expect(formatWslShellTransportFailureReason("spawn")).toContain("could not start wsl.exe");
172 expect(formatWslShellTransportFailureReason("process")).toContain("lost communication");
173 expect(formatWslShellTransportFailureReason(null)).toBeNull();
174 });
175});
176
177describe("parseWslTcpPortAllocationOutput", () => {
178 it("parses a preferred-port success", () => {
179 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:3774:0\n")).toEqual({
180 port: 3774,
181 usedEphemeralFallback: false,
182 });
183 });
184
185 it("parses a Linux-kernel ephemeral fallback", () => {
186 expect(
187 parseWslTcpPortAllocationOutput("noise\nt3code-wsl-port:49152:1\nmore noise\n"),
188 ).toEqual({
189 port: 49152,
190 usedEphemeralFallback: true,
191 });
192 });
193
194 it("rejects malformed and out-of-range announcements", () => {
195 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:0:1\n")).toBeNull();
196 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:70000:1\n")).toBeNull();
197 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:3774:maybe\n")).toBeNull();
198 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:3774abc:0\n")).toBeNull();
199 expect(parseWslTcpPortAllocationOutput("t3code-wsl-port:3774:0:extra\n")).toBeNull();
200 });
201});
202
203describe("buildWslNodeEnvPreamble", () => {
204 it("passes the required Node engine range into the shared resolver", () => {
205 const preamble = buildWslNodeEnvPreamble("^22.16 || ^23.11 || >=24.10");
206
207 expect(preamble).toContain("T3_NODE_ENGINE_RANGE='^22.16 || ^23.11 || >=24.10'");
208 expect(preamble.indexOf("T3_NODE_ENGINE_RANGE=")).toBeLessThan(
209 preamble.lastIndexOf("ensure_remote_node_path || true"),
210 );
211 });
212
213 it("keeps the shared resolver permissive when no Node engine range is provided", () => {
214 expect(buildWslNodeEnvPreamble()).toContain("T3_NODE_ENGINE_RANGE=''");
215 });
216});
217
218describe("parseToolchainReport", () => {
219 it("returns no missing tools and no node version on empty output", () => {
220 expect(parseToolchainReport("")).toEqual({ missingTools: [], nodeVersion: null });
221 });
222
223 it("collects all missing: lines", () => {
224 const stdout = ["missing:make", "missing:g++", "nodeVersion:24.10.0"].join("\n");
225 expect(parseToolchainReport(stdout)).toEqual({
226 missingTools: ["make", "g++"],
227 nodeVersion: "24.10.0",
228 });
229 });
230
231 it("ignores blank lines and trims whitespace", () => {
232 const stdout = [" missing:python3 ", "", " nodeVersion:v22.16.0 "].join("\n");
233 expect(parseToolchainReport(stdout)).toEqual({
234 missingTools: ["python3"],
235 nodeVersion: "v22.16.0",
236 });
237 });
238
239 it("returns null node version when value after prefix is empty", () => {
240 expect(parseToolchainReport("nodeVersion:")).toEqual({
241 missingTools: [],
242 nodeVersion: null,
243 });
244 });
245});
246
247describe("parseNodePath", () => {
248 it("extracts the absolute node path from a nodePath: line", () => {
249 const stdout = "nodePath:/home/josh/.nvm/versions/node/v22.16.0/bin/node";
250 expect(parseNodePath(stdout)).toBe("/home/josh/.nvm/versions/node/v22.16.0/bin/node");
251 });
252
253 it("returns null when node was not found (empty value after prefix)", () => {
254 expect(parseNodePath("nodePath:")).toBeNull();
255 });
256
257 it("returns null when there is no nodePath line at all", () => {
258 expect(parseNodePath("missing:node\nnodeVersion:")).toBeNull();
259 });
260
261 it("ignores surrounding noise and trims whitespace", () => {
262 const stdout = ["some preamble noise", " nodePath:/usr/bin/node ", "trailing"].join("\n");
263 expect(parseNodePath(stdout)).toBe("/usr/bin/node");
264 });
265});
266
267describe("parseNodeVersion", () => {
268 it("extracts the node version from a nodeVersion: line", () => {
269 expect(parseNodeVersion("nodeVersion:24.10.0")).toBe("24.10.0");
270 });
271
272 it("returns null when the version value is empty", () => {
273 expect(parseNodeVersion("nodeVersion:")).toBeNull();
274 });
275
276 it("returns null when there is no nodeVersion line at all", () => {
277 expect(parseNodeVersion("nodePath:/usr/bin/node\nresolvedPath:/usr/bin")).toBeNull();
278 });
279
280 it("ignores surrounding noise and trims whitespace", () => {
281 const stdout = [
282 "some preamble noise",
283 " nodeVersion:22.16.0 ",
284 "nodePath:/usr/bin/node",
285 ].join("\n");
286 expect(parseNodeVersion(stdout)).toBe("22.16.0");
287 });
288});
289
290describe("parseResolvedPath", () => {
291 it("preserves spaces and apostrophes in the resolved login-shell PATH", () => {
292 const resolvedPath = "/home/test user/bin:/opt/test's tools/bin:/usr/bin:/bin";
293 expect(parseResolvedPath(`nodePath:/usr/bin/node\nresolvedPath:${resolvedPath}\n`)).toBe(
294 resolvedPath,
295 );
296 });
297
298 it("accepts CRLF output without retaining the carriage return", () => {
299 expect(parseResolvedPath("resolvedPath:/usr/local/bin:/usr/bin\r\n")).toBe(
300 "/usr/local/bin:/usr/bin",
301 );
302 });
303
304 it("returns null when the resolved PATH is absent or empty", () => {
305 expect(parseResolvedPath("nodePath:/usr/bin/node\n")).toBeNull();
306 expect(parseResolvedPath("resolvedPath:\n")).toBeNull();
307 });
308});
309
310describe("formatMissingToolsReason", () => {
311 it("returns null when everything is present and node is in range", () => {
312 expect(
313 formatMissingToolsReason({ missingTools: [], nodeVersion: "24.10.0" }, "^24.10"),
314 ).toBeNull();
315 });
316
317 it("returns null when range is not specified and tools are present", () => {
318 expect(formatMissingToolsReason({ missingTools: [], nodeVersion: "18.0.0" }, null)).toBeNull();
319 });
320
321 it("flags missing node first", () => {
322 const reason = formatMissingToolsReason(
323 { missingTools: ["node", "make"], nodeVersion: null },
324 "^24.10",
325 );
326 expect(reason).toContain("node");
327 expect(reason).toContain("^24.10");
328 expect(reason).toContain("make");
329 expect(reason).toContain("nvm");
330 });
331
332 it("flags an out-of-range node version with the actual version surfaced", () => {
333 const reason = formatMissingToolsReason(
334 { missingTools: [], nodeVersion: "20.0.0" },
335 "^24.10 || ^22.16",
336 );
337 expect(reason).toContain("node 20.0.0");
338 expect(reason).toContain("requires ^24.10 || ^22.16");
339 });
340
341 it("flags missing build tools without node when node is fine", () => {
342 const reason = formatMissingToolsReason(
343 { missingTools: ["g++", "python3"], nodeVersion: "24.10.0" },
344 "^24.10",
345 );
346 expect(reason).toContain("g++");
347 expect(reason).toContain("python3");
348 expect(reason).toContain("build-essential");
349 expect(reason).not.toContain("nvm");
350 });
351});