Open History to see the last commit
1import { DesktopWslStateSchema } from "@t3tools/contracts";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6
7import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
8import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
9import * as DesktopShutdown from "../../app/DesktopShutdown.ts";
10import * as DesktopState from "../../app/DesktopState.ts";
11import * as ElectronApp from "../../electron/ElectronApp.ts";
12import * as ElectronDialog from "../../electron/ElectronDialog.ts";
13import * as ElectronTheme from "../../electron/ElectronTheme.ts";
14import * as ElectronWindow from "../../electron/ElectronWindow.ts";
15import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
16import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
17import * as DesktopWindow from "../../window/DesktopWindow.ts";
18import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
19import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
20import { getWslState, setWslBackendEnabled, setWslDistro, setWslOnly } from "./wsl.ts";
21
22const decodeWslState = Schema.decodeUnknownEffect(DesktopWslStateSchema);
23
24const invokeSetWslBackendEnabled = (enabled: boolean) =>
25 setWslBackendEnabled.handler(enabled).pipe(Effect.flatMap(decodeWslState));
26const invokeSetWslDistro = (distro: string | null) =>
27 setWslDistro.handler(distro).pipe(Effect.flatMap(decodeWslState));
28const invokeSetWslOnly = (enabled: boolean) =>
29 setWslOnly.handler(enabled).pipe(Effect.flatMap(decodeWslState));
30
31function layerWslBackend(input: { readonly onReconcile?: Effect.Effect<void> } = {}) {
32 return Layer.succeed(
33 DesktopWslBackend.DesktopWslBackend,
34 DesktopWslBackend.DesktopWslBackend.of({
35 reconcile: input.onReconcile ?? Effect.void,
36 lastPreflightError: Effect.succeedNone,
37 }),
38 );
39}
40
41function layerLifecycle(relaunchReasons: Array<string>) {
42 return Layer.succeed(
43 DesktopLifecycle.DesktopLifecycle,
44 DesktopLifecycle.DesktopLifecycle.of({
45 relaunch: (reason) =>
46 Effect.sync(() => {
47 relaunchReasons.push(reason);
48 }),
49 register: Effect.void,
50 }),
51 );
52}
53
54const layerUnusedLifecycleRuntime = Layer.mergeAll(
55 DesktopShutdown.layer,
56 DesktopState.layer,
57 Layer.succeed(
58 DesktopEnvironment.DesktopEnvironment,
59 DesktopEnvironment.DesktopEnvironment.of(
60 {} as DesktopEnvironment.DesktopEnvironment["Service"],
61 ),
62 ),
63 Layer.succeed(
64 DesktopWindow.DesktopWindow,
65 DesktopWindow.DesktopWindow.of({} as DesktopWindow.DesktopWindow["Service"]),
66 ),
67 Layer.succeed(
68 ElectronApp.ElectronApp,
69 ElectronApp.ElectronApp.of({} as ElectronApp.ElectronApp["Service"]),
70 ),
71 Layer.succeed(
72 ElectronTheme.ElectronTheme,
73 ElectronTheme.ElectronTheme.of({} as ElectronTheme.ElectronTheme["Service"]),
74 ),
75 Layer.succeed(
76 ElectronDialog.ElectronDialog,
77 ElectronDialog.ElectronDialog.of({} as ElectronDialog.ElectronDialog["Service"]),
78 ),
79 Layer.succeed(
80 ElectronWindow.ElectronWindow,
81 ElectronWindow.ElectronWindow.of({} as ElectronWindow.ElectronWindow["Service"]),
82 ),
83 DesktopClientSettings.layerTest(),
84);
85
86describe("WSL IPC", () => {
87 it.effect("does not probe WSL when local execution is disabled", () =>
88 Effect.gen(function* () {
89 const wsl = yield* DesktopWslEnvironment.DesktopWslEnvironment;
90 const state = yield* getWslState.handler(undefined).pipe(
91 Effect.provideService(DesktopWslEnvironment.DesktopWslEnvironment, {
92 ...wsl,
93 isAvailable: Effect.die("must not probe WSL"),
94 listDistros: Effect.die("must not enumerate distros"),
95 }),
96 Effect.flatMap(decodeWslState),
97 );
98 assert.deepEqual(state, {
99 enabled: true,
100 distro: "Ubuntu",
101 available: false,
102 wslOnly: true,
103 distros: [],
104 preflightError: null,
105 });
106 }).pipe(
107 Effect.provide(
108 Layer.mergeAll(
109 DesktopAppSettings.layerTest({
110 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
111 localEnvironmentEnabled: false,
112 wslBackendEnabled: true,
113 wslDistro: "Ubuntu",
114 wslOnly: true,
115 }),
116 DesktopWslEnvironment.layerTest(),
117 layerWslBackend(),
118 ),
119 ),
120 ),
121 );
122
123 it.effect("does not enumerate distros until the WSL backend is enabled", () => {
124 const distros = [
125 { name: "Ubuntu", isDefault: true, version: 2 as const },
126 { name: "Debian", isDefault: false, version: 2 as const },
127 ];
128 const layer = Layer.mergeAll(
129 DesktopAppSettings.layerTest({
130 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
131 wslDistro: "Debian",
132 }),
133 DesktopWslEnvironment.layerTest({ isAvailable: true, distros }),
134 layerWslBackend(),
135 layerLifecycle([]),
136 layerUnusedLifecycleRuntime,
137 );
138
139 return Effect.gen(function* () {
140 const wsl = yield* DesktopWslEnvironment.DesktopWslEnvironment;
141 const state = yield* getWslState.handler(undefined).pipe(
142 Effect.provideService(DesktopWslEnvironment.DesktopWslEnvironment, {
143 ...wsl,
144 listDistros: Effect.die("must not enumerate distros"),
145 }),
146 Effect.flatMap(decodeWslState),
147 );
148 assert.equal(state.enabled, false);
149 assert.equal(state.available, true);
150 assert.equal(state.distro, "Debian");
151 assert.deepEqual(state.distros, []);
152
153 const enabledState = yield* invokeSetWslBackendEnabled(true);
154 assert.equal(enabledState.enabled, true);
155 assert.equal(enabledState.distro, "Debian");
156 assert.deepEqual(enabledState.distros, distros);
157 }).pipe(Effect.provide(layer));
158 });
159
160 it.effect("stages dual-backend preferences before enabling without relaunching", () => {
161 const relaunchReasons: Array<string> = [];
162 const layer = Layer.mergeAll(
163 DesktopAppSettings.layerTest({
164 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
165 wslBackendEnabled: false,
166 wslOnly: true,
167 }),
168 DesktopWslEnvironment.layerTest({ isAvailable: true }),
169 layerWslBackend(),
170 layerLifecycle(relaunchReasons),
171 layerUnusedLifecycleRuntime,
172 );
173
174 return Effect.gen(function* () {
175 yield* invokeSetWslOnly(false);
176 yield* invokeSetWslDistro("Debian");
177 const state = yield* invokeSetWslBackendEnabled(true);
178
179 assert.deepEqual(state, {
180 enabled: true,
181 distro: "Debian",
182 available: true,
183 wslOnly: false,
184 distros: [],
185 preflightError: null,
186 });
187 assert.deepEqual(relaunchReasons, []);
188 }).pipe(Effect.provide(layer));
189 });
190
191 it.effect("stages WSL-only preferences and relaunches only after enabling", () => {
192 const relaunchReasons: Array<string> = [];
193 const layer = Layer.mergeAll(
194 DesktopAppSettings.layerTest({
195 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
196 wslBackendEnabled: false,
197 wslOnly: false,
198 }),
199 DesktopWslEnvironment.layerTest({ isAvailable: true }),
200 layerWslBackend(),
201 layerLifecycle(relaunchReasons),
202 layerUnusedLifecycleRuntime,
203 );
204
205 return Effect.gen(function* () {
206 const stagedMode = yield* invokeSetWslOnly(true);
207 assert.equal(stagedMode.enabled, false);
208 assert.equal(stagedMode.wslOnly, true);
209 assert.deepEqual(relaunchReasons, []);
210
211 yield* invokeSetWslDistro("Debian");
212 assert.deepEqual(relaunchReasons, []);
213
214 const state = yield* invokeSetWslBackendEnabled(true);
215 assert.deepEqual(state, {
216 enabled: true,
217 distro: "Debian",
218 available: true,
219 wslOnly: true,
220 distros: [],
221 preflightError: null,
222 });
223 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=true"]);
224 }).pipe(Effect.provide(layer));
225 });
226
227 it.effect("relaunches when enabling the WSL backend while wsl-only is already persisted", () => {
228 const relaunchReasons: Array<string> = [];
229 let reconcileCount = 0;
230 const layer = Layer.mergeAll(
231 DesktopAppSettings.layerTest({
232 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
233 wslBackendEnabled: false,
234 wslOnly: true,
235 }),
236 DesktopWslEnvironment.layerTest({ isAvailable: true }),
237 layerWslBackend({
238 onReconcile: Effect.sync(() => {
239 reconcileCount += 1;
240 }),
241 }),
242 layerLifecycle(relaunchReasons),
243 layerUnusedLifecycleRuntime,
244 );
245
246 return Effect.gen(function* () {
247 const state = yield* invokeSetWslBackendEnabled(true);
248
249 assert.deepEqual(state, {
250 enabled: true,
251 distro: null,
252 available: true,
253 wslOnly: true,
254 distros: [],
255 preflightError: null,
256 });
257 assert.equal(reconcileCount, 0);
258 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=true"]);
259 }).pipe(Effect.provide(layer));
260 });
261
262 it.effect("reconciles in dual-backend mode without relaunching", () => {
263 const relaunchReasons: Array<string> = [];
264 let reconcileCount = 0;
265 const layer = Layer.mergeAll(
266 DesktopAppSettings.layerTest({
267 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
268 wslBackendEnabled: false,
269 wslOnly: false,
270 }),
271 DesktopWslEnvironment.layerTest({ isAvailable: true }),
272 layerWslBackend({
273 onReconcile: Effect.sync(() => {
274 reconcileCount += 1;
275 }),
276 }),
277 layerLifecycle(relaunchReasons),
278 layerUnusedLifecycleRuntime,
279 );
280
281 return Effect.gen(function* () {
282 const state = yield* invokeSetWslBackendEnabled(true);
283
284 assert.equal(state.enabled, true);
285 assert.equal(state.wslOnly, false);
286 assert.equal(reconcileCount, 1);
287 assert.deepEqual(relaunchReasons, []);
288 }).pipe(Effect.provide(layer));
289 });
290
291 it.effect("clears wsl-only before relaunching when disabling a WSL-only backend", () => {
292 const relaunchReasons: Array<string> = [];
293 let reconcileCount = 0;
294 const layer = Layer.mergeAll(
295 DesktopAppSettings.layerTest({
296 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
297 wslBackendEnabled: true,
298 wslOnly: true,
299 }),
300 DesktopWslEnvironment.layerTest({ isAvailable: true }),
301 layerWslBackend({
302 onReconcile: Effect.sync(() => {
303 reconcileCount += 1;
304 }),
305 }),
306 layerLifecycle(relaunchReasons),
307 layerUnusedLifecycleRuntime,
308 );
309
310 return Effect.gen(function* () {
311 const state = yield* invokeSetWslBackendEnabled(false);
312 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
313 const settings = yield* appSettings.get;
314
315 assert.deepEqual(state, {
316 enabled: false,
317 distro: null,
318 available: true,
319 wslOnly: false,
320 distros: [],
321 preflightError: null,
322 });
323 assert.equal(settings.wslBackendEnabled, false);
324 assert.equal(settings.wslOnly, false);
325 assert.equal(reconcileCount, 0);
326 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=false"]);
327 }).pipe(Effect.provide(layer));
328 });
329
330 it.effect("clears dual-backend WSL without relaunching", () => {
331 const relaunchReasons: Array<string> = [];
332 let reconcileCount = 0;
333 const layer = Layer.mergeAll(
334 DesktopAppSettings.layerTest({
335 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
336 wslBackendEnabled: true,
337 wslOnly: false,
338 }),
339 DesktopWslEnvironment.layerTest({ isAvailable: true }),
340 layerWslBackend({
341 onReconcile: Effect.sync(() => {
342 reconcileCount += 1;
343 }),
344 }),
345 layerLifecycle(relaunchReasons),
346 layerUnusedLifecycleRuntime,
347 );
348
349 return Effect.gen(function* () {
350 const state = yield* invokeSetWslBackendEnabled(false);
351
352 assert.equal(state.enabled, false);
353 assert.equal(state.wslOnly, false);
354 assert.equal(reconcileCount, 1);
355 assert.deepEqual(relaunchReasons, []);
356 }).pipe(Effect.provide(layer));
357 });
358});