Open History to see the last commit
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 browserAutoShowFloatingPreview: false,
22 browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
23 browserDefaultProfileId: "work",
24 confirmQuit: "double-click",
25 confirmThreadArchive: true,
26 confirmThreadDelete: false,
27 confirmThreadUnpin: false,
28 continueThreadsAfterServerUpdate: true,
29 contextWindowMeterEnabled: false,
30 dismissedProviderUpdateNotificationKeys: [],
31 diffIgnoreWhitespace: true,
32 diffLayout: "stacked",
33 environmentIdentificationMode: "artwork",
34 favorites: [],
35 fontFamilyCode: "",
36 fontFamilyComposer: "",
37 fontFamilySans: "",
38 fontFamilyTerminal: "",
39 fontSizeCode: 13,
40 fontSizeInterface: 16,
41 fontSizePrompt: 14,
42 fontSizeTerminal: 12,
43 fontSmoothing: true,
44 glassOpacity: 80,
45 panelAnimationDurationMs: 0,
46 planModeEnabled: false,
47 proactivePanelsEnabled: true,
48 showSkillsInSlashMenu: false,
49 providerModelPreferences: {},
50 sidebarProjectGroupingMode: "repository_path",
51 sidebarProjectGroupingOverrides: {
52 "environment-1:/tmp/project-a": "separate",
53 },
54 sidebarProjectSortOrder: "manual",
55 sidebarThreadSortOrder: "created_at",
56 sidebarThreadPreviewCount: 6,
57 legacySidebarEnabled: false,
58 timestampFormat: "24-hour",
59 wordWrap: true,
60};
61
62const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));
63const decodeRecordJson = Schema.decodeEffect(
64 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
65);
66function makeLayer(baseDir: string) {
67 const environmentLayer = DesktopEnvironment.layer({
68 dirname: "/repo/apps/desktop/src",
69 homeDirectory: baseDir,
70 platform: "darwin",
71 processArch: "x64",
72 appVersion: "1.2.3",
73 appPath: "/repo",
74 isPackaged: true,
75 resourcesPath: "/missing/resources",
76 runningUnderArm64Translation: false,
77 }).pipe(
78 Layer.provide(
79 Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: baseDir })),
80 ),
81 );
82
83 return DesktopClientSettings.layer.pipe(
84 Layer.provideMerge(environmentLayer),
85 Layer.provideMerge(NodeServices.layer),
86 );
87}
88
89const withClientSettings = <A, E, R>(
90 effect: Effect.Effect<A, E, R | DesktopClientSettings.DesktopClientSettings>,
91) =>
92 Effect.gen(function* () {
93 const fileSystem = yield* FileSystem.FileSystem;
94 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
95 prefix: "t3-desktop-client-settings-test-",
96 });
97 return yield* effect.pipe(Effect.provide(makeLayer(baseDir)));
98 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
99
100describe("DesktopClientSettings", () => {
101 it.effect("returns none when no client settings file exists", () =>
102 withClientSettings(
103 Effect.gen(function* () {
104 const settings = yield* DesktopClientSettings.DesktopClientSettings;
105 assert.isTrue(Option.isNone(yield* settings.get));
106 }),
107 ),
108 );
109
110 it.effect("persists and reloads client settings", () =>
111 withClientSettings(
112 Effect.gen(function* () {
113 const environment = yield* DesktopEnvironment.DesktopEnvironment;
114 const fileSystem = yield* FileSystem.FileSystem;
115 const settings = yield* DesktopClientSettings.DesktopClientSettings;
116 yield* settings.set(clientSettings);
117
118 assert.deepEqual(yield* settings.get, Option.some(clientSettings));
119 assert.deepEqual(
120 yield* decodeClientSettingsJson(
121 yield* fileSystem.readFileString(environment.clientSettingsPath),
122 ),
123 clientSettings,
124 );
125 assert.isFalse(
126 Object.hasOwn(
127 yield* decodeRecordJson(
128 yield* fileSystem.readFileString(environment.clientSettingsPath),
129 ),
130 "settings",
131 ),
132 );
133 }),
134 ),
135 );
136
137 it.effect("reports the failed client settings write operation and path", () =>
138 withClientSettings(
139 Effect.gen(function* () {
140 const environment = yield* DesktopEnvironment.DesktopEnvironment;
141 const fileSystem = yield* FileSystem.FileSystem;
142 const settings = yield* DesktopClientSettings.DesktopClientSettings;
143 yield* fileSystem.makeDirectory(environment.clientSettingsPath, { recursive: true });
144
145 const error = yield* settings.set(clientSettings).pipe(Effect.flip);
146 assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsWriteError);
147 assert.equal(error.operation, "replace-settings-file");
148 assert.equal(error.path, environment.clientSettingsPath);
149 assert.instanceOf(error.cause, PlatformError.PlatformError);
150 assert.isString(error.cause.stack);
151 assert.equal(
152 error.message,
153 `Desktop client settings write failed during replace-settings-file at ${environment.clientSettingsPath}.`,
154 );
155 assert.notInclude(error.message, error.cause.message);
156 }),
157 ),
158 );
159
160 it.effect("loads lenient direct client settings documents", () =>
161 withClientSettings(
162 Effect.gen(function* () {
163 const environment = yield* DesktopEnvironment.DesktopEnvironment;
164 const fileSystem = yield* FileSystem.FileSystem;
165 const settings = yield* DesktopClientSettings.DesktopClientSettings;
166 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
167 yield* fileSystem.writeFileString(
168 environment.clientSettingsPath,
169 `{
170 // Matches server settings parsing.
171 "timestampFormat": "24-hour",
172 }\n`,
173 );
174
175 const persisted = yield* settings.get;
176 assert.isTrue(Option.isSome(persisted));
177 if (Option.isSome(persisted)) {
178 assert.equal(persisted.value.timestampFormat, "24-hour");
179 }
180 }),
181 ),
182 );
183
184 it.effect("loads legacy wrapped client settings documents", () =>
185 withClientSettings(
186 Effect.gen(function* () {
187 const environment = yield* DesktopEnvironment.DesktopEnvironment;
188 const fileSystem = yield* FileSystem.FileSystem;
189 const settings = yield* DesktopClientSettings.DesktopClientSettings;
190 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
191 yield* fileSystem.writeFileString(
192 environment.clientSettingsPath,
193 `{
194 "settings": {
195 "timestampFormat": "12-hour"
196 }
197 }\n`,
198 );
199
200 const persisted = yield* settings.get;
201 assert.isTrue(Option.isSome(persisted));
202 if (Option.isSome(persisted)) {
203 assert.equal(persisted.value.timestampFormat, "12-hour");
204 }
205 }),
206 ),
207 );
208
209 it.effect("loads defaults from empty client settings documents", () =>
210 withClientSettings(
211 Effect.gen(function* () {
212 const environment = yield* DesktopEnvironment.DesktopEnvironment;
213 const fileSystem = yield* FileSystem.FileSystem;
214 const settings = yield* DesktopClientSettings.DesktopClientSettings;
215 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
216 yield* fileSystem.writeFileString(environment.clientSettingsPath, "{}\n");
217
218 assert.deepEqual(yield* settings.get, Option.some(yield* decodeClientSettingsJson("{}")));
219 }),
220 ),
221 );
222
223 it.effect("treats malformed client settings documents as absent", () =>
224 withClientSettings(
225 Effect.gen(function* () {
226 const environment = yield* DesktopEnvironment.DesktopEnvironment;
227 const fileSystem = yield* FileSystem.FileSystem;
228 const settings = yield* DesktopClientSettings.DesktopClientSettings;
229 yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
230 yield* fileSystem.writeFileString(environment.clientSettingsPath, "{not-json");
231
232 assert.isTrue(Option.isNone(yield* settings.get));
233 }),
234 ),
235 );
236});