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