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 composerCollapseOnScroll: true,
38 dismissedProviderUpdateNotificationKeys: [],
39 diffFilesCollapsed: true,
40 diffIgnoreWhitespace: true,
41 diffLayout: "stacked",
42 environmentIdentificationMode: "artwork",
43 favorites: [],
44 fontFamilyCode: "",
45 fontFamilyComposer: "",
46 fontFamilySans: "",
47 fontFamilyTerminal: "",
48 fontSizeCode: 13,
49 fontSizeInterface: 16,
50 fontSizePrompt: 14,
51 fontSizeTerminal: 12,
52 fontSmoothing: true,
53 glassOpacity: 80,
54 onboardingCompletedAt: null,
55 panelAnimationDurationMs: 0,
56 planModeEnabled: false,
57 proactivePanelsEnabled: true,
58 showSkillsInSlashMenu: false,
59 persistComposerContextStrip: true,
60 providerModelPreferences: {},
61 sidebarProjectGroupingMode: "repository_path",
62 sidebarProjectGroupingOverrides: {
63 "environment-1:/tmp/project-a": "separate",
64 },
65 sidebarProjectSortOrder: "manual",
66 sidebarThreadSortOrder: "created_at",
67 sidebarThreadPreviewCount: 6,
68 legacySidebarEnabled: false,
69 sidebarWorkingShelfEnabled: false,
70 loadBalancingEnabled: false,
71 loadBalancingWeights: { "environment-1": 75, "environment-2": 0 },
72 pullRequestMergeMethodOverrides: {},
73 timestampFormat: "24-hour",
74 wordWrap: true,
75};
76
77const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));
78function layer(baseDir: string) {
79 const layerEnvironment = DesktopEnvironment.layer({
80 dirname: "/repo/apps/desktop/src",
81 homeDirectory: baseDir,
82 platform: "darwin",
83 processArch: "x64",
84 appVersion: "1.2.3",
85 appPath: "/repo",
86 isPackaged: true,
87 resourcesPath: "/missing/resources",
88 runningUnderArm64Translation: false,
89 }).pipe(
90 Layer.provide(
91 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
92 ),
93 );
94
95 return DesktopClientSettings.layer.pipe(
96 Layer.provideMerge(layerEnvironment),
97 Layer.provideMerge(NodeServices.layer),
98 );
99}
100
101const withClientSettings = <A, E, R>(
102 effect: Effect.Effect<A, E, R | DesktopClientSettings.DesktopClientSettings>,
103) =>
104 Effect.gen(function* () {
105 const fileSystem = yield* FileSystem.FileSystem;
106 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
107 prefix: "t3-desktop-client-settings-test-",
108 });
109 return yield* effect.pipe(Effect.provide(layer(baseDir)));
110 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
111
112describe("DesktopClientSettings", () => {
113 it.effect("saves through a symlinked client settings file without replacing the link", () =>
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 const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
120 prefix: "t3-desktop-client-settings-dotfiles-",
121 });
122 const linkedSettingsPath = `${dotfiles}/client-settings.json`;
123 yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
124 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
125 yield* fileSystem.symlink(linkedSettingsPath, environment.clientSettingsPath);
126
127 yield* settings.set(clientSettings);
128
129 assert.equal(
130 yield* fileSystem.readLink(environment.clientSettingsPath),
131 linkedSettingsPath,
132 );
133 assert.deepEqual(
134 yield* decodeClientSettingsJson(yield* fileSystem.readFileString(linkedSettingsPath)),
135 clientSettings,
136 );
137 }),
138 ),
139 );
140
141 it.effect.each([
142 { label: "permission", reason: "PermissionDenied" },
143 { label: "I/O", reason: "Unknown" },
144 ] as const)("preserves saved preferences across $label read failures and retries", (failure) =>
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 it.effect("loads lenient direct client settings documents", () =>
193 withClientSettings(
194 Effect.gen(function* () {
195 const environment = yield* DesktopEnvironment.DesktopEnvironment;
196 const fileSystem = yield* FileSystem.FileSystem;
197 const settings = yield* DesktopClientSettings.DesktopClientSettings;
198 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
199 yield* fileSystem.writeFileString(
200 environment.clientSettingsPath,
201 `{
202 // Matches server settings parsing.
203 "timestampFormat": "24-hour",
204 }\n`,
205 );
206
207 const persisted = yield* settings.get;
208 assert.isTrue(Option.isSome(persisted));
209 if (Option.isSome(persisted)) {
210 assert.equal(persisted.value.timestampFormat, "24-hour");
211 }
212 }),
213 ),
214 );
215
216 it.effect("loads legacy wrapped 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 "settings": {
227 "timestampFormat": "12-hour"
228 }
229 }\n`,
230 );
231
232 const persisted = yield* settings.get;
233 assert.isTrue(Option.isSome(persisted));
234 if (Option.isSome(persisted)) {
235 assert.equal(persisted.value.timestampFormat, "12-hour");
236 }
237 }),
238 ),
239 );
240
241 it.effect.each([
242 { label: "malformed JSON", contents: "{not-json" },
243 { label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' },
244 ])("reports $label without treating the settings file as absent", (document) =>
245 withClientSettings(
246 Effect.gen(function* () {
247 const environment = yield* DesktopEnvironment.DesktopEnvironment;
248 const fileSystem = yield* FileSystem.FileSystem;
249 const settings = yield* DesktopClientSettings.DesktopClientSettings;
250 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
251 yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents);
252
253 const error = yield* settings.get.pipe(Effect.flip);
254 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
255 assert.equal(error.operation, "decode-document");
256 assert.equal(error.path, environment.clientSettingsPath);
257 assert.instanceOf(error.cause, Schema.SchemaError);
258 assert.equal(
259 yield* fileSystem.readFileString(environment.clientSettingsPath),
260 document.contents,
261 );
262 }),
263 ),
264 );
265});