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 threadDetailsUsageEnabled: false,
68 threadDetailsItems: {},
69 legacySidebarEnabled: false,
70 sidebarWorkingShelfEnabled: false,
71 loadBalancingEnabled: false,
72 loadBalancingWeights: { "environment-1": 75, "environment-2": 0 },
73 pullRequestMergeMethodOverrides: {},
74 timestampFormat: "24-hour",
75 wordWrap: true,
76};
77
78const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));
79function layer(baseDir: string) {
80 const layerEnvironment = DesktopEnvironment.layer({
81 dirname: "/repo/apps/desktop/src",
82 homeDirectory: baseDir,
83 platform: "darwin",
84 processArch: "x64",
85 appVersion: "1.2.3",
86 appPath: "/repo",
87 isPackaged: true,
88 resourcesPath: "/missing/resources",
89 runningUnderArm64Translation: false,
90 }).pipe(
91 Layer.provide(
92 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
93 ),
94 );
95
96 return DesktopClientSettings.layer.pipe(
97 Layer.provideMerge(layerEnvironment),
98 Layer.provideMerge(NodeServices.layer),
99 );
100}
101
102const withClientSettings = <A, E, R>(
103 effect: Effect.Effect<A, E, R | DesktopClientSettings.DesktopClientSettings>,
104) =>
105 Effect.gen(function* () {
106 const fileSystem = yield* FileSystem.FileSystem;
107 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
108 prefix: "t3-desktop-client-settings-test-",
109 });
110 return yield* effect.pipe(Effect.provide(layer(baseDir)));
111 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
112
113describe("DesktopClientSettings", () => {
114 it.effect("saves through a symlinked client settings file without replacing the link", () =>
115 withClientSettings(
116 Effect.gen(function* () {
117 const environment = yield* DesktopEnvironment.DesktopEnvironment;
118 const fileSystem = yield* FileSystem.FileSystem;
119 const settings = yield* DesktopClientSettings.DesktopClientSettings;
120 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
121 prefix: "t3-desktop-client-settings-dotfiles-",
122 });
123 const linkedSettingsPath = `${dotfiles}/client-settings.json`;
124 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
125 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
126 yield* fileSystem.symlink(linkedSettingsPath, environment.clientSettingsPath);
127
128 yield* settings.set(clientSettings);
129
130 assert.equal(
131 yield* fileSystem.readLink(environment.clientSettingsPath),
132 linkedSettingsPath,
133 );
134 assert.deepEqual(
135 yield* decodeClientSettingsJson(yield* fileSystem.readFileString(linkedSettingsPath)),
136 clientSettings,
137 );
138 }),
139 ),
140 );
141
142 it.effect.each([
143 { label: "permission", reason: "PermissionDenied" },
144 { label: "I/O", reason: "Unknown" },
145 ] as const)("preserves saved preferences across $label read failures and retries", (failure) =>
146 withClientSettings(
147 Effect.gen(function* () {
148 const environment = yield* DesktopEnvironment.DesktopEnvironment;
149 const fileSystem = yield* FileSystem.FileSystem;
150 const settings = yield* DesktopClientSettings.DesktopClientSettings;
151 const savedSettings = {
152 ...clientSettings,
153 onboardingCompletedAt: "2026-09-05T12:00:00.000Z",
154 };
155 yield* settings.set(savedSettings);
156 const savedContents = yield* fileSystem.readFileString(environment.clientSettingsPath);
157 const cause = PlatformError.systemError({
158 _tag: failure.reason,
159 module: "FileSystem",
160 method: "readFileString",
161 pathOrDescriptor: environment.clientSettingsPath,
162 });
163 let failRead = true;
164 const retryableSettings = yield* DesktopClientSettings.make.pipe(
165 Effect.provideService(
166 FileSystem.FileSystem,
167 FileSystem.FileSystem.of({
168 ...fileSystem,
169 readFileString: (path) =>
170 Effect.suspend(() =>
171 failRead ? Effect.fail(cause) : fileSystem.readFileString(path),
172 ),
173 }),
174 ),
175 );
176
177 const error = yield* retryableSettings.get.pipe(Effect.flip);
178 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
179 assert.equal(error.operation, "read-file");
180 assert.equal(error.path, environment.clientSettingsPath);
181 assert.strictEqual(error.cause, cause);
182 assert.equal(
183 yield* fileSystem.readFileString(environment.clientSettingsPath),
184 savedContents,
185 );
186
187 failRead = false;
188 assert.deepEqual(yield* retryableSettings.get, Option.some(savedSettings));
189 }),
190 ),
191 );
192
193 it.effect("loads lenient direct client settings documents", () =>
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.stateDir, { recursive: true });
200 yield* fileSystem.writeFileString(
201 environment.clientSettingsPath,
202 `{
203 // Matches server settings parsing.
204 "timestampFormat": "24-hour",
205 }\n`,
206 );
207
208 const persisted = yield* settings.get;
209 assert.isTrue(Option.isSome(persisted));
210 if (Option.isSome(persisted)) {
211 assert.equal(persisted.value.timestampFormat, "24-hour");
212 }
213 }),
214 ),
215 );
216
217 it.effect("loads legacy wrapped client settings documents", () =>
218 withClientSettings(
219 Effect.gen(function* () {
220 const environment = yield* DesktopEnvironment.DesktopEnvironment;
221 const fileSystem = yield* FileSystem.FileSystem;
222 const settings = yield* DesktopClientSettings.DesktopClientSettings;
223 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
224 yield* fileSystem.writeFileString(
225 environment.clientSettingsPath,
226 `{
227 "settings": {
228 "timestampFormat": "12-hour"
229 }
230 }\n`,
231 );
232
233 const persisted = yield* settings.get;
234 assert.isTrue(Option.isSome(persisted));
235 if (Option.isSome(persisted)) {
236 assert.equal(persisted.value.timestampFormat, "12-hour");
237 }
238 }),
239 ),
240 );
241
242 it.effect.each([
243 { label: "malformed JSON", contents: "{not-json" },
244 { label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' },
245 ])("reports $label without treating the settings file as absent", (document) =>
246 withClientSettings(
247 Effect.gen(function* () {
248 const environment = yield* DesktopEnvironment.DesktopEnvironment;
249 const fileSystem = yield* FileSystem.FileSystem;
250 const settings = yield* DesktopClientSettings.DesktopClientSettings;
251 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
252 yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents);
253
254 const error = yield* settings.get.pipe(Effect.flip);
255 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
256 assert.equal(error.operation, "decode-document");
257 assert.equal(error.path, environment.clientSettingsPath);
258 assert.instanceOf(error.cause, Schema.SchemaError);
259 assert.equal(
260 yield* fileSystem.readFileString(environment.clientSettingsPath),
261 document.contents,
262 );
263 }),
264 ),
265 );
266});