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 Schema from "effect/Schema";
7
8import * as DesktopConfig from "../app/DesktopConfig.ts";
9import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
10import * as DesktopAppSettings from "./DesktopAppSettings.ts";
11
12const DesktopSettingsPatch = Schema.Struct({
13 linuxPasswordStore: Schema.optionalKey(
14 Schema.Literals(["auto", "gnome-libsecret", "kwallet", "kwallet5", "kwallet6"]),
15 ),
16 mainWindowBounds: Schema.optionalKey(
17 Schema.NullOr(
18 Schema.Struct({
19 x: Schema.Number,
20 y: Schema.Number,
21 width: Schema.Number,
22 height: Schema.Number,
23 }),
24 ),
25 ),
26 mainWindowMaximized: Schema.optionalKey(Schema.Boolean),
27 serverExposureMode: Schema.optionalKey(Schema.Literals(["local-only", "network-accessible"])),
28 tailscaleServeEnabled: Schema.optionalKey(Schema.Boolean),
29 tailscaleServePort: Schema.optionalKey(Schema.Number),
30 updateChannel: Schema.optionalKey(Schema.Literals(["latest", "nightly"])),
31 updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean),
32 wslBackendEnabled: Schema.optionalKey(Schema.Boolean),
33 wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])),
34 wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)),
35 wslOnly: Schema.optionalKey(Schema.Boolean),
36});
37
38const decodeDesktopSettingsPatch = Schema.decodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
39const encodeDesktopSettingsPatch = Schema.encodeEffect(Schema.fromJsonString(DesktopSettingsPatch));
40
41function layerEnvironment(baseDir: string, appVersion = "0.0.17") {
42 return DesktopEnvironment.layer({
43 dirname: "/repo/apps/desktop/src",
44 homeDirectory: baseDir,
45 platform: "darwin",
46 processArch: "x64",
47 appVersion,
48 appPath: "/repo",
49 isPackaged: true,
50 resourcesPath: "/missing/resources",
51 runningUnderArm64Translation: false,
52 }).pipe(
53 Layer.provide(
54 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
55 ),
56 );
57}
58
59const withSettings = <A, E, R>(
60 effect: Effect.Effect<
61 A,
62 E,
63 R | DesktopAppSettings.DesktopAppSettings | DesktopEnvironment.DesktopEnvironment
64 >,
65 options?: { readonly appVersion?: string },
66) =>
67 Effect.gen(function* () {
68 const fileSystem = yield* FileSystem.FileSystem;
69 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
70 prefix: "t3-desktop-settings-test-",
71 });
72 return yield* effect.pipe(
73 Effect.provide(
74 DesktopAppSettings.layer.pipe(
75 Layer.provideMerge(layerEnvironment(baseDir, options?.appVersion)),
76 Layer.provideMerge(NodeServices.layer),
77 ),
78 ),
79 );
80 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
81
82function writeSettingsPatch(patch: typeof DesktopSettingsPatch.Type) {
83 return Effect.gen(function* () {
84 const environment = yield* DesktopEnvironment.DesktopEnvironment;
85 const fileSystem = yield* FileSystem.FileSystem;
86 const encoded = yield* encodeDesktopSettingsPatch(patch);
87 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
88 yield* fileSystem.writeFileString(environment.desktopSettingsPath, `${encoded}\n`);
89 });
90}
91
92describe("DesktopSettings", () => {
93 it.effect(
94 "persists disabling and re-enabling local execution without clearing backend settings",
95 () =>
96 withSettings(
97 Effect.gen(function* () {
98 const settings = yield* DesktopAppSettings.DesktopAppSettings;
99 yield* settings.setWslBackendEnabled(true);
100 yield* settings.setWslDistro("Ubuntu");
101 yield* settings.setServerExposureMode("network-accessible");
102 const before = yield* settings.get;
103 assert.isTrue((yield* settings.setLocalEnvironmentEnabled(false)).changed);
104 assert.deepEqual(yield* settings.load, { ...before, localEnvironmentEnabled: false });
105 assert.isFalse((yield* settings.setLocalEnvironmentEnabled(false)).changed);
106 yield* settings.setLocalEnvironmentEnabled(true);
107 assert.deepEqual(yield* settings.load, before);
108 }),
109 ),
110 );
111
112 it.effect("falls back to defaults when the settings file is malformed", () =>
113 withSettings(
114 Effect.gen(function* () {
115 const environment = yield* DesktopEnvironment.DesktopEnvironment;
116 const fileSystem = yield* FileSystem.FileSystem;
117 const settings = yield* DesktopAppSettings.DesktopAppSettings;
118 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
119 yield* fileSystem.writeFileString(environment.desktopSettingsPath, "{not-json");
120
121 assert.deepEqual(yield* settings.load, DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS);
122 }),
123 ),
124 );
125
126 it.effect("loads lenient persisted desktop settings JSON", () =>
127 withSettings(
128 Effect.gen(function* () {
129 const environment = yield* DesktopEnvironment.DesktopEnvironment;
130 const fileSystem = yield* FileSystem.FileSystem;
131 const settings = yield* DesktopAppSettings.DesktopAppSettings;
132 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
133 yield* fileSystem.writeFileString(
134 environment.desktopSettingsPath,
135 `{
136 // JSONC-style comments and trailing commas match server settings parsing.
137 "serverExposureMode": "network-accessible",
138 "tailscaleServeEnabled": true,
139 "tailscaleServePort": 8443,
140 "mainWindowBounds": { "x": 120, "y": 80, "width": 1280, "height": 900 },
141 }\n`,
142 );
143
144 assert.deepEqual(yield* settings.load, {
145 linuxPasswordStore: "auto",
146 localEnvironmentEnabled: true,
147 mainWindowBounds: { x: 120, y: 80, width: 1280, height: 900 },
148 mainWindowMaximized: false,
149 serverExposureMode: "network-accessible",
150 tailscaleServeEnabled: true,
151 tailscaleServePort: 8443,
152 updateChannel: "latest",
153 updateChannelConfiguredByUser: false,
154 wslBackendEnabled: false,
155 wslOnly: false,
156 wslDistro: null,
157 } satisfies DesktopAppSettings.DesktopSettings);
158 }),
159 ),
160 );
161
162 it.effect("rejects window bounds that do not satisfy the domain schema", () =>
163 withSettings(
164 Effect.gen(function* () {
165 const settings = yield* DesktopAppSettings.DesktopAppSettings;
166 yield* writeSettingsPatch({
167 mainWindowBounds: { x: 10.5, y: 20, width: 839, height: 620 },
168 mainWindowMaximized: true,
169 serverExposureMode: "network-accessible",
170 });
171
172 const loaded = yield* settings.load;
173 assert.isNull(loaded.mainWindowBounds);
174 assert.isFalse(loaded.mainWindowMaximized);
175 assert.equal(loaded.serverExposureMode, "network-accessible");
176 }),
177 ),
178 );
179
180 it.effect("persists sparse desktop settings documents", () =>
181 withSettings(
182 Effect.gen(function* () {
183 const environment = yield* DesktopEnvironment.DesktopEnvironment;
184 const fileSystem = yield* FileSystem.FileSystem;
185 const settings = yield* DesktopAppSettings.DesktopAppSettings;
186
187 yield* settings.setMainWindowBounds({ x: -1200, y: 40, width: 1440, height: 960 }, true);
188 yield* settings.setServerExposureMode("network-accessible");
189
190 const persisted = yield* decodeDesktopSettingsPatch(
191 yield* fileSystem.readFileString(environment.desktopSettingsPath),
192 );
193 assert.deepEqual(persisted, {
194 mainWindowBounds: { x: -1200, y: 40, width: 1440, height: 960 },
195 mainWindowMaximized: true,
196 serverExposureMode: "network-accessible",
197 } satisfies typeof DesktopSettingsPatch.Type);
198 }),
199 ),
200 );
201
202 it.effect("saves through a symlinked settings file without replacing the link", () =>
203 withSettings(
204 Effect.gen(function* () {
205 const environment = yield* DesktopEnvironment.DesktopEnvironment;
206 const fileSystem = yield* FileSystem.FileSystem;
207 const settings = yield* DesktopAppSettings.DesktopAppSettings;
208 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
209 prefix: "t3-desktop-settings-dotfiles-",
210 });
211 const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
212 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
213 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
214 yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);
215
216 yield* settings.setServerExposureMode("network-accessible");
217
218 assert.equal(
219 yield* fileSystem.readLink(environment.desktopSettingsPath),
220 linkedSettingsPath,
221 );
222 const persisted = yield* decodeDesktopSettingsPatch(
223 yield* fileSystem.readFileString(linkedSettingsPath),
224 );
225 assert.equal(persisted.serverExposureMode, "network-accessible");
226 }),
227 ),
228 );
229
230 it.effect("migrates legacy implicit update channels to the runtime default", () =>
231 withSettings(
232 Effect.gen(function* () {
233 const settings = yield* DesktopAppSettings.DesktopAppSettings;
234 yield* writeSettingsPatch({
235 serverExposureMode: "local-only",
236 updateChannel: "latest",
237 });
238
239 assert.deepEqual(yield* settings.load, {
240 linuxPasswordStore: "auto",
241 localEnvironmentEnabled: true,
242 mainWindowBounds: null,
243 mainWindowMaximized: false,
244 serverExposureMode: "local-only",
245 tailscaleServeEnabled: false,
246 tailscaleServePort: 443,
247 updateChannel: "nightly",
248 updateChannelConfiguredByUser: false,
249 wslBackendEnabled: false,
250 wslOnly: false,
251 wslDistro: null,
252 } satisfies DesktopAppSettings.DesktopSettings);
253 }),
254 { appVersion: "0.0.17-nightly.20260415.1" },
255 ),
256 );
257
258 it.effect("preserves explicit stable update channel on nightly builds", () =>
259 withSettings(
260 Effect.gen(function* () {
261 const settings = yield* DesktopAppSettings.DesktopAppSettings;
262 yield* writeSettingsPatch({
263 serverExposureMode: "local-only",
264 updateChannel: "latest",
265 updateChannelConfiguredByUser: true,
266 });
267
268 assert.deepEqual(yield* settings.load, {
269 linuxPasswordStore: "auto",
270 localEnvironmentEnabled: true,
271 mainWindowBounds: null,
272 mainWindowMaximized: false,
273 serverExposureMode: "local-only",
274 tailscaleServeEnabled: false,
275 tailscaleServePort: 443,
276 updateChannel: "latest",
277 updateChannelConfiguredByUser: true,
278 wslBackendEnabled: false,
279 wslOnly: false,
280 wslDistro: null,
281 } satisfies DesktopAppSettings.DesktopSettings);
282 }),
283 { appVersion: "0.0.17-nightly.20260415.1" },
284 ),
285 );
286
287 it.effect("persists wsl backend toggle and normalizes invalid distro names", () =>
288 withSettings(
289 Effect.gen(function* () {
290 const settings = yield* DesktopAppSettings.DesktopAppSettings;
291 const enable = yield* settings.setWslBackendEnabled(true);
292 assert.isTrue(enable.changed);
293 assert.equal(enable.settings.wslBackendEnabled, true);
294
295 const distro = yield* settings.setWslDistro("Ubuntu-22.04");
296 assert.isTrue(distro.changed);
297 assert.equal(distro.settings.wslDistro, "Ubuntu-22.04");
298
299 const reloaded = yield* settings.load;
300 assert.equal(reloaded.wslBackendEnabled, true);
301 assert.equal(reloaded.wslDistro, "Ubuntu-22.04");
302
303 const reject = yield* settings.setWslDistro("bad name!");
304 assert.equal(reject.settings.wslDistro, null);
305
306 const noop = yield* settings.setWslDistro(null);
307 assert.isFalse(noop.changed);
308 }),
309 ),
310 );
311
312 it.effect("applies WSL Windows fallback with persisted and volatile updates", () =>
313 withSettings(
314 Effect.gen(function* () {
315 const settings = yield* DesktopAppSettings.DesktopAppSettings;
316 yield* settings.setWslBackendEnabled(true);
317 yield* settings.setWslOnly(true);
318
319 const persistedFallback = yield* settings.applyWslWindowsFallback;
320 assert.isTrue(persistedFallback.changed);
321 assert.equal(persistedFallback.settings.wslBackendEnabled, false);
322 assert.equal(persistedFallback.settings.wslOnly, false);
323
324 const persistedReload = yield* settings.load;
325 assert.equal(persistedReload.wslBackendEnabled, false);
326 assert.equal(persistedReload.wslOnly, false);
327
328 yield* settings.setWslBackendEnabled(true);
329 yield* settings.setWslOnly(true);
330
331 const volatileFallback = yield* settings.applyWslWindowsFallbackInMemory;
332 assert.isTrue(volatileFallback.changed);
333 assert.equal(volatileFallback.settings.wslBackendEnabled, false);
334 assert.equal(volatileFallback.settings.wslOnly, false);
335
336 const current = yield* settings.get;
337 assert.equal(current.wslBackendEnabled, false);
338 assert.equal(current.wslOnly, false);
339
340 const diskReload = yield* settings.load;
341 assert.equal(diskReload.wslBackendEnabled, true);
342 assert.equal(diskReload.wslOnly, true);
343 }),
344 ),
345 );
346
347 it.effect("migrates legacy wslMode=wsl to wslBackendEnabled on load", () =>
348 withSettings(
349 Effect.gen(function* () {
350 const settings = yield* DesktopAppSettings.DesktopAppSettings;
351 yield* writeSettingsPatch({
352 wslMode: "wsl",
353 wslDistro: "Ubuntu-22.04",
354 });
355 const loaded = yield* settings.load;
356 assert.equal(loaded.wslBackendEnabled, true);
357 assert.equal(loaded.wslDistro, "Ubuntu-22.04");
358 }),
359 ),
360 );
361});