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 Option from "effect/Option";
6import * as Schema from "effect/Schema";
7
8import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
9import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
10import * as DesktopShutdown from "../../app/DesktopShutdown.ts";
11import * as DesktopState from "../../app/DesktopState.ts";
12import * as ElectronApp from "../../electron/ElectronApp.ts";
13import * as ElectronDialog from "../../electron/ElectronDialog.ts";
14import * as ElectronTheme from "../../electron/ElectronTheme.ts";
15import * as ElectronWindow from "../../electron/ElectronWindow.ts";
16import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
17import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
18import * as DesktopWindow from "../../window/DesktopWindow.ts";
19import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
20import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
21import { setWslBackendEnabled, setWslDistro, setWslOnly } from "./wsl.ts";
22
23const decodeWslState = Schema.decodeUnknownEffect(DesktopWslStateSchema);
24
25const invokeSetWslBackendEnabled = (enabled: boolean) =>
26 setWslBackendEnabled.handler(enabled).pipe(Effect.flatMap(decodeWslState));
27const invokeSetWslDistro = (distro: string | null) =>
28 setWslDistro.handler(distro).pipe(Effect.flatMap(decodeWslState));
29const invokeSetWslOnly = (enabled: boolean) =>
30 setWslOnly.handler(enabled).pipe(Effect.flatMap(decodeWslState));
31
32function makeWslBackendLayer(input: { readonly onReconcile?: Effect.Effect<void> } = {}) {
33 return Layer.succeed(
34 DesktopWslBackend.DesktopWslBackend,
35 DesktopWslBackend.DesktopWslBackend.of({
36 reconcile: input.onReconcile ?? Effect.void,
37 lastPreflightError: Effect.succeed(Option.none()),
38 }),
39 );
40}
41
42function makeLifecycleLayer(relaunchReasons: Array<string>) {
43 return Layer.succeed(
44 DesktopLifecycle.DesktopLifecycle,
45 DesktopLifecycle.DesktopLifecycle.of({
46 relaunch: (reason) =>
47 Effect.sync(() => {
48 relaunchReasons.push(reason);
49 }),
50 register: Effect.void,
51 }),
52 );
53}
54
55const unusedLifecycleRuntimeLayer = Layer.mergeAll(
56 DesktopShutdown.layer,
57 DesktopState.layer,
58 Layer.succeed(
59 DesktopEnvironment.DesktopEnvironment,
60 DesktopEnvironment.DesktopEnvironment.of(
61 {} as DesktopEnvironment.DesktopEnvironment["Service"],
62 ),
63 ),
64 Layer.succeed(
65 DesktopWindow.DesktopWindow,
66 DesktopWindow.DesktopWindow.of({} as DesktopWindow.DesktopWindow["Service"]),
67 ),
68 Layer.succeed(
69 ElectronApp.ElectronApp,
70 ElectronApp.ElectronApp.of({} as ElectronApp.ElectronApp["Service"]),
71 ),
72 Layer.succeed(
73 ElectronTheme.ElectronTheme,
74 ElectronTheme.ElectronTheme.of({} as ElectronTheme.ElectronTheme["Service"]),
75 ),
76 Layer.succeed(
77 ElectronDialog.ElectronDialog,
78 ElectronDialog.ElectronDialog.of({} as ElectronDialog.ElectronDialog["Service"]),
79 ),
80 Layer.succeed(
81 ElectronWindow.ElectronWindow,
82 ElectronWindow.ElectronWindow.of({} as ElectronWindow.ElectronWindow["Service"]),
83 ),
84 DesktopClientSettings.layerTest(),
85);
86
87describe("WSL IPC", () => {
88 it.effect("stages dual-backend preferences before enabling without relaunching", () => {
89 const relaunchReasons: Array<string> = [];
90 const layer = Layer.mergeAll(
91 DesktopAppSettings.layerTest({
92 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
93 wslBackendEnabled: false,
94 wslOnly: true,
95 }),
96 DesktopWslEnvironment.layerTest({ isAvailable: true }),
97 makeWslBackendLayer(),
98 makeLifecycleLayer(relaunchReasons),
99 unusedLifecycleRuntimeLayer,
100 );
101
102 return Effect.gen(function* () {
103 yield* invokeSetWslOnly(false);
104 yield* invokeSetWslDistro("Debian");
105 const state = yield* invokeSetWslBackendEnabled(true);
106
107 assert.deepEqual(state, {
108 enabled: true,
109 distro: "Debian",
110 available: true,
111 wslOnly: false,
112 distros: [],
113 preflightError: null,
114 });
115 assert.deepEqual(relaunchReasons, []);
116 }).pipe(Effect.provide(layer));
117 });
118
119 it.effect("stages WSL-only preferences and relaunches only after enabling", () => {
120 const relaunchReasons: Array<string> = [];
121 const layer = Layer.mergeAll(
122 DesktopAppSettings.layerTest({
123 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
124 wslBackendEnabled: false,
125 wslOnly: false,
126 }),
127 DesktopWslEnvironment.layerTest({ isAvailable: true }),
128 makeWslBackendLayer(),
129 makeLifecycleLayer(relaunchReasons),
130 unusedLifecycleRuntimeLayer,
131 );
132
133 return Effect.gen(function* () {
134 const stagedMode = yield* invokeSetWslOnly(true);
135 assert.equal(stagedMode.enabled, false);
136 assert.equal(stagedMode.wslOnly, true);
137 assert.deepEqual(relaunchReasons, []);
138
139 yield* invokeSetWslDistro("Debian");
140 assert.deepEqual(relaunchReasons, []);
141
142 const state = yield* invokeSetWslBackendEnabled(true);
143 assert.deepEqual(state, {
144 enabled: true,
145 distro: "Debian",
146 available: true,
147 wslOnly: true,
148 distros: [],
149 preflightError: null,
150 });
151 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=true"]);
152 }).pipe(Effect.provide(layer));
153 });
154
155 it.effect("relaunches when enabling the WSL backend while wsl-only is already persisted", () => {
156 const relaunchReasons: Array<string> = [];
157 let reconcileCount = 0;
158 const layer = Layer.mergeAll(
159 DesktopAppSettings.layerTest({
160 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
161 wslBackendEnabled: false,
162 wslOnly: true,
163 }),
164 DesktopWslEnvironment.layerTest({ isAvailable: true }),
165 makeWslBackendLayer({
166 onReconcile: Effect.sync(() => {
167 reconcileCount += 1;
168 }),
169 }),
170 makeLifecycleLayer(relaunchReasons),
171 unusedLifecycleRuntimeLayer,
172 );
173
174 return Effect.gen(function* () {
175 const state = yield* invokeSetWslBackendEnabled(true);
176
177 assert.deepEqual(state, {
178 enabled: true,
179 distro: null,
180 available: true,
181 wslOnly: true,
182 distros: [],
183 preflightError: null,
184 });
185 assert.equal(reconcileCount, 0);
186 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=true"]);
187 }).pipe(Effect.provide(layer));
188 });
189
190 it.effect("reconciles in dual-backend mode without relaunching", () => {
191 const relaunchReasons: Array<string> = [];
192 let reconcileCount = 0;
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 makeWslBackendLayer({
201 onReconcile: Effect.sync(() => {
202 reconcileCount += 1;
203 }),
204 }),
205 makeLifecycleLayer(relaunchReasons),
206 unusedLifecycleRuntimeLayer,
207 );
208
209 return Effect.gen(function* () {
210 const state = yield* invokeSetWslBackendEnabled(true);
211
212 assert.equal(state.enabled, true);
213 assert.equal(state.wslOnly, false);
214 assert.equal(reconcileCount, 1);
215 assert.deepEqual(relaunchReasons, []);
216 }).pipe(Effect.provide(layer));
217 });
218
219 it.effect("clears wsl-only before relaunching when disabling a WSL-only backend", () => {
220 const relaunchReasons: Array<string> = [];
221 let reconcileCount = 0;
222 const layer = Layer.mergeAll(
223 DesktopAppSettings.layerTest({
224 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
225 wslBackendEnabled: true,
226 wslOnly: true,
227 }),
228 DesktopWslEnvironment.layerTest({ isAvailable: true }),
229 makeWslBackendLayer({
230 onReconcile: Effect.sync(() => {
231 reconcileCount += 1;
232 }),
233 }),
234 makeLifecycleLayer(relaunchReasons),
235 unusedLifecycleRuntimeLayer,
236 );
237
238 return Effect.gen(function* () {
239 const state = yield* invokeSetWslBackendEnabled(false);
240 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
241 const settings = yield* appSettings.get;
242
243 assert.deepEqual(state, {
244 enabled: false,
245 distro: null,
246 available: true,
247 wslOnly: false,
248 distros: [],
249 preflightError: null,
250 });
251 assert.equal(settings.wslBackendEnabled, false);
252 assert.equal(settings.wslOnly, false);
253 assert.equal(reconcileCount, 0);
254 assert.deepEqual(relaunchReasons, ["wslBackendEnabled=false"]);
255 }).pipe(Effect.provide(layer));
256 });
257
258 it.effect("clears dual-backend WSL without relaunching", () => {
259 const relaunchReasons: Array<string> = [];
260 let reconcileCount = 0;
261 const layer = Layer.mergeAll(
262 DesktopAppSettings.layerTest({
263 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
264 wslBackendEnabled: true,
265 wslOnly: false,
266 }),
267 DesktopWslEnvironment.layerTest({ isAvailable: true }),
268 makeWslBackendLayer({
269 onReconcile: Effect.sync(() => {
270 reconcileCount += 1;
271 }),
272 }),
273 makeLifecycleLayer(relaunchReasons),
274 unusedLifecycleRuntimeLayer,
275 );
276
277 return Effect.gen(function* () {
278 const state = yield* invokeSetWslBackendEnabled(false);
279
280 assert.equal(state.enabled, false);
281 assert.equal(state.wslOnly, false);
282 assert.equal(reconcileCount, 1);
283 assert.deepEqual(relaunchReasons, []);
284 }).pipe(Effect.provide(layer));
285 });
286});