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

DesktopClientSettings.test.ts

306 lines11.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8import * as PlatformError from "effect/PlatformError";
9import * as Schema from "effect/Schema";
10​
11import * as DesktopConfig from "../app/DesktopConfig.ts";
12import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
13import * as DesktopClientSettings from "./DesktopClientSettings.ts";
14​
15const clientSettings: ClientSettings = {
16 appearanceContrast: 100,
17 browserDefaultViewport: { _tag: "preset", width: 1024, height: 600, presetId: "nest-hub" },
18 browserDefaultZoomFactor: 1.25,
19 browserDefaultAppearance: "dark",
20 browserRecordingFrameRate: 60,
21 browserLinkTarget: "app",
22 browserAutoShowFloatingPreview: false,
23 browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
24 browserDefaultProfileId: "work",
25 confirmQuit: "double-click",
26 confirmThreadArchive: true,
27 confirmThreadDelete: false,
28 confirmThreadUnpin: false,
29 contextWindowMeterEnabled: false,
30 composerCollapseOnBlur: false,
31 composerCollapseOnScroll: true,
32 dismissedProviderUpdateNotificationKeys: [],
33 diffIgnoreWhitespace: true,
34 diffLayout: "stacked",
35 environmentIdentificationMode: "artwork",
36 favorites: [],
37 fontFamilyCode: "",
38 fontFamilyComposer: "",
39 fontFamilySans: "",
40 fontFamilyTerminal: "",
41 fontSizeCode: 13,
42 fontSizeInterface: 16,
43 fontSizePrompt: 14,
44 fontSizeTerminal: 12,
45 fontSmoothing: true,
46 glassOpacity: 80,
47 onboardingCompletedAt: null,
48 panelAnimationDurationMs: 0,
49 planModeEnabled: false,
50 proactivePanelsEnabled: true,
51 showSkillsInSlashMenu: false,
52 providerModelPreferences: {},
53 sidebarProjectGroupingMode: "repository_path",
54 sidebarProjectGroupingOverrides: {
55 "environment-1:/tmp/project-a": "separate",
56 },
57 sidebarProjectSortOrder: "manual",
58 sidebarThreadSortOrder: "created_at",
59 sidebarThreadPreviewCount: 6,
60 legacySidebarEnabled: false,
61 timestampFormat: "24-hour",
62 wordWrap: true,
63};
64​
65const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));
66const decodeRecordJson = Schema.decodeEffect(
67 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
68);
69function makeLayer(baseDir: string) {
70 const environmentLayer = DesktopEnvironment.layer({
71 dirname: "/repo/apps/desktop/src",
72 homeDirectory: baseDir,
73 platform: "darwin",
74 processArch: "x64",
75 appVersion: "1.2.3",
76 appPath: "/repo",
77 isPackaged: true,
78 resourcesPath: "/missing/resources",
79 runningUnderArm64Translation: false,
80 }).pipe(
81 Layer.provide(
82 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
83 ),
84 );
85​
86 return DesktopClientSettings.layer.pipe(
87 Layer.provideMerge(environmentLayer),
88 Layer.provideMerge(NodeServices.layer),
89 );
91​
92const withClientSettings = <A, E, R>(
93 effect: Effect.Effect<A, E, R | DesktopClientSettings.DesktopClientSettings>,
94) =>
95 Effect.gen(function* () {
96 const fileSystem = yield* FileSystem.FileSystem;
97 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
98 prefix: "t3-desktop-client-settings-test-",
99 });
100 return yield* effect.pipe(Effect.provide(makeLayer(baseDir)));
101 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
102​
103describe("DesktopClientSettings", () => {
104 it.effect("returns none when no client settings file exists", () =>
105 withClientSettings(
106 Effect.gen(function* () {
107 const settings = yield* DesktopClientSettings.DesktopClientSettings;
108 assert.isTrue(Option.isNone(yield* settings.get));
109 }),
110 ),
111 );
112​
113 it.effect("persists and reloads client settings", () =>
114 withClientSettings(
115 Effect.gen(function* () {
116 const environment = yield* DesktopEnvironment.DesktopEnvironment;
117 const fileSystem = yield* FileSystem.FileSystem;
118 const settings = yield* DesktopClientSettings.DesktopClientSettings;
119 yield* settings.set(clientSettings);
120​
121 assert.deepEqual(yield* settings.get, Option.some(clientSettings));
122 assert.deepEqual(
123 yield* decodeClientSettingsJson(
124 yield* fileSystem.readFileString(environment.clientSettingsPath),
125 ),
126 clientSettings,
127 );
128 assert.isFalse(
129 Object.hasOwn(
130 yield* decodeRecordJson(
131 yield* fileSystem.readFileString(environment.clientSettingsPath),
132 ),
133 "settings",
134 ),
135 );
136 }),
137 ),
138 );
139​
140 for (const failure of [
141 { label: "permission", reason: "PermissionDenied" },
142 { label: "I/O", reason: "Unknown" },
143 ] as const) {
144 it.effect(`preserves saved preferences across ${failure.label} read failures and retries`, () =>
145 withClientSettings(
146 Effect.gen(function* () {
147 const environment = yield* DesktopEnvironment.DesktopEnvironment;
148 const fileSystem = yield* FileSystem.FileSystem;
149 const settings = yield* DesktopClientSettings.DesktopClientSettings;
150 const savedSettings = {
151 ...clientSettings,
152 onboardingCompletedAt: "2026-09-05T12:00:00.000Z",
153 };
154 yield* settings.set(savedSettings);
155 const savedContents = yield* fileSystem.readFileString(environment.clientSettingsPath);
156 const cause = PlatformError.systemError({
157 _tag: failure.reason,
158 module: "FileSystem",
159 method: "readFileString",
160 pathOrDescriptor: environment.clientSettingsPath,
161 });
162 let failRead = true;
163 const retryableSettings = yield* DesktopClientSettings.make.pipe(
164 Effect.provideService(
165 FileSystem.FileSystem,
166 FileSystem.FileSystem.of({
167 ...fileSystem,
168 readFileString: (path) =>
169 Effect.suspend(() =>
170 failRead ? Effect.fail(cause) : fileSystem.readFileString(path),
171 ),
172 }),
173 ),
174 );
175​
176 const error = yield* retryableSettings.get.pipe(Effect.flip);
177 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
178 assert.equal(error.operation, "read-file");
179 assert.equal(error.path, environment.clientSettingsPath);
180 assert.strictEqual(error.cause, cause);
181 assert.equal(
182 yield* fileSystem.readFileString(environment.clientSettingsPath),
183 savedContents,
184 );
185​
186 failRead = false;
187 assert.deepEqual(yield* retryableSettings.get, Option.some(savedSettings));
188 }),
189 ),
190 );
191 }
192​
193 it.effect("reports the failed client settings write operation and path", () =>
194 withClientSettings(
195 Effect.gen(function* () {
196 const environment = yield* DesktopEnvironment.DesktopEnvironment;
197 const fileSystem = yield* FileSystem.FileSystem;
198 const settings = yield* DesktopClientSettings.DesktopClientSettings;
199 yield* fileSystem.makeDirectory(environment.clientSettingsPath, { recursive: true });
200​
201 const error = yield* settings.set(clientSettings).pipe(Effect.flip);
202 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsWriteError);
203 assert.equal(error.operation, "replace-settings-file");
204 assert.equal(error.path, environment.clientSettingsPath);
205 assert.instanceOf(error.cause, PlatformError.PlatformError);
206 assert.isString(error.cause.stack);
207 assert.equal(
208 error.message,
209 `Desktop client settings write failed during replace-settings-file at ${environment.clientSettingsPath}.`,
210 );
211 assert.notInclude(error.message, error.cause.message);
212 }),
213 ),
214 );
215​
216 it.effect("loads lenient direct client settings documents", () =>
217 withClientSettings(
218 Effect.gen(function* () {
219 const environment = yield* DesktopEnvironment.DesktopEnvironment;
220 const fileSystem = yield* FileSystem.FileSystem;
221 const settings = yield* DesktopClientSettings.DesktopClientSettings;
222 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
223 yield* fileSystem.writeFileString(
224 environment.clientSettingsPath,
225 `{
226 // Matches server settings parsing.
227 "timestampFormat": "24-hour",
228 }\n`,
229 );
230​
231 const persisted = yield* settings.get;
232 assert.isTrue(Option.isSome(persisted));
233 if (Option.isSome(persisted)) {
234 assert.equal(persisted.value.timestampFormat, "24-hour");
235 }
236 }),
237 ),
238 );
239​
240 it.effect("loads legacy wrapped client settings documents", () =>
241 withClientSettings(
242 Effect.gen(function* () {
243 const environment = yield* DesktopEnvironment.DesktopEnvironment;
244 const fileSystem = yield* FileSystem.FileSystem;
245 const settings = yield* DesktopClientSettings.DesktopClientSettings;
246 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
247 yield* fileSystem.writeFileString(
248 environment.clientSettingsPath,
249 `{
250 "settings": {
251 "timestampFormat": "12-hour"
252 }
253 }\n`,
254 );
255​
256 const persisted = yield* settings.get;
257 assert.isTrue(Option.isSome(persisted));
258 if (Option.isSome(persisted)) {
259 assert.equal(persisted.value.timestampFormat, "12-hour");
260 }
261 }),
262 ),
263 );
264​
265 it.effect("loads defaults from empty client settings documents", () =>
266 withClientSettings(
267 Effect.gen(function* () {
268 const environment = yield* DesktopEnvironment.DesktopEnvironment;
269 const fileSystem = yield* FileSystem.FileSystem;
270 const settings = yield* DesktopClientSettings.DesktopClientSettings;
271 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
272 yield* fileSystem.writeFileString(environment.clientSettingsPath, "{}\n");
273​
274 assert.deepEqual(yield* settings.get, Option.some(yield* decodeClientSettingsJson("{}")));
275 }),
276 ),
277 );
278​
279 for (const document of [
280 { label: "malformed JSON", contents: "{not-json" },
281 { label: "invalid direct settings", contents: '{"fontSizeCode":"large"}' },
282 { label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' },
283 ]) {
284 it.effect(`reports ${document.label} without treating the settings file as absent`, () =>
285 withClientSettings(
286 Effect.gen(function* () {
287 const environment = yield* DesktopEnvironment.DesktopEnvironment;
288 const fileSystem = yield* FileSystem.FileSystem;
289 const settings = yield* DesktopClientSettings.DesktopClientSettings;
290 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
291 yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents);
292​
293 const error = yield* settings.get.pipe(Effect.flip);
294 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
295 assert.equal(error.operation, "decode-document");
296 assert.equal(error.path, environment.clientSettingsPath);
297 assert.instanceOf(error.cause, Schema.SchemaError);
298 assert.equal(
299 yield* fileSystem.readFileString(environment.clientSettingsPath),
300 document.contents,
301 );
302 }),
303 ),
304 );
305 }
306});