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

DesktopClientSettings.test.ts

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