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

DesktopAppSettings.test.ts

434 lines16.4 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Schema from "effect/Schema";
8import * as Tracer from "effect/Tracer";
9​
10import * as DesktopConfig from "../app/DesktopConfig.ts";
11import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
12import * as DesktopAppSettings from "./DesktopAppSettings.ts";
13​
14const DesktopSettingsPatch = Schema.Struct({
15 linuxPasswordStore: Schema.optionalKey(
16 Schema.Literals(["auto", "gnome-libsecret", "kwallet", "kwallet5", "kwallet6"]),
17 ),
18 mainWindowBounds: Schema.optionalKey(
19 Schema.NullOr(
20 Schema.Struct({
21 x: Schema.Number,
22 y: Schema.Number,
23 width: Schema.Number,
24 height: Schema.Number,
25 }),
26 ),
27 ),
28 mainWindowMaximized: Schema.optionalKey(Schema.Boolean),
29 serverExposureMode: Schema.optionalKey(Schema.Literals(["local-only", "network-accessible"])),
30 tailscaleServeEnabled: Schema.optionalKey(Schema.Boolean),
31 tailscaleServePort: Schema.optionalKey(Schema.Number),
32 updateChannel: Schema.optionalKey(Schema.Literals(["latest", "nightly"])),
33 updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean),
34 wslBackendEnabled: Schema.optionalKey(Schema.Boolean),
35 wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])),
36 wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)),
37 wslOnly: Schema.optionalKey(Schema.Boolean),
38});
39​
40const decodeDesktopSettingsPatch = Schema.decodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
41const encodeDesktopSettingsPatch = Schema.encodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
42​
43function layerEnvironment(baseDir: string, appVersion = "0.0.17") {
44 return DesktopEnvironment.layer({
45 dirname: "/repo/apps/desktop/src",
46 homeDirectory: baseDir,
47 platform: "darwin",
48 processArch: "x64",
49 appVersion,
50 appPath: "/repo",
51 isPackaged: true,
52 resourcesPath: "/missing/resources",
53 runningUnderArm64Translation: false,
54 }).pipe(
55 Layer.provide(
56 Layer.mergeAll(
57 NodeServices.layer,
58 NodeCrypto.layer,
59 DesktopConfig.layerTest({ T3CODE_HOME: baseDir }),
60 ),
61 ),
62 );
64​
65const withSettings = <A, E, R>(
66 effect: Effect.Effect<
67 A,
68 E,
69 R | DesktopAppSettings.DesktopAppSettings | DesktopEnvironment.DesktopEnvironment
70 >,
71 options?: { readonly appVersion?: string },
72) =>
73 Effect.gen(function* () {
74 const fileSystem = yield* FileSystem.FileSystem;
75 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
76 prefix: "t3-desktop-settings-test-",
77 });
78 return yield* effect.pipe(
79 Effect.provide(
80 DesktopAppSettings.layer.pipe(
81 Layer.provideMerge(layerEnvironment(baseDir, options?.appVersion)),
82 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
83 ),
84 ),
85 );
86 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Effect.scoped);
87​
88function writeSettingsPatch(patch: typeof DesktopSettingsPatch.Type) {
89 return Effect.gen(function* () {
90 const environment = yield* DesktopEnvironment.DesktopEnvironment;
91 const fileSystem = yield* FileSystem.FileSystem;
92 const encoded = yield* encodeDesktopSettingsPatch(patch);
93 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
94 yield* fileSystem.writeFileString(environment.desktopSettingsPath, `${encoded}\n`);
95 });
97​
98describe("DesktopSettings", () => {
99 it.effect("records one bounds operation span with its input attributes", () =>
100 Effect.gen(function* () {
101 const spans: Array<Tracer.Span> = [];
102 const tracer = Tracer.make({
103 span: (options) => {
104 const span = new Tracer.NativeSpan(options);
105 spans.push(span);
106 return span;
107 },
108 });
109 yield* withSettings(
110 Effect.gen(function* () {
111 const settings = yield* DesktopAppSettings.DesktopAppSettings;
112 yield* settings.setMainWindowBounds({ x: 10, y: 20, width: 900, height: 600 }, true);
113 }),
114 ).pipe(Effect.provideService(Tracer.Tracer, tracer));
115 const operationSpans = spans.filter(
116 (span) => span.name === "desktop.settings.setMainWindowBounds",
117 );
118 assert.lengthOf(operationSpans, 1);
119 assert.deepEqual(Object.fromEntries(operationSpans[0]!.attributes), {
120 x: 10,
121 y: 20,
122 width: 900,
123 height: 600,
124 isMaximized: true,
125 });
126 assert.equal(operationSpans[0]!.status._tag, "Ended");
127 }),
128 );
129​
130 it.effect(
131 "persists disabling and re-enabling local execution without clearing backend settings",
132 () =>
133 withSettings(
134 Effect.gen(function* () {
135 const settings = yield* DesktopAppSettings.DesktopAppSettings;
136 yield* settings.setWslBackendEnabled(true);
137 yield* settings.setWslDistro("Ubuntu");
138 yield* settings.setServerExposureMode("network-accessible");
139 const before = yield* settings.get;
140 assert.isTrue((yield* settings.setLocalEnvironmentEnabled(false)).changed);
141 assert.deepEqual(yield* settings.load, { ...before, localEnvironmentEnabled: false });
142 assert.isFalse((yield* settings.setLocalEnvironmentEnabled(false)).changed);
143 yield* settings.setLocalEnvironmentEnabled(true);
144 assert.deepEqual(yield* settings.load, before);
145 }),
146 ),
147 );
148​
149 it.effect("falls back to defaults when the settings file is malformed", () =>
150 withSettings(
151 Effect.gen(function* () {
152 const environment = yield* DesktopEnvironment.DesktopEnvironment;
153 const fileSystem = yield* FileSystem.FileSystem;
154 const settings = yield* DesktopAppSettings.DesktopAppSettings;
155 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
156 yield* fileSystem.writeFileString(environment.desktopSettingsPath, "{not-json");
157​
158 assert.deepEqual(yield* settings.load, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
159 }),
160 ),
161 );
162​
163 it.effect("loads lenient persisted desktop settings JSON", () =>
164 withSettings(
165 Effect.gen(function* () {
166 const environment = yield* DesktopEnvironment.DesktopEnvironment;
167 const fileSystem = yield* FileSystem.FileSystem;
168 const settings = yield* DesktopAppSettings.DesktopAppSettings;
169 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
170 yield* fileSystem.writeFileString(
171 environment.desktopSettingsPath,
172 `{
173 // JSONC-style comments and trailing commas match server settings parsing.
174 "serverExposureMode": "network-accessible",
175 "tailscaleServeEnabled": true,
176 "tailscaleServePort": 8443,
177 "mainWindowBounds": { "x": 120, "y": 80, "width": 1280, "height": 900 },
178 }\n`,
179 );
180​
181 assert.deepEqual(yield* settings.load, {
182 linuxPasswordStore: "auto",
183 localEnvironmentEnabled: true,
184 mainWindowBounds: { x: 120, y: 80, width: 1280, height: 900 },
185 mainWindowMaximized: false,
186 serverExposureMode: "network-accessible",
187 tailscaleServeEnabled: true,
188 tailscaleServePort: 8443,
189 updateChannel: "latest",
190 updateChannelConfiguredByUser: false,
191 wslBackendEnabled: false,
192 wslOnly: false,
193 wslDistro: null,
194 } satisfies DesktopAppSettings.DesktopSettings);
195 }),
196 ),
197 );
198​
199 it.effect("rejects window bounds that do not satisfy the domain schema", () =>
200 withSettings(
201 Effect.gen(function* () {
202 const settings = yield* DesktopAppSettings.DesktopAppSettings;
203 for (const bounds of [
204 { x: 10.5, y: 20, width: 400, height: 360 },
205 { x: 10, y: 20, width: 399, height: 360 },
206 { x: 10, y: 20, width: 400, height: 359 },
207 ]) {
208 yield* writeSettingsPatch({
209 mainWindowBounds: bounds,
210 mainWindowMaximized: true,
211 serverExposureMode: "network-accessible",
212 });
213​
214 const loaded = yield* settings.load;
215 assert.isNull(loaded.mainWindowBounds);
216 assert.isFalse(loaded.mainWindowMaximized);
217 assert.equal(loaded.serverExposureMode, "network-accessible");
218 }
219 }),
220 ),
221 );
222​
223 it.effect("persists sparse desktop settings documents", () =>
224 withSettings(
225 Effect.gen(function* () {
226 const environment = yield* DesktopEnvironment.DesktopEnvironment;
227 const fileSystem = yield* FileSystem.FileSystem;
228 const settings = yield* DesktopAppSettings.DesktopAppSettings;
229​
230 yield* settings.setMainWindowBounds({ x: -1200, y: 40, width: 400, height: 360 }, true);
231 yield* settings.setServerExposureMode("network-accessible");
232​
233 const persisted = yield* decodeDesktopSettingsPatch(
234 yield* fileSystem.readFileString(environment.desktopSettingsPath),
235 );
236 assert.deepEqual(persisted, {
237 mainWindowBounds: { x: -1200, y: 40, width: 400, height: 360 },
238 mainWindowMaximized: true,
239 serverExposureMode: "network-accessible",
240 } satisfies typeof DesktopSettingsPatch.Type);
241​
242 const restored = yield* Effect.gen(function* () {
243 const freshSettings = yield* DesktopAppSettings.DesktopAppSettings;
244 return yield* freshSettings.load;
245 }).pipe(Effect.provide(DesktopAppSettings.layer));
246 assert.deepEqual(restored.mainWindowBounds, persisted.mainWindowBounds);
247 assert.isTrue(restored.mainWindowMaximized);
248 }),
249 ),
250 );
251​
252 it.effect("saves through a symlinked settings file without replacing the link", () =>
253 withSettings(
254 Effect.gen(function* () {
255 const environment = yield* DesktopEnvironment.DesktopEnvironment;
256 const fileSystem = yield* FileSystem.FileSystem;
257 const settings = yield* DesktopAppSettings.DesktopAppSettings;
258 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
259 prefix: "t3-desktop-settings-dotfiles-",
260 });
261 const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
262 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
263 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
264 yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);
265​
266 yield* settings.setServerExposureMode("network-accessible");
267​
268 assert.equal(
269 yield* fileSystem.readLink(environment.desktopSettingsPath),
270 linkedSettingsPath,
271 );
272 const persisted = yield* decodeDesktopSettingsPatch(
273 yield* fileSystem.readFileString(linkedSettingsPath),
274 );
275 assert.equal(persisted.serverExposureMode, "network-accessible");
276 }),
277 ),
278 );
279​
280 it.effect("removes its temporary file when replacing the settings file fails", () =>
281 withSettings(
282 Effect.gen(function* () {
283 const environment = yield* DesktopEnvironment.DesktopEnvironment;
284 const fileSystem = yield* FileSystem.FileSystem;
285 const settings = yield* DesktopAppSettings.DesktopAppSettings;
286 yield* fileSystem.makeDirectory(`${environment.desktopSettingsPath}/occupied`, {
287 recursive: true,
288 });
289​
290 const error = yield* settings.setServerExposureMode("network-accessible").pipe(Effect.flip);
291​
292 assert.equal(error.operation, "replace-settings-file");
293 assert.deepEqual(
294 (yield* fileSystem.readDirectory(environment.stateDir)).filter((name) =>
295 name.endsWith(".tmp"),
296 ),
297 [],
298 );
299 }),
300 ),
301 );
302​
303 it.effect("migrates legacy implicit update channels to the runtime default", () =>
304 withSettings(
305 Effect.gen(function* () {
306 const settings = yield* DesktopAppSettings.DesktopAppSettings;
307 yield* writeSettingsPatch({
308 serverExposureMode: "local-only",
309 updateChannel: "latest",
310 });
311​
312 assert.deepEqual(yield* settings.load, {
313 linuxPasswordStore: "auto",
314 localEnvironmentEnabled: true,
315 mainWindowBounds: null,
316 mainWindowMaximized: false,
317 serverExposureMode: "local-only",
318 tailscaleServeEnabled: false,
319 tailscaleServePort: 443,
320 updateChannel: "nightly",
321 updateChannelConfiguredByUser: false,
322 wslBackendEnabled: false,
323 wslOnly: false,
324 wslDistro: null,
325 } satisfies DesktopAppSettings.DesktopSettings);
326 }),
327 { appVersion: "0.0.17-nightly.20260415.1" },
328 ),
329 );
330​
331 it.effect("preserves explicit stable update channel on nightly builds", () =>
332 withSettings(
333 Effect.gen(function* () {
334 const settings = yield* DesktopAppSettings.DesktopAppSettings;
335 yield* writeSettingsPatch({
336 serverExposureMode: "local-only",
337 updateChannel: "latest",
338 updateChannelConfiguredByUser: true,
339 });
340​
341 assert.deepEqual(yield* settings.load, {
342 linuxPasswordStore: "auto",
343 localEnvironmentEnabled: true,
344 mainWindowBounds: null,
345 mainWindowMaximized: false,
346 serverExposureMode: "local-only",
347 tailscaleServeEnabled: false,
348 tailscaleServePort: 443,
349 updateChannel: "latest",
350 updateChannelConfiguredByUser: true,
351 wslBackendEnabled: false,
352 wslOnly: false,
353 wslDistro: null,
354 } satisfies DesktopAppSettings.DesktopSettings);
355 }),
356 { appVersion: "0.0.17-nightly.20260415.1" },
357 ),
358 );
359​
360 it.effect("persists wsl backend toggle and normalizes invalid distro names", () =>
361 withSettings(
362 Effect.gen(function* () {
363 const settings = yield* DesktopAppSettings.DesktopAppSettings;
364 const enable = yield* settings.setWslBackendEnabled(true);
365 assert.isTrue(enable.changed);
366 assert.equal(enable.settings.wslBackendEnabled, true);
367​
368 const distro = yield* settings.setWslDistro("Ubuntu-22.04");
369 assert.isTrue(distro.changed);
370 assert.equal(distro.settings.wslDistro, "Ubuntu-22.04");
371​
372 const reloaded = yield* settings.load;
373 assert.equal(reloaded.wslBackendEnabled, true);
374 assert.equal(reloaded.wslDistro, "Ubuntu-22.04");
375​
376 const reject = yield* settings.setWslDistro("bad name!");
377 assert.equal(reject.settings.wslDistro, null);
378​
379 const noop = yield* settings.setWslDistro(null);
380 assert.isFalse(noop.changed);
381 }),
382 ),
383 );
384​
385 it.effect("applies WSL Windows fallback with persisted and volatile updates", () =>
386 withSettings(
387 Effect.gen(function* () {
388 const settings = yield* DesktopAppSettings.DesktopAppSettings;
389 yield* settings.setWslBackendEnabled(true);
390 yield* settings.setWslOnly(true);
391​
392 const persistedFallback = yield* settings.applyWslWindowsFallback;
393 assert.isTrue(persistedFallback.changed);
394 assert.equal(persistedFallback.settings.wslBackendEnabled, false);
395 assert.equal(persistedFallback.settings.wslOnly, false);
396​
397 const persistedReload = yield* settings.load;
398 assert.equal(persistedReload.wslBackendEnabled, false);
399 assert.equal(persistedReload.wslOnly, false);
400​
401 yield* settings.setWslBackendEnabled(true);
402 yield* settings.setWslOnly(true);
403​
404 const volatileFallback = yield* settings.applyWslWindowsFallbackInMemory;
405 assert.isTrue(volatileFallback.changed);
406 assert.equal(volatileFallback.settings.wslBackendEnabled, false);
407 assert.equal(volatileFallback.settings.wslOnly, false);
408​
409 const current = yield* settings.get;
410 assert.equal(current.wslBackendEnabled, false);
411 assert.equal(current.wslOnly, false);
412​
413 const diskReload = yield* settings.load;
414 assert.equal(diskReload.wslBackendEnabled, true);
415 assert.equal(diskReload.wslOnly, true);
416 }),
417 ),
418 );
419​
420 it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () =>
421 withSettings(
422 Effect.gen(function* () {
423 const settings = yield* DesktopAppSettings.DesktopAppSettings;
424 yield* writeSettingsPatch({
425 wslMode: "wsl",
426 wslDistro: "Ubuntu-22.04",
427 });
428 const loaded = yield* settings.load;
429 assert.equal(loaded.wslBackendEnabled, true);
430 assert.equal(loaded.wslDistro, "Ubuntu-22.04");
431 }),
432 ),
433 );
434});