Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopClientSettings.test.ts

238 lines8.6 KB

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