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