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

DesktopAppSettings.test.ts

393 lines14.8 KB

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 Schema from "effect/Schema";
7import * as Tracer from "effect/Tracer";
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 layerEnvironment(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 );
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(layerEnvironment(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 });
92​
93describe("DesktopSettings", () => {
94 it.effect("records one bounds operation span with its input attributes", () =>
95 Effect.gen(function* () {
96 const spans: Array<Tracer.Span> = [];
97 const tracer = Tracer.make({
98 span: (options) => {
99 const span = new Tracer.NativeSpan(options);
100 spans.push(span);
101 return span;
102 },
103 });
104 yield* withSettings(
105 Effect.gen(function* () {
106 const settings = yield* DesktopAppSettings.DesktopAppSettings;
107 yield* settings.setMainWindowBounds({ x: 10, y: 20, width: 900, height: 600 }, true);
108 }),
109 ).pipe(Effect.provideService(Tracer.Tracer, tracer));
110 const operationSpans = spans.filter(
111 (span) => span.name === "desktop.settings.setMainWindowBounds",
112 );
113 assert.lengthOf(operationSpans, 1);
114 assert.deepEqual(Object.fromEntries(operationSpans[0]!.attributes), {
115 x: 10,
116 y: 20,
117 width: 900,
118 height: 600,
119 isMaximized: true,
120 });
121 assert.equal(operationSpans[0]!.status._tag, "Ended");
122 }),
123 );
124​
125 it.effect(
126 "persists disabling and re-enabling local execution without clearing backend settings",
127 () =>
128 withSettings(
129 Effect.gen(function* () {
130 const settings = yield* DesktopAppSettings.DesktopAppSettings;
131 yield* settings.setWslBackendEnabled(true);
132 yield* settings.setWslDistro("Ubuntu");
133 yield* settings.setServerExposureMode("network-accessible");
134 const before = yield* settings.get;
135 assert.isTrue((yield* settings.setLocalEnvironmentEnabled(false)).changed);
136 assert.deepEqual(yield* settings.load, { ...before, localEnvironmentEnabled: false });
137 assert.isFalse((yield* settings.setLocalEnvironmentEnabled(false)).changed);
138 yield* settings.setLocalEnvironmentEnabled(true);
139 assert.deepEqual(yield* settings.load, before);
140 }),
141 ),
142 );
143​
144 it.effect("falls back to defaults when the settings file is malformed", () =>
145 withSettings(
146 Effect.gen(function* () {
147 const environment = yield* DesktopEnvironment.DesktopEnvironment;
148 const fileSystem = yield* FileSystem.FileSystem;
149 const settings = yield* DesktopAppSettings.DesktopAppSettings;
150 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
151 yield* fileSystem.writeFileString(environment.desktopSettingsPath, "{not-json");
152​
153 assert.deepEqual(yield* settings.load, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
154 }),
155 ),
156 );
157​
158 it.effect("loads lenient persisted desktop settings JSON", () =>
159 withSettings(
160 Effect.gen(function* () {
161 const environment = yield* DesktopEnvironment.DesktopEnvironment;
162 const fileSystem = yield* FileSystem.FileSystem;
163 const settings = yield* DesktopAppSettings.DesktopAppSettings;
164 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
165 yield* fileSystem.writeFileString(
166 environment.desktopSettingsPath,
167 `{
168 // JSONC-style comments and trailing commas match server settings parsing.
169 "serverExposureMode": "network-accessible",
170 "tailscaleServeEnabled": true,
171 "tailscaleServePort": 8443,
172 "mainWindowBounds": { "x": 120, "y": 80, "width": 1280, "height": 900 },
173 }\n`,
174 );
175​
176 assert.deepEqual(yield* settings.load, {
177 linuxPasswordStore: "auto",
178 localEnvironmentEnabled: true,
179 mainWindowBounds: { x: 120, y: 80, width: 1280, height: 900 },
180 mainWindowMaximized: false,
181 serverExposureMode: "network-accessible",
182 tailscaleServeEnabled: true,
183 tailscaleServePort: 8443,
184 updateChannel: "latest",
185 updateChannelConfiguredByUser: false,
186 wslBackendEnabled: false,
187 wslOnly: false,
188 wslDistro: null,
189 } satisfies DesktopAppSettings.DesktopSettings);
190 }),
191 ),
192 );
193​
194 it.effect("rejects window bounds that do not satisfy the domain schema", () =>
195 withSettings(
196 Effect.gen(function* () {
197 const settings = yield* DesktopAppSettings.DesktopAppSettings;
198 yield* writeSettingsPatch({
199 mainWindowBounds: { x: 10.5, y: 20, width: 839, height: 620 },
200 mainWindowMaximized: true,
201 serverExposureMode: "network-accessible",
202 });
203​
204 const loaded = yield* settings.load;
205 assert.isNull(loaded.mainWindowBounds);
206 assert.isFalse(loaded.mainWindowMaximized);
207 assert.equal(loaded.serverExposureMode, "network-accessible");
208 }),
209 ),
210 );
211​
212 it.effect("persists sparse desktop settings documents", () =>
213 withSettings(
214 Effect.gen(function* () {
215 const environment = yield* DesktopEnvironment.DesktopEnvironment;
216 const fileSystem = yield* FileSystem.FileSystem;
217 const settings = yield* DesktopAppSettings.DesktopAppSettings;
218​
219 yield* settings.setMainWindowBounds({ x: -1200, y: 40, width: 1440, height: 960 }, true);
220 yield* settings.setServerExposureMode("network-accessible");
221​
222 const persisted = yield* decodeDesktopSettingsPatch(
223 yield* fileSystem.readFileString(environment.desktopSettingsPath),
224 );
225 assert.deepEqual(persisted, {
226 mainWindowBounds: { x: -1200, y: 40, width: 1440, height: 960 },
227 mainWindowMaximized: true,
228 serverExposureMode: "network-accessible",
229 } satisfies typeof DesktopSettingsPatch.Type);
230 }),
231 ),
232 );
233​
234 it.effect("saves through a symlinked settings file without replacing the link", () =>
235 withSettings(
236 Effect.gen(function* () {
237 const environment = yield* DesktopEnvironment.DesktopEnvironment;
238 const fileSystem = yield* FileSystem.FileSystem;
239 const settings = yield* DesktopAppSettings.DesktopAppSettings;
240 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
241 prefix: "t3-desktop-settings-dotfiles-",
242 });
243 const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
244 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
245 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
246 yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);
247​
248 yield* settings.setServerExposureMode("network-accessible");
249​
250 assert.equal(
251 yield* fileSystem.readLink(environment.desktopSettingsPath),
252 linkedSettingsPath,
253 );
254 const persisted = yield* decodeDesktopSettingsPatch(
255 yield* fileSystem.readFileString(linkedSettingsPath),
256 );
257 assert.equal(persisted.serverExposureMode, "network-accessible");
258 }),
259 ),
260 );
261​
262 it.effect("migrates legacy implicit update channels to the runtime default", () =>
263 withSettings(
264 Effect.gen(function* () {
265 const settings = yield* DesktopAppSettings.DesktopAppSettings;
266 yield* writeSettingsPatch({
267 serverExposureMode: "local-only",
268 updateChannel: "latest",
269 });
270​
271 assert.deepEqual(yield* settings.load, {
272 linuxPasswordStore: "auto",
273 localEnvironmentEnabled: true,
274 mainWindowBounds: null,
275 mainWindowMaximized: false,
276 serverExposureMode: "local-only",
277 tailscaleServeEnabled: false,
278 tailscaleServePort: 443,
279 updateChannel: "nightly",
280 updateChannelConfiguredByUser: false,
281 wslBackendEnabled: false,
282 wslOnly: false,
283 wslDistro: null,
284 } satisfies DesktopAppSettings.DesktopSettings);
285 }),
286 { appVersion: "0.0.17-nightly.20260415.1" },
287 ),
288 );
289​
290 it.effect("preserves explicit stable update channel on nightly builds", () =>
291 withSettings(
292 Effect.gen(function* () {
293 const settings = yield* DesktopAppSettings.DesktopAppSettings;
294 yield* writeSettingsPatch({
295 serverExposureMode: "local-only",
296 updateChannel: "latest",
297 updateChannelConfiguredByUser: true,
298 });
299​
300 assert.deepEqual(yield* settings.load, {
301 linuxPasswordStore: "auto",
302 localEnvironmentEnabled: true,
303 mainWindowBounds: null,
304 mainWindowMaximized: false,
305 serverExposureMode: "local-only",
306 tailscaleServeEnabled: false,
307 tailscaleServePort: 443,
308 updateChannel: "latest",
309 updateChannelConfiguredByUser: true,
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("persists wsl backend toggle and normalizes invalid distro names", () =>
320 withSettings(
321 Effect.gen(function* () {
322 const settings = yield* DesktopAppSettings.DesktopAppSettings;
323 const enable = yield* settings.setWslBackendEnabled(true);
324 assert.isTrue(enable.changed);
325 assert.equal(enable.settings.wslBackendEnabled, true);
326​
327 const distro = yield* settings.setWslDistro("Ubuntu-22.04");
328 assert.isTrue(distro.changed);
329 assert.equal(distro.settings.wslDistro, "Ubuntu-22.04");
330​
331 const reloaded = yield* settings.load;
332 assert.equal(reloaded.wslBackendEnabled, true);
333 assert.equal(reloaded.wslDistro, "Ubuntu-22.04");
334​
335 const reject = yield* settings.setWslDistro("bad name!");
336 assert.equal(reject.settings.wslDistro, null);
337​
338 const noop = yield* settings.setWslDistro(null);
339 assert.isFalse(noop.changed);
340 }),
341 ),
342 );
343​
344 it.effect("applies WSL Windows fallback with persisted and volatile updates", () =>
345 withSettings(
346 Effect.gen(function* () {
347 const settings = yield* DesktopAppSettings.DesktopAppSettings;
348 yield* settings.setWslBackendEnabled(true);
349 yield* settings.setWslOnly(true);
350​
351 const persistedFallback = yield* settings.applyWslWindowsFallback;
352 assert.isTrue(persistedFallback.changed);
353 assert.equal(persistedFallback.settings.wslBackendEnabled, false);
354 assert.equal(persistedFallback.settings.wslOnly, false);
355​
356 const persistedReload = yield* settings.load;
357 assert.equal(persistedReload.wslBackendEnabled, false);
358 assert.equal(persistedReload.wslOnly, false);
359​
360 yield* settings.setWslBackendEnabled(true);
361 yield* settings.setWslOnly(true);
362​
363 const volatileFallback = yield* settings.applyWslWindowsFallbackInMemory;
364 assert.isTrue(volatileFallback.changed);
365 assert.equal(volatileFallback.settings.wslBackendEnabled, false);
366 assert.equal(volatileFallback.settings.wslOnly, false);
367​
368 const current = yield* settings.get;
369 assert.equal(current.wslBackendEnabled, false);
370 assert.equal(current.wslOnly, false);
371​
372 const diskReload = yield* settings.load;
373 assert.equal(diskReload.wslBackendEnabled, true);
374 assert.equal(diskReload.wslOnly, true);
375 }),
376 ),
377 );
378​
379 it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () =>
380 withSettings(
381 Effect.gen(function* () {
382 const settings = yield* DesktopAppSettings.DesktopAppSettings;
383 yield* writeSettingsPatch({
384 wslMode: "wsl",
385 wslDistro: "Ubuntu-22.04",
386 });
387 const loaded = yield* settings.load;
388 assert.equal(loaded.wslBackendEnabled, true);
389 assert.equal(loaded.wslDistro, "Ubuntu-22.04");
390 }),
391 ),
392 );
393});