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