Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9import * as DesktopConfig from "../app/DesktopConfig.ts";
10import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
11import * as DesktopAppSettings from "./DesktopAppSettings.ts";
12
13const DesktopSettingsPatch = Schema.Struct({
14 linuxPasswordStore: Schema.optionalKey(
15 Schema.Literals(["auto", "gnome-libsecret", "kwallet", "kwallet5", "kwallet6"]),
16 ),
17 mainWindowBounds: Schema.optionalKey(
18 Schema.NullOr(
19 Schema.Struct({
20 x: Schema.Number,
21 y: Schema.Number,
22 width: Schema.Number,
23 height: Schema.Number,
24 }),
25 ),
26 ),
27 mainWindowMaximized: Schema.optionalKey(Schema.Boolean),
28 serverExposureMode: Schema.optionalKey(Schema.Literals(["local-only", "network-accessible"])),
29 tailscaleServeEnabled: Schema.optionalKey(Schema.Boolean),
30 tailscaleServePort: Schema.optionalKey(Schema.Number),
31 updateChannel: Schema.optionalKey(Schema.Literals(["latest", "nightly"])),
32 updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean),
33 wslBackendEnabled: Schema.optionalKey(Schema.Boolean),
34 wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])),
35 wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)),
36 wslOnly: Schema.optionalKey(Schema.Boolean),
37});
38
39const decodeDesktopSettingsPatch = Schema.decodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
40const encodeDesktopSettingsPatch = Schema.encodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
41
42function makeEnvironmentLayer(baseDir: string, appVersion = "0.0.17") {
43 return DesktopEnvironment.layer({
44 dirname: "/repo/apps/desktop/src",
45 homeDirectory: baseDir,
46 platform: "darwin",
47 processArch: "x64",
48 appVersion,
49 appPath: "/repo",
50 isPackaged: true,
51 resourcesPath: "/missing/resources",
52 runningUnderArm64Translation: false,
53 }).pipe(
54 Layer.provide(
55 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
56 ),
57 );
58}
59
60const withSettings = <A, E, R>(
61 effect: Effect.Effect<
62 A,
63 E,
64 R | DesktopAppSettings.DesktopAppSettings | DesktopEnvironment.DesktopEnvironment
65 >,
66 options?: { readonly appVersion?: string },
67) =>
68 Effect.gen(function* () {
69 const fileSystem = yield* FileSystem.FileSystem;
70 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
71 prefix: "t3-desktop-settings-test-",
72 });
73 return yield* effect.pipe(
74 Effect.provide(
75 DesktopAppSettings.layer.pipe(
76 Layer.provideMerge(makeEnvironmentLayer(baseDir, options?.appVersion)),
77 Layer.provideMerge(NodeServices.layer),
78 ),
79 ),
80 );
81 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
82
83function writeSettingsPatch(patch: typeof DesktopSettingsPatch.Type) {
84 return Effect.gen(function* () {
85 const environment = yield* DesktopEnvironment.DesktopEnvironment;
86 const fileSystem = yield* FileSystem.FileSystem;
87 const encoded = yield* encodeDesktopSettingsPatch(patch);
88 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
89 yield* fileSystem.writeFileString(environment.desktopSettingsPath, `${encoded}\n`);
90 });
91}
92
93describe("DesktopSettings", () => {
94 it.effect("loads defaults when no settings file exists", () =>
95 withSettings(
96 Effect.gen(function* () {
97 const settings = yield* DesktopAppSettings.DesktopAppSettings;
98 assert.deepEqual(yield* settings.load, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
99 assert.deepEqual(yield* settings.get, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
100 }),
101 ),
102 );
103
104 it("defaults packaged nightly builds to the nightly update channel", () => {
105 assert.deepEqual(
106 DesktopAppSettings.resolveDefaultDesktopSettings("0.0.17-nightly.20260415.1"),
107 {
108 linuxPasswordStore: "auto",
109 mainWindowBounds: null,
110 mainWindowMaximized: false,
111 serverExposureMode: "local-only",
112 tailscaleServeEnabled: false,
113 tailscaleServePort: 443,
114 updateChannel: "nightly",
115 updateChannelConfiguredByUser: false,
116 wslBackendEnabled: false,
117 wslOnly: false,
118 wslDistro: null,
119 } satisfies DesktopAppSettings.DesktopSettings,
120 );
121 });
122
123 it.effect("loads persisted settings and applies semantic updates", () =>
124 withSettings(
125 Effect.gen(function* () {
126 const settings = yield* DesktopAppSettings.DesktopAppSettings;
127 yield* writeSettingsPatch({
128 linuxPasswordStore: "gnome-libsecret",
129 serverExposureMode: "network-accessible",
130 tailscaleServeEnabled: true,
131 tailscaleServePort: 8443,
132 updateChannel: "latest",
133 updateChannelConfiguredByUser: true,
134 });
135
136 assert.deepEqual(yield* settings.load, {
137 linuxPasswordStore: "gnome-libsecret",
138 mainWindowBounds: null,
139 mainWindowMaximized: false,
140 serverExposureMode: "network-accessible",
141 tailscaleServeEnabled: true,
142 tailscaleServePort: 8443,
143 updateChannel: "latest",
144 updateChannelConfiguredByUser: true,
145 wslBackendEnabled: false,
146 wslOnly: false,
147 wslDistro: null,
148 } satisfies DesktopAppSettings.DesktopSettings);
149
150 const exposure = yield* settings.setServerExposureMode("local-only");
151 assert.isTrue(exposure.changed);
152 assert.equal(exposure.settings.serverExposureMode, "local-only");
153
154 const tailscale = yield* settings.setTailscaleServe({
155 enabled: true,
156 port: Option.some(9443),
157 });
158 assert.isTrue(tailscale.changed);
159 assert.equal(tailscale.settings.tailscaleServePort, 9443);
160
161 const updateChannel = yield* settings.setUpdateChannel("nightly");
162 assert.isTrue(updateChannel.changed);
163 assert.equal(updateChannel.settings.updateChannel, "nightly");
164 assert.equal(updateChannel.settings.updateChannelConfiguredByUser, true);
165 }),
166 ),
167 );
168
169 it.effect("reports the failed desktop settings write operation and path", () =>
170 withSettings(
171 Effect.gen(function* () {
172 const environment = yield* DesktopEnvironment.DesktopEnvironment;
173 const fileSystem = yield* FileSystem.FileSystem;
174 const settings = yield* DesktopAppSettings.DesktopAppSettings;
175 yield* fileSystem.makeDirectory(environment.desktopSettingsPath, { recursive: true });
176
177 const error = yield* settings.setServerExposureMode("network-accessible").pipe(Effect.flip);
178 assert.instanceOf(error, DesktopAppSettings.DesktopSettingsWriteError);
179 assert.equal(error.operation, "replace-settings-file");
180 assert.equal(error.path, environment.desktopSettingsPath);
181 assert.exists(error.cause);
182 assert.equal(
183 error.message,
184 `Desktop settings write failed during replace-settings-file at ${environment.desktopSettingsPath}.`,
185 );
186 }),
187 ),
188 );
189
190 it.effect("does not persist no-op semantic updates", () =>
191 withSettings(
192 Effect.gen(function* () {
193 const settings = yield* DesktopAppSettings.DesktopAppSettings;
194
195 const exposure = yield* settings.setServerExposureMode("local-only");
196 assert.isFalse(exposure.changed);
197
198 const tailscale = yield* settings.setTailscaleServe({
199 enabled: false,
200 port: Option.none(),
201 });
202 assert.isFalse(tailscale.changed);
203
204 const updateChannel = yield* settings.setUpdateChannel("latest");
205 assert.isFalse(updateChannel.changed);
206 assert.equal(updateChannel.settings.updateChannelConfiguredByUser, false);
207 }),
208 ),
209 );
210
211 it.effect("falls back to defaults when the settings file is malformed", () =>
212 withSettings(
213 Effect.gen(function* () {
214 const environment = yield* DesktopEnvironment.DesktopEnvironment;
215 const fileSystem = yield* FileSystem.FileSystem;
216 const settings = yield* DesktopAppSettings.DesktopAppSettings;
217 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
218 yield* fileSystem.writeFileString(environment.desktopSettingsPath, "{not-json");
219
220 assert.deepEqual(yield* settings.load, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
221 }),
222 ),
223 );
224
225 it.effect("loads lenient persisted desktop settings JSON", () =>
226 withSettings(
227 Effect.gen(function* () {
228 const environment = yield* DesktopEnvironment.DesktopEnvironment;
229 const fileSystem = yield* FileSystem.FileSystem;
230 const settings = yield* DesktopAppSettings.DesktopAppSettings;
231 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
232 yield* fileSystem.writeFileString(
233 environment.desktopSettingsPath,
234 `{
235 // JSONC-style comments and trailing commas match server settings parsing.
236 "serverExposureMode": "network-accessible",
237 "tailscaleServeEnabled": true,
238 "tailscaleServePort": 8443,
239 "mainWindowBounds": { "x": 120, "y": 80, "width": 1280, "height": 900 },
240 }\n`,
241 );
242
243 assert.deepEqual(yield* settings.load, {
244 linuxPasswordStore: "auto",
245 mainWindowBounds: { x: 120, y: 80, width: 1280, height: 900 },
246 mainWindowMaximized: false,
247 serverExposureMode: "network-accessible",
248 tailscaleServeEnabled: true,
249 tailscaleServePort: 8443,
250 updateChannel: "latest",
251 updateChannelConfiguredByUser: false,
252 wslBackendEnabled: false,
253 wslOnly: false,
254 wslDistro: null,
255 } satisfies DesktopAppSettings.DesktopSettings);
256 }),
257 ),
258 );
259
260 it.effect("rejects window bounds that do not satisfy the domain schema", () =>
261 withSettings(
262 Effect.gen(function* () {
263 const settings = yield* DesktopAppSettings.DesktopAppSettings;
264 yield* writeSettingsPatch({
265 mainWindowBounds: { x: 10.5, y: 20, width: 839, height: 620 },
266 mainWindowMaximized: true,
267 serverExposureMode: "network-accessible",
268 });
269
270 const loaded = yield* settings.load;
271 assert.isNull(loaded.mainWindowBounds);
272 assert.isFalse(loaded.mainWindowMaximized);
273 assert.equal(loaded.serverExposureMode, "network-accessible");
274 }),
275 ),
276 );
277
278 it.effect(
279 "normalizes unsupported linux password-store values without dropping other settings",
280 () =>
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.stateDir, { recursive: true });
287 yield* fileSystem.writeFileString(
288 environment.desktopSettingsPath,
289 `{
290 "linuxPasswordStore": "unsupported-store",
291 "serverExposureMode": "network-accessible",
292 "tailscaleServeEnabled": true,
293 "tailscaleServePort": 8443,
294 "updateChannel": "nightly",
295 "updateChannelConfiguredByUser": true
296 }\n`,
297 );
298
299 assert.deepEqual(yield* settings.load, {
300 linuxPasswordStore: "auto",
301 mainWindowBounds: null,
302 mainWindowMaximized: false,
303 serverExposureMode: "network-accessible",
304 tailscaleServeEnabled: true,
305 tailscaleServePort: 8443,
306 updateChannel: "nightly",
307 updateChannelConfiguredByUser: true,
308 wslBackendEnabled: false,
309 wslOnly: false,
310 wslDistro: null,
311 } satisfies DesktopAppSettings.DesktopSettings);
312 }),
313 ),
314 );
315
316 it.effect("persists sparse desktop settings documents", () =>
317 withSettings(
318 Effect.gen(function* () {
319 const environment = yield* DesktopEnvironment.DesktopEnvironment;
320 const fileSystem = yield* FileSystem.FileSystem;
321 const settings = yield* DesktopAppSettings.DesktopAppSettings;
322
323 yield* settings.setMainWindowBounds({ x: -1200, y: 40, width: 1440, height: 960 }, true);
324 yield* settings.setServerExposureMode("network-accessible");
325
326 const persisted = yield* decodeDesktopSettingsPatch(
327 yield* fileSystem.readFileString(environment.desktopSettingsPath),
328 );
329 assert.deepEqual(persisted, {
330 mainWindowBounds: { x: -1200, y: 40, width: 1440, height: 960 },
331 mainWindowMaximized: true,
332 serverExposureMode: "network-accessible",
333 } satisfies typeof DesktopSettingsPatch.Type);
334 }),
335 ),
336 );
337
338 it.effect("migrates legacy implicit update channels to the runtime default", () =>
339 withSettings(
340 Effect.gen(function* () {
341 const settings = yield* DesktopAppSettings.DesktopAppSettings;
342 yield* writeSettingsPatch({
343 serverExposureMode: "local-only",
344 updateChannel: "latest",
345 });
346
347 assert.deepEqual(yield* settings.load, {
348 linuxPasswordStore: "auto",
349 mainWindowBounds: null,
350 mainWindowMaximized: false,
351 serverExposureMode: "local-only",
352 tailscaleServeEnabled: false,
353 tailscaleServePort: 443,
354 updateChannel: "nightly",
355 updateChannelConfiguredByUser: false,
356 wslBackendEnabled: false,
357 wslOnly: false,
358 wslDistro: null,
359 } satisfies DesktopAppSettings.DesktopSettings);
360 }),
361 { appVersion: "0.0.17-nightly.20260415.1" },
362 ),
363 );
364
365 it.effect("preserves explicit stable update channel on nightly builds", () =>
366 withSettings(
367 Effect.gen(function* () {
368 const settings = yield* DesktopAppSettings.DesktopAppSettings;
369 yield* writeSettingsPatch({
370 serverExposureMode: "local-only",
371 updateChannel: "latest",
372 updateChannelConfiguredByUser: true,
373 });
374
375 assert.deepEqual(yield* settings.load, {
376 linuxPasswordStore: "auto",
377 mainWindowBounds: null,
378 mainWindowMaximized: false,
379 serverExposureMode: "local-only",
380 tailscaleServeEnabled: false,
381 tailscaleServePort: 443,
382 updateChannel: "latest",
383 updateChannelConfiguredByUser: true,
384 wslBackendEnabled: false,
385 wslOnly: false,
386 wslDistro: null,
387 } satisfies DesktopAppSettings.DesktopSettings);
388 }),
389 { appVersion: "0.0.17-nightly.20260415.1" },
390 ),
391 );
392
393 it.effect("normalizes invalid persisted Tailscale Serve ports", () =>
394 withSettings(
395 Effect.gen(function* () {
396 const settings = yield* DesktopAppSettings.DesktopAppSettings;
397 yield* writeSettingsPatch({
398 tailscaleServeEnabled: true,
399 tailscaleServePort: 0,
400 });
401
402 assert.deepEqual(yield* settings.load, {
403 linuxPasswordStore: "auto",
404 mainWindowBounds: null,
405 mainWindowMaximized: false,
406 serverExposureMode: "local-only",
407 tailscaleServeEnabled: true,
408 tailscaleServePort: 443,
409 updateChannel: "latest",
410 updateChannelConfiguredByUser: false,
411 wslBackendEnabled: false,
412 wslOnly: false,
413 wslDistro: null,
414 } satisfies DesktopAppSettings.DesktopSettings);
415 }),
416 ),
417 );
418
419 it.effect("persists wsl backend toggle and normalizes invalid distro names", () =>
420 withSettings(
421 Effect.gen(function* () {
422 const settings = yield* DesktopAppSettings.DesktopAppSettings;
423 const enable = yield* settings.setWslBackendEnabled(true);
424 assert.isTrue(enable.changed);
425 assert.equal(enable.settings.wslBackendEnabled, true);
426
427 const distro = yield* settings.setWslDistro("Ubuntu-22.04");
428 assert.isTrue(distro.changed);
429 assert.equal(distro.settings.wslDistro, "Ubuntu-22.04");
430
431 const reloaded = yield* settings.load;
432 assert.equal(reloaded.wslBackendEnabled, true);
433 assert.equal(reloaded.wslDistro, "Ubuntu-22.04");
434
435 const reject = yield* settings.setWslDistro("bad name!");
436 assert.equal(reject.settings.wslDistro, null);
437
438 const noop = yield* settings.setWslDistro(null);
439 assert.isFalse(noop.changed);
440 }),
441 ),
442 );
443
444 it.effect("applies WSL Windows fallback with persisted and volatile updates", () =>
445 withSettings(
446 Effect.gen(function* () {
447 const settings = yield* DesktopAppSettings.DesktopAppSettings;
448 yield* settings.setWslBackendEnabled(true);
449 yield* settings.setWslOnly(true);
450
451 const persistedFallback = yield* settings.applyWslWindowsFallback;
452 assert.isTrue(persistedFallback.changed);
453 assert.equal(persistedFallback.settings.wslBackendEnabled, false);
454 assert.equal(persistedFallback.settings.wslOnly, false);
455
456 const persistedReload = yield* settings.load;
457 assert.equal(persistedReload.wslBackendEnabled, false);
458 assert.equal(persistedReload.wslOnly, false);
459
460 yield* settings.setWslBackendEnabled(true);
461 yield* settings.setWslOnly(true);
462
463 const volatileFallback = yield* settings.applyWslWindowsFallbackInMemory;
464 assert.isTrue(volatileFallback.changed);
465 assert.equal(volatileFallback.settings.wslBackendEnabled, false);
466 assert.equal(volatileFallback.settings.wslOnly, false);
467
468 const current = yield* settings.get;
469 assert.equal(current.wslBackendEnabled, false);
470 assert.equal(current.wslOnly, false);
471
472 const diskReload = yield* settings.load;
473 assert.equal(diskReload.wslBackendEnabled, true);
474 assert.equal(diskReload.wslOnly, true);
475 }),
476 ),
477 );
478
479 it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () =>
480 withSettings(
481 Effect.gen(function* () {
482 const settings = yield* DesktopAppSettings.DesktopAppSettings;
483 yield* writeSettingsPatch({
484 wslMode: "wsl",
485 wslDistro: "Ubuntu-22.04",
486 });
487 const loaded = yield* settings.load;
488 assert.equal(loaded.wslBackendEnabled, true);
489 assert.equal(loaded.wslDistro, "Ubuntu-22.04");
490 }),
491 ),
492 );
493
494 it.effect("drops invalid persisted wsl distro values on load", () =>
495 withSettings(
496 Effect.gen(function* () {
497 const settings = yield* DesktopAppSettings.DesktopAppSettings;
498 yield* writeSettingsPatch({
499 wslBackendEnabled: true,
500 wslDistro: "bad/name",
501 });
502 const loaded = yield* settings.load;
503 assert.equal(loaded.wslBackendEnabled, true);
504 assert.equal(loaded.wslDistro, null);
505 }),
506 ),
507 );
508});