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

DesktopClientSettings.test.ts

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