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

DesktopClientSettings.test.ts

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