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

DesktopWslEnvironment.test.ts

237 lines8.4 KB

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