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

DesktopClientSettings.test.ts

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