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

DesktopAppSettings.test.ts

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