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

window.test.ts

248 lines8.0 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5​
6import type * as Electron from "electron";
7​
8import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
9import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
10import * as ElectronWindow from "../../electron/ElectronWindow.ts";
11import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
12import {
13 getLocalEnvironmentBootstraps,
14 getWindowFullscreenState,
15 resolveWslPickerDistro,
16 resolveWslPickerSelection,
17} from "./window.ts";
18​
19const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
20 executablePath: "wsl.exe",
21 args: ["-d", "Ubuntu", "--", "node", "/app/bin.mjs"],
22 entryPath: "/app/bin.mjs",
23 cwd: "/app",
24 env: {},
25 extendEnv: false,
26 bootstrap: {
27 mode: "desktop",
28 noBrowser: true,
29 port: 3774,
30 host: "0.0.0.0",
31 desktopBootstrapToken: "bootstrap-token",
32 tailscaleServeEnabled: false,
33 tailscaleServePort: 443,
34 },
35 bootstrapDelivery: "stdin",
36 httpBaseUrl: new URL("http://127.0.0.1:3774"),
37 captureOutput: true,
38 preflightFailure: Option.none(),
39 runningDistro: "Ubuntu",
40};
41​
42const defaultWslInstance: DesktopBackendManager.DesktopBackendInstance = {
43 id: DesktopBackendManager.BackendInstanceId("wsl:default"),
44 label: Effect.succeed("WSL (default distro)"),
45 start: Effect.void,
46 stop: () => Effect.void,
47 currentConfig: Effect.succeed(Option.some(readyWslConfig)),
48 snapshot: Effect.succeed({
49 desiredRunning: true,
50 ready: true,
51 activePid: Option.some(123),
52 restartAttempt: 0,
53 restartScheduled: false,
54 }),
55 waitForReady: () => Effect.succeed(true),
56 probeReady: () => Effect.succeed(true),
57};
58​
59const primaryWslInstance: DesktopBackendManager.DesktopBackendInstance = {
60 ...defaultWslInstance,
61 id: DesktopBackendManager.PRIMARY_INSTANCE_ID,
62 label: Effect.succeed("WSL (Ubuntu)"),
63};
64​
65describe("getLocalEnvironmentBootstraps", () => {
66 it.effect("publishes the concrete running distro without replacing the stable instance id", () =>
67 Effect.gen(function* () {
68 const result = yield* getLocalEnvironmentBootstraps.handler();
69​
70 assert.deepEqual(result, [
71 {
72 id: "wsl:default",
73 label: "WSL (Ubuntu)",
74 runningDistro: "Ubuntu",
75 httpBaseUrl: "http://127.0.0.1:3774/",
76 wsBaseUrl: "ws://127.0.0.1:3774/",
77 bootstrapToken: "bootstrap-token",
78 },
79 ]);
80 }).pipe(Effect.provide(DesktopBackendPool.layerTest([defaultWslInstance]))),
81 );
82​
83 it.effect("publishes a pending bootstrap only while a transient retry is scheduled", () => {
84 const retryingConfig: DesktopBackendManager.DesktopBackendStartConfig = {
85 ...readyWslConfig,
86 preflightFailure: Option.some({
87 reason: "WSL probe timed out",
88 fatal: false,
89 retryLimit: 12,
90 }),
91 };
92 const retryingInstance: DesktopBackendManager.DesktopBackendInstance = {
93 ...defaultWslInstance,
94 currentConfig: Effect.succeed(Option.some(retryingConfig)),
95 snapshot: Effect.succeed({
96 desiredRunning: true,
97 ready: false,
98 activePid: Option.none(),
99 restartAttempt: 2,
100 restartScheduled: true,
101 }),
102 };
103​
104 return Effect.gen(function* () {
105 const result = yield* getLocalEnvironmentBootstraps.handler();
106 assert.deepEqual(result, [
107 {
108 id: "wsl:default",
109 label: "WSL (default distro)",
110 runningDistro: null,
111 httpBaseUrl: null,
112 wsBaseUrl: null,
113 },
114 ]);
115 }).pipe(Effect.provide(DesktopBackendPool.layerTest([retryingInstance])));
116 });
117​
118 it.effect("omits a bounded transient bootstrap after retries stop", () => {
119 const stoppedInstance: DesktopBackendManager.DesktopBackendInstance = {
120 ...defaultWslInstance,
121 currentConfig: Effect.succeed(
122 Option.some({
123 ...readyWslConfig,
124 preflightFailure: Option.some({
125 reason: "WSL probe timed out",
126 fatal: false,
127 retryLimit: 12,
128 }),
129 }),
130 ),
131 snapshot: Effect.succeed({
132 desiredRunning: false,
133 ready: false,
134 activePid: Option.none(),
135 restartAttempt: 12,
136 restartScheduled: false,
137 }),
138 };
139​
140 return Effect.gen(function* () {
141 const result = yield* getLocalEnvironmentBootstraps.handler();
142 assert.deepEqual(result, []);
143 }).pipe(Effect.provide(DesktopBackendPool.layerTest([stoppedInstance])));
144 });
145});
146​
147describe("WSL picker distro identity", () => {
148 it.effect("pins wsl:default to the distro already running in the target instance", () =>
149 Effect.gen(function* () {
150 const distro = yield* resolveWslPickerDistro({
151 targetEnvironmentId: "wsl:default",
152 configuredDistro: null,
153 wslOnly: false,
154 });
155 assert.equal(distro, "Ubuntu");
156 }).pipe(Effect.provide(DesktopBackendPool.layerTest([defaultWslInstance]))),
157 );
158​
159 it.effect("uses the concrete primary distro for a WSL-only synthetic picker target", () =>
160 Effect.gen(function* () {
161 const distro = yield* resolveWslPickerDistro({
162 targetEnvironmentId: "wsl:default",
163 configuredDistro: null,
164 wslOnly: true,
165 });
166 assert.equal(distro, "Ubuntu");
167 }).pipe(Effect.provide(DesktopBackendPool.layerTest([primaryWslInstance]))),
168 );
169});
170​
171describe("WSL picker selection safety", () => {
172 it.effect("accepts a UNC path only when it belongs to the target distro", () =>
173 Effect.gen(function* () {
174 const result = yield* resolveWslPickerSelection({
175 selectedPath: "\\\\wsl.localhost\\ubuntu\\home\\alice\\repo",
176 targetDistro: "Ubuntu",
177 });
178 assert.deepEqual(result, { _tag: "Success", linuxPath: "/home/alice/repo" });
179 }).pipe(Effect.provide(DesktopWslEnvironment.layerTest())),
180 );
181​
182 it.effect("rejects a UNC path owned by another distro", () =>
183 Effect.gen(function* () {
184 const result = yield* resolveWslPickerSelection({
185 selectedPath: "\\\\wsl.localhost\\Debian\\home\\alice\\repo",
186 targetDistro: "Ubuntu",
187 });
188 assert.deepEqual(result, {
189 _tag: "CrossDistro",
190 selectedDistro: "Debian",
191 targetDistro: "Ubuntu",
192 });
193 }).pipe(Effect.provide(DesktopWslEnvironment.layerTest())),
194 );
195​
196 it.effect("fails closed when a Windows path cannot be converted", () =>
197 Effect.gen(function* () {
198 const result = yield* resolveWslPickerSelection({
199 selectedPath: "C:\\Users\\Alice\\repo",
200 targetDistro: "Ubuntu",
201 });
202 assert.deepEqual(result, { _tag: "ConversionFailed", targetDistro: "Ubuntu" });
203 }).pipe(
204 Effect.provide(
205 DesktopWslEnvironment.layerTest({
206 windowsToWslPath: () => Option.none(),
207 }),
208 ),
209 ),
210 );
211​
212 it.effect("converts Windows paths inside the concrete target distro", () => {
213 const calls: Array<readonly [string | null, string]> = [];
214 return Effect.gen(function* () {
215 const result = yield* resolveWslPickerSelection({
216 selectedPath: "C:\\Users\\Alice\\repo",
217 targetDistro: "Ubuntu",
218 });
219 assert.deepEqual(result, { _tag: "Success", linuxPath: "/mnt/c/Users/Alice/repo" });
220 assert.deepEqual(calls, [["Ubuntu", "C:\\Users\\Alice\\repo"]]);
221 }).pipe(
222 Effect.provide(
223 DesktopWslEnvironment.layerTest({
224 windowsToWslPath: (distro, windowsPath) => {
225 calls.push([distro, windowsPath]);
226 return Option.some("/mnt/c/Users/Alice/repo");
227 },
228 }),
229 ),
230 );
231 });
232});
233​
234describe("getWindowFullscreenState", () => {
235 it.effect("reads the current native window state", () => {
236 const window = { isFullScreen: () => true } as Electron.BrowserWindow;
237​
238 return Effect.gen(function* () {
239 assert.isTrue(yield* getWindowFullscreenState.handler());
240 }).pipe(
241 Effect.provide(
242 Layer.mock(ElectronWindow.ElectronWindow)({
243 currentMainOrFirst: Effect.succeed(Option.some(window)),
244 }),
245 ),
246 );
247 });
248});