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

wsl.test.ts

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