Open History to see the last commit
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 );
63}
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 });
96}
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 yield* writeSettingsPatch({
204 mainWindowBounds: { x: 10.5, y: 20, width: 839, height: 620 },
205 mainWindowMaximized: true,
206 serverExposureMode: "network-accessible",
207 });
208
209 const loaded = yield* settings.load;
210 assert.isNull(loaded.mainWindowBounds);
211 assert.isFalse(loaded.mainWindowMaximized);
212 assert.equal(loaded.serverExposureMode, "network-accessible");
213 }),
214 ),
215 );
216
217 it.effect("persists sparse desktop settings documents", () =>
218 withSettings(
219 Effect.gen(function* () {
220 const environment = yield* DesktopEnvironment.DesktopEnvironment;
221 const fileSystem = yield* FileSystem.FileSystem;
222 const settings = yield* DesktopAppSettings.DesktopAppSettings;
223
224 yield* settings.setMainWindowBounds({ x: -1200, y: 40, width: 1440, height: 960 }, true);
225 yield* settings.setServerExposureMode("network-accessible");
226
227 const persisted = yield* decodeDesktopSettingsPatch(
228 yield* fileSystem.readFileString(environment.desktopSettingsPath),
229 );
230 assert.deepEqual(persisted, {
231 mainWindowBounds: { x: -1200, y: 40, width: 1440, height: 960 },
232 mainWindowMaximized: true,
233 serverExposureMode: "network-accessible",
234 } satisfies typeof DesktopSettingsPatch.Type);
235 }),
236 ),
237 );
238
239 it.effect("saves through a symlinked settings file without replacing the link", () =>
240 withSettings(
241 Effect.gen(function* () {
242 const environment = yield* DesktopEnvironment.DesktopEnvironment;
243 const fileSystem = yield* FileSystem.FileSystem;
244 const settings = yield* DesktopAppSettings.DesktopAppSettings;
245 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
246 prefix: "t3-desktop-settings-dotfiles-",
247 });
248 const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
249 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
250 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
251 yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);
252
253 yield* settings.setServerExposureMode("network-accessible");
254
255 assert.equal(
256 yield* fileSystem.readLink(environment.desktopSettingsPath),
257 linkedSettingsPath,
258 );
259 const persisted = yield* decodeDesktopSettingsPatch(
260 yield* fileSystem.readFileString(linkedSettingsPath),
261 );
262 assert.equal(persisted.serverExposureMode, "network-accessible");
263 }),
264 ),
265 );
266
267 it.effect("migrates legacy implicit update channels to the runtime default", () =>
268 withSettings(
269 Effect.gen(function* () {
270 const settings = yield* DesktopAppSettings.DesktopAppSettings;
271 yield* writeSettingsPatch({
272 serverExposureMode: "local-only",
273 updateChannel: "latest",
274 });
275
276 assert.deepEqual(yield* settings.load, {
277 linuxPasswordStore: "auto",
278 localEnvironmentEnabled: true,
279 mainWindowBounds: null,
280 mainWindowMaximized: false,
281 serverExposureMode: "local-only",
282 tailscaleServeEnabled: false,
283 tailscaleServePort: 443,
284 updateChannel: "nightly",
285 updateChannelConfiguredByUser: false,
286 wslBackendEnabled: false,
287 wslOnly: false,
288 wslDistro: null,
289 } satisfies DesktopAppSettings.DesktopSettings);
290 }),
291 { appVersion: "0.0.17-nightly.20260415.1" },
292 ),
293 );
294
295 it.effect("preserves explicit stable update channel on nightly builds", () =>
296 withSettings(
297 Effect.gen(function* () {
298 const settings = yield* DesktopAppSettings.DesktopAppSettings;
299 yield* writeSettingsPatch({
300 serverExposureMode: "local-only",
301 updateChannel: "latest",
302 updateChannelConfiguredByUser: true,
303 });
304
305 assert.deepEqual(yield* settings.load, {
306 linuxPasswordStore: "auto",
307 localEnvironmentEnabled: true,
308 mainWindowBounds: null,
309 mainWindowMaximized: false,
310 serverExposureMode: "local-only",
311 tailscaleServeEnabled: false,
312 tailscaleServePort: 443,
313 updateChannel: "latest",
314 updateChannelConfiguredByUser: true,
315 wslBackendEnabled: false,
316 wslOnly: false,
317 wslDistro: null,
318 } satisfies DesktopAppSettings.DesktopSettings);
319 }),
320 { appVersion: "0.0.17-nightly.20260415.1" },
321 ),
322 );
323
324 it.effect("persists wsl backend toggle and normalizes invalid distro names", () =>
325 withSettings(
326 Effect.gen(function* () {
327 const settings = yield* DesktopAppSettings.DesktopAppSettings;
328 const enable = yield* settings.setWslBackendEnabled(true);
329 assert.isTrue(enable.changed);
330 assert.equal(enable.settings.wslBackendEnabled, true);
331
332 const distro = yield* settings.setWslDistro("Ubuntu-22.04");
333 assert.isTrue(distro.changed);
334 assert.equal(distro.settings.wslDistro, "Ubuntu-22.04");
335
336 const reloaded = yield* settings.load;
337 assert.equal(reloaded.wslBackendEnabled, true);
338 assert.equal(reloaded.wslDistro, "Ubuntu-22.04");
339
340 const reject = yield* settings.setWslDistro("bad name!");
341 assert.equal(reject.settings.wslDistro, null);
342
343 const noop = yield* settings.setWslDistro(null);
344 assert.isFalse(noop.changed);
345 }),
346 ),
347 );
348
349 it.effect("applies WSL Windows fallback with persisted and volatile updates", () =>
350 withSettings(
351 Effect.gen(function* () {
352 const settings = yield* DesktopAppSettings.DesktopAppSettings;
353 yield* settings.setWslBackendEnabled(true);
354 yield* settings.setWslOnly(true);
355
356 const persistedFallback = yield* settings.applyWslWindowsFallback;
357 assert.isTrue(persistedFallback.changed);
358 assert.equal(persistedFallback.settings.wslBackendEnabled, false);
359 assert.equal(persistedFallback.settings.wslOnly, false);
360
361 const persistedReload = yield* settings.load;
362 assert.equal(persistedReload.wslBackendEnabled, false);
363 assert.equal(persistedReload.wslOnly, false);
364
365 yield* settings.setWslBackendEnabled(true);
366 yield* settings.setWslOnly(true);
367
368 const volatileFallback = yield* settings.applyWslWindowsFallbackInMemory;
369 assert.isTrue(volatileFallback.changed);
370 assert.equal(volatileFallback.settings.wslBackendEnabled, false);
371 assert.equal(volatileFallback.settings.wslOnly, false);
372
373 const current = yield* settings.get;
374 assert.equal(current.wslBackendEnabled, false);
375 assert.equal(current.wslOnly, false);
376
377 const diskReload = yield* settings.load;
378 assert.equal(diskReload.wslBackendEnabled, true);
379 assert.equal(diskReload.wslOnly, true);
380 }),
381 ),
382 );
383
384 it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () =>
385 withSettings(
386 Effect.gen(function* () {
387 const settings = yield* DesktopAppSettings.DesktopAppSettings;
388 yield* writeSettingsPatch({
389 wslMode: "wsl",
390 wslDistro: "Ubuntu-22.04",
391 });
392 const loaded = yield* settings.load;
393 assert.equal(loaded.wslBackendEnabled, true);
394 assert.equal(loaded.wslDistro, "Ubuntu-22.04");
395 }),
396 ),
397 );
398});