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

wsl.test.ts

280 lines9.2 KB

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