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 formatWslShellTransportFailureReason,
18 parseNodePath,
19 parseNodeVersion,
20 parseResolvedPath,
21 parseToolchainReport,
22 probeWslDistros,
23} from "./DesktopWslEnvironment.ts";
24
25const encoder = new TextEncoder();
26
27const makeDistroListSpawner = (result: { readonly stdout?: string; readonly exitCode?: number }) =>
28 ChildProcessSpawner.make(() =>
29 Effect.succeed(
30 ChildProcessSpawner.makeHandle({
31 pid: ChildProcessSpawner.ProcessId(1),
32 exitCode:
33 result.exitCode === undefined
34 ? Effect.never
35 : Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
36 isRunning: Effect.succeed(result.exitCode === undefined),
37 kill: () => Effect.void,
38 unref: Effect.succeed(Effect.void),
39 stdin: Sink.drain,
40 stdout: Stream.make(encoder.encode(result.stdout ?? "")),
41 stderr: Stream.empty,
42 all: Stream.empty,
43 getInputFd: () => Sink.drain,
44 getOutputFd: () => Stream.empty,
45 }),
46 ),
47 );
48
49describe("probeWslDistros", () => {
50 it.effect("preserves a successful empty distro list", () =>
51 Effect.gen(function* () {
52 const distros = yield* probeWslDistros;
53 expect(distros).toEqual([]);
54 }).pipe(
55 Effect.provideService(
56 ChildProcessSpawner.ChildProcessSpawner,
57 makeDistroListSpawner({ stdout: "", exitCode: 0 }),
58 ),
59 ),
60 );
61
62 it.effect("fails when the distro-list command exits unsuccessfully", () =>
63 Effect.gen(function* () {
64 const error = yield* probeWslDistros.pipe(Effect.flip);
65 expect(error).toBeInstanceOf(DesktopWslDistroListError);
66 expect(error.message).toContain("exited with code 1");
67 }).pipe(
68 Effect.provideService(
69 ChildProcessSpawner.ChildProcessSpawner,
70 makeDistroListSpawner({ exitCode: 1 }),
71 ),
72 ),
73 );
74
75 it.effect("fails when the distro-list command times out", () => {
76 const layer = Layer.merge(
77 TestClock.layer(),
78 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, makeDistroListSpawner({})),
79 );
80 return Effect.gen(function* () {
81 const fiber = yield* probeWslDistros.pipe(Effect.flip, Effect.forkScoped);
82 yield* Effect.yieldNow;
83 yield* TestClock.adjust(Duration.seconds(8));
84 const error = yield* Fiber.join(fiber);
85 expect(error).toBeInstanceOf(DesktopWslDistroListError);
86 expect(error.message).toContain("timed out");
87 }).pipe(Effect.provide(layer));
88 });
89});
90
91describe("formatNodePtyProbeFailureReason", () => {
92 it("identifies a packaged build that omitted the Linux node-pty prebuild", () => {
93 const reason = formatNodePtyProbeFailureReason(4);
94
95 expect(reason).toContain("packaged Linux node-pty binary was not included");
96 expect(reason).toContain("--wsl-prebuild");
97 });
98
99 it("leaves other node-pty load failures to the compatibility diagnostic", () => {
100 expect(formatNodePtyProbeFailureReason(1)).toBeNull();
101 });
102});
103
104describe("formatWslShellTransportFailureReason", () => {
105 it("distinguishes timeouts and spawn failures from normal shell exit codes", () => {
106 expect(formatWslShellTransportFailureReason("timeout")).toContain("timed out");
107 expect(formatWslShellTransportFailureReason("spawn")).toContain("could not start wsl.exe");
108 expect(formatWslShellTransportFailureReason("process")).toContain("lost communication");
109 expect(formatWslShellTransportFailureReason(null)).toBeNull();
110 });
111});
112
113describe("buildWslNodeEnvPreamble", () => {
114 it("passes the required Node engine range into the shared resolver", () => {
115 const preamble = buildWslNodeEnvPreamble("^22.16 || ^23.11 || >=24.10");
116
117 expect(preamble).toContain("T3_NODE_ENGINE_RANGE='^22.16 || ^23.11 || >=24.10'");
118 expect(preamble.indexOf("T3_NODE_ENGINE_RANGE=")).toBeLessThan(
119 preamble.lastIndexOf("ensure_remote_node_path || true"),
120 );
121 });
122
123 it("keeps the shared resolver permissive when no Node engine range is provided", () => {
124 expect(buildWslNodeEnvPreamble()).toContain("T3_NODE_ENGINE_RANGE=''");
125 });
126});
127
128describe("parseToolchainReport", () => {
129 it("returns no missing tools and no node version on empty output", () => {
130 expect(parseToolchainReport("")).toEqual({ missingTools: [], nodeVersion: null });
131 });
132
133 it("collects all missing: lines", () => {
134 const stdout = ["missing:make", "missing:g++", "nodeVersion:24.10.0"].join("\n");
135 expect(parseToolchainReport(stdout)).toEqual({
136 missingTools: ["make", "g++"],
137 nodeVersion: "24.10.0",
138 });
139 });
140
141 it("ignores blank lines and trims whitespace", () => {
142 const stdout = [" missing:python3 ", "", " nodeVersion:v22.16.0 "].join("\n");
143 expect(parseToolchainReport(stdout)).toEqual({
144 missingTools: ["python3"],
145 nodeVersion: "v22.16.0",
146 });
147 });
148
149 it("returns null node version when value after prefix is empty", () => {
150 expect(parseToolchainReport("nodeVersion:")).toEqual({
151 missingTools: [],
152 nodeVersion: null,
153 });
154 });
155});
156
157describe("parseNodePath", () => {
158 it("extracts the absolute node path from a nodePath: line", () => {
159 const stdout = "nodePath:/home/josh/.nvm/versions/node/v22.16.0/bin/node";
160 expect(parseNodePath(stdout)).toBe("/home/josh/.nvm/versions/node/v22.16.0/bin/node");
161 });
162
163 it("returns null when node was not found (empty value after prefix)", () => {
164 expect(parseNodePath("nodePath:")).toBeNull();
165 });
166
167 it("returns null when there is no nodePath line at all", () => {
168 expect(parseNodePath("missing:node\nnodeVersion:")).toBeNull();
169 });
170
171 it("ignores surrounding noise and trims whitespace", () => {
172 const stdout = ["some preamble noise", " nodePath:/usr/bin/node ", "trailing"].join("\n");
173 expect(parseNodePath(stdout)).toBe("/usr/bin/node");
174 });
175});
176
177describe("parseNodeVersion", () => {
178 it("extracts the node version from a nodeVersion: line", () => {
179 expect(parseNodeVersion("nodeVersion:24.10.0")).toBe("24.10.0");
180 });
181
182 it("returns null when the version value is empty", () => {
183 expect(parseNodeVersion("nodeVersion:")).toBeNull();
184 });
185
186 it("returns null when there is no nodeVersion line at all", () => {
187 expect(parseNodeVersion("nodePath:/usr/bin/node\nresolvedPath:/usr/bin")).toBeNull();
188 });
189
190 it("ignores surrounding noise and trims whitespace", () => {
191 const stdout = [
192 "some preamble noise",
193 " nodeVersion:22.16.0 ",
194 "nodePath:/usr/bin/node",
195 ].join("\n");
196 expect(parseNodeVersion(stdout)).toBe("22.16.0");
197 });
198});
199
200describe("parseResolvedPath", () => {
201 it("preserves spaces and apostrophes in the resolved login-shell PATH", () => {
202 const resolvedPath = "/home/test user/bin:/opt/test's tools/bin:/usr/bin:/bin";
203 expect(parseResolvedPath(`nodePath:/usr/bin/node\nresolvedPath:${resolvedPath}\n`)).toBe(
204 resolvedPath,
205 );
206 });
207
208 it("accepts CRLF output without retaining the carriage return", () => {
209 expect(parseResolvedPath("resolvedPath:/usr/local/bin:/usr/bin\r\n")).toBe(
210 "/usr/local/bin:/usr/bin",
211 );
212 });
213
214 it("returns null when the resolved PATH is absent or empty", () => {
215 expect(parseResolvedPath("nodePath:/usr/bin/node\n")).toBeNull();
216 expect(parseResolvedPath("resolvedPath:\n")).toBeNull();
217 });
218});
219
220describe("formatMissingToolsReason", () => {
221 it("returns null when everything is present and node is in range", () => {
222 expect(
223 formatMissingToolsReason({ missingTools: [], nodeVersion: "24.10.0" }, "^24.10"),
224 ).toBeNull();
225 });
226
227 it("returns null when range is not specified and tools are present", () => {
228 expect(formatMissingToolsReason({ missingTools: [], nodeVersion: "18.0.0" }, null)).toBeNull();
229 });
230
231 it("flags missing node first", () => {
232 const reason = formatMissingToolsReason(
233 { missingTools: ["node", "make"], nodeVersion: null },
234 "^24.10",
235 );
236 expect(reason).toContain("node");
237 expect(reason).toContain("^24.10");
238 expect(reason).toContain("make");
239 expect(reason).toContain("nvm");
240 });
241
242 it("flags an out-of-range node version with the actual version surfaced", () => {
243 const reason = formatMissingToolsReason(
244 { missingTools: [], nodeVersion: "20.0.0" },
245 "^24.10 || ^22.16",
246 );
247 expect(reason).toContain("node 20.0.0");
248 expect(reason).toContain("requires ^24.10 || ^22.16");
249 });
250
251 it("flags missing build tools without node when node is fine", () => {
252 const reason = formatMissingToolsReason(
253 { missingTools: ["g++", "python3"], nodeVersion: "24.10.0" },
254 "^24.10",
255 );
256 expect(reason).toContain("g++");
257 expect(reason).toContain("python3");
258 expect(reason).toContain("build-essential");
259 expect(reason).not.toContain("nvm");
260 });
261});