-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathsettingsStore.test.ts
More file actions
481 lines (404 loc) · 14.1 KB
/
Copy pathsettingsStore.test.ts
File metadata and controls
481 lines (404 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import {
flushRendererStateWrites,
registerRendererStateStorage,
} from "@posthog/ui/shell/rendererStorage";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
type CompletionSound,
DEFAULT_WORKSPACE_MODE,
getEffectiveCustomInstructions,
useSettingsStore,
} from "./settingsStore";
const getItem = vi.fn();
const setItem = vi.fn();
const removeItem = vi.fn();
registerRendererStateStorage({ getItem, setItem, removeItem });
// Lands any coalesced write from the previous test on the old mocks (so a
// pending value cannot leak into this test's reads or assertions), then
// resets them.
async function resetPersistenceMocks() {
await flushRendererStateWrites();
getItem.mockReset();
setItem.mockReset();
removeItem.mockReset();
getItem.mockResolvedValue(null);
setItem.mockResolvedValue(undefined);
removeItem.mockResolvedValue(undefined);
}
// Persisted writes are debounced; flush while polling so the assertion sees
// the coalesced write as soon as the store has queued it.
async function waitForPersistedWrite() {
await vi.waitFor(async () => {
await flushRendererStateWrites();
expect(setItem).toHaveBeenCalled();
});
}
// Runs before any test mutates the store singleton, so getState() still
// reflects the initial values.
describe("feature settingsStore defaults", () => {
it("defaults the workspace mode to cloud with a local fallback", () => {
expect(DEFAULT_WORKSPACE_MODE).toBe("cloud");
expect(useSettingsStore.getState().lastUsedWorkspaceMode).toBe("cloud");
expect(useSettingsStore.getState().lastUsedLocalWorkspaceMode).toBe(
"local",
);
});
});
describe("feature settingsStore cloud selections", () => {
beforeEach(async () => {
await resetPersistenceMocks();
useSettingsStore.setState({
allowBypassPermissions: false,
lastUsedCloudRepository: null,
cachedCloudRepositoryMap: {},
cachedCloudDefaultBranchMap: {},
});
});
it("persists the last used cloud repository", async () => {
useSettingsStore.getState().setLastUsedCloudRepository("posthog/posthog");
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.lastUsedCloudRepository).toBe("posthog/posthog");
});
it("rehydrates the last used cloud repository", async () => {
getItem.mockResolvedValue(
JSON.stringify({
state: {
lastUsedCloudRepository: "posthog/posthog",
},
version: 0,
}),
);
useSettingsStore.setState({
lastUsedCloudRepository: null,
});
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().lastUsedCloudRepository).toBe(
"posthog/posthog",
);
});
it("persists the cached cloud repository map", async () => {
useSettingsStore.getState().setCachedCloudRepositoryMap({
"posthog/posthog": {
userIntegrationId: "user-1",
installationId: "install-1",
},
});
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.cachedCloudRepositoryMap).toEqual({
"posthog/posthog": {
userIntegrationId: "user-1",
installationId: "install-1",
},
});
});
it("rehydrates the cached cloud repository map", async () => {
getItem.mockResolvedValue(
JSON.stringify({
state: {
cachedCloudRepositoryMap: {
"posthog/code": {
userIntegrationId: "user-2",
installationId: "install-2",
},
},
},
version: 0,
}),
);
useSettingsStore.setState({ cachedCloudRepositoryMap: {} });
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().cachedCloudRepositoryMap).toEqual({
"posthog/code": {
userIntegrationId: "user-2",
installationId: "install-2",
},
});
});
it("caches and persists the cloud default branch per repo", async () => {
useSettingsStore
.getState()
.setCachedCloudDefaultBranch("posthog/posthog", "master");
useSettingsStore
.getState()
.setCachedCloudDefaultBranch("posthog/code", "main");
expect(useSettingsStore.getState().cachedCloudDefaultBranchMap).toEqual({
"posthog/posthog": "master",
"posthog/code": "main",
});
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.cachedCloudDefaultBranchMap).toEqual({
"posthog/posthog": "master",
"posthog/code": "main",
});
});
it("keeps the same map reference when the default branch is unchanged", () => {
useSettingsStore
.getState()
.setCachedCloudDefaultBranch("posthog/code", "main");
const first = useSettingsStore.getState().cachedCloudDefaultBranchMap;
useSettingsStore
.getState()
.setCachedCloudDefaultBranch("posthog/code", "main");
const second = useSettingsStore.getState().cachedCloudDefaultBranchMap;
expect(second).toBe(first);
});
it("rehydrates the cached cloud default branch map", async () => {
getItem.mockResolvedValue(
JSON.stringify({
state: {
cachedCloudDefaultBranchMap: { "posthog/code": "main" },
},
version: 0,
}),
);
useSettingsStore.setState({ cachedCloudDefaultBranchMap: {} });
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().cachedCloudDefaultBranchMap).toEqual({
"posthog/code": "main",
});
});
it("rehydrates the unsafe mode toggle", async () => {
getItem.mockResolvedValue(
JSON.stringify({
state: {
allowBypassPermissions: true,
},
version: 0,
}),
);
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().allowBypassPermissions).toBe(true);
});
it.each([
["lastUsedWorkspaceMode", "local", "cloud"],
["debugLogsCloudRuns", false, true],
["slotMachineMode", false, true],
["dismissibleUpdateBanners", false, true],
["showSidebarWorktrees", false, true],
] as const)("rehydrates %s", async (field, initial, persisted) => {
getItem.mockResolvedValue(
JSON.stringify({ state: { [field]: persisted }, version: 0 }),
);
useSettingsStore.setState({ [field]: initial } as Parameters<
typeof useSettingsStore.setState
>[0]);
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState()[field]).toBe(persisted);
});
it("flips _hasHydrated once the persisted snapshot lands", async () => {
getItem.mockResolvedValue(JSON.stringify({ state: {}, version: 0 }));
useSettingsStore.setState({ _hasHydrated: false });
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState()._hasHydrated).toBe(true);
});
});
describe("feature settingsStore custom sounds", () => {
beforeEach(async () => {
await resetPersistenceMocks();
useSettingsStore.setState({ customSounds: [], completionSound: "none" });
});
const sound = {
id: "abc",
name: "My ding",
dataUrl: "data:audio/webm;base64,AAAA",
durationMs: 1200,
};
it("adds a custom sound", () => {
useSettingsStore.getState().addCustomSound(sound);
expect(useSettingsStore.getState().customSounds).toEqual([sound]);
});
it("renames a custom sound without touching its clip", () => {
useSettingsStore.getState().addCustomSound(sound);
useSettingsStore.getState().renameCustomSound("abc", "Renamed");
const stored = useSettingsStore.getState().customSounds[0];
expect(stored.name).toBe("Renamed");
expect(stored.dataUrl).toBe(sound.dataUrl);
});
it.each([
{
label: "active sound",
activeSound: "custom:abc" as CompletionSound,
expectedSound: "none" as CompletionSound,
},
{
label: "non-active sound",
activeSound: "meep" as CompletionSound,
expectedSound: "meep" as CompletionSound,
},
{
label: "last sound feeding random-custom",
activeSound: "random-custom" as CompletionSound,
expectedSound: "none" as CompletionSound,
},
])(
"removing the $label leaves completionSound as $expectedSound",
({ activeSound, expectedSound }) => {
useSettingsStore.getState().addCustomSound(sound);
useSettingsStore.getState().setCompletionSound(activeSound);
useSettingsStore.getState().removeCustomSound("abc");
expect(useSettingsStore.getState().customSounds).toEqual([]);
expect(useSettingsStore.getState().completionSound).toBe(expectedSound);
},
);
it("keeps random-custom active while other custom sounds remain", () => {
useSettingsStore.getState().addCustomSound(sound);
useSettingsStore.getState().addCustomSound({ ...sound, id: "def" });
useSettingsStore.getState().setCompletionSound("random-custom");
useSettingsStore.getState().removeCustomSound("abc");
expect(useSettingsStore.getState().completionSound).toBe("random-custom");
});
it("persists custom sounds", async () => {
useSettingsStore.getState().addCustomSound(sound);
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.customSounds).toEqual([sound]);
});
it.each([
{
label: "random-custom with empty customSounds array",
persistedState: { completionSound: "random-custom", customSounds: [] },
expectedCompletionSound: "none",
},
{
label: "random-custom with absent customSounds key",
persistedState: { completionSound: "random-custom" },
expectedCompletionSound: "none",
},
{
label: "random-custom with non-empty library",
persistedState: {
completionSound: "random-custom",
customSounds: [sound],
},
expectedCompletionSound: "random-custom",
},
])(
"rehydrate merge normalizes $label",
async ({ persistedState, expectedCompletionSound }) => {
getItem.mockResolvedValue(
JSON.stringify({ state: persistedState, version: 0 }),
);
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().completionSound).toBe(
expectedCompletionSound,
);
},
);
});
describe("getEffectiveCustomInstructions", () => {
const synced = {
path: "/home/u/.claude/CLAUDE.md",
displayPath: "~/.claude/CLAUDE.md",
content: "from file",
truncated: false,
};
it.each([
{
label: "typed instructions when sync is off",
sync: false,
syncedValue: synced,
expected: "typed",
},
{
label: "file content when sync is on and a file was found",
sync: true,
syncedValue: synced,
expected: "from file",
},
{
label: "nothing when sync is on but no file was found",
sync: true,
syncedValue: null,
expected: "",
},
{
label: "nothing when the synced file is whitespace",
sync: true,
syncedValue: { ...synced, content: " \n" },
expected: "",
},
])("returns $label", ({ sync, syncedValue, expected }) => {
expect(
getEffectiveCustomInstructions({
customInstructions: "typed",
syncCustomInstructionsFromFile: sync,
syncedCustomInstructions: syncedValue,
}),
).toBe(expected);
});
});
describe("feature settingsStore custom instructions sync persistence", () => {
beforeEach(async () => {
await resetPersistenceMocks();
useSettingsStore.setState({
syncCustomInstructionsFromFile: false,
syncedCustomInstructions: null,
});
});
it("persists the sync toggle but never the runtime snapshot", async () => {
// The toggle is durable preference; the snapshot is re-read on boot by the
// sync contribution. Persisting the snapshot would let a stale file rehydrate
// and reach a session created before the contribution's re-read finishes.
useSettingsStore.setState({
syncCustomInstructionsFromFile: true,
syncedCustomInstructions: {
path: "/home/u/.claude/CLAUDE.md",
displayPath: "~/.claude/CLAUDE.md",
content: "from file",
truncated: false,
},
});
// Nudge a persisted write via a partialized field.
useSettingsStore.getState().setCustomInstructions("touch");
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.syncCustomInstructionsFromFile).toBe(true);
expect(persisted.state).not.toHaveProperty("syncedCustomInstructions");
});
});
describe("feature settingsStore terminal font", () => {
beforeEach(async () => {
await resetPersistenceMocks();
useSettingsStore.setState({
terminalFont: "berkeley-mono",
terminalCustomFontFamily: "",
});
});
it("defaults to berkeley-mono with no custom override", () => {
expect(useSettingsStore.getState().terminalFont).toBe("berkeley-mono");
expect(useSettingsStore.getState().terminalCustomFontFamily).toBe("");
});
it("persists terminal font selection and custom family", async () => {
useSettingsStore.getState().setTerminalFont("custom");
useSettingsStore.getState().setTerminalCustomFontFamily("Fira Code");
await waitForPersistedWrite();
const lastCall = setItem.mock.calls[setItem.mock.calls.length - 1];
const persisted = JSON.parse(lastCall[1]);
expect(persisted.state.terminalFont).toBe("custom");
expect(persisted.state.terminalCustomFontFamily).toBe("Fira Code");
});
it("rehydrates terminal font selection and custom family", async () => {
getItem.mockResolvedValue(
JSON.stringify({
state: {
terminalFont: "jetbrains-mono",
terminalCustomFontFamily: "Cascadia Code",
},
version: 0,
}),
);
await useSettingsStore.persist.rehydrate();
expect(useSettingsStore.getState().terminalFont).toBe("jetbrains-mono");
expect(useSettingsStore.getState().terminalCustomFontFamily).toBe(
"Cascadia Code",
);
});
});