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

uiStateStore.test.ts

585 lines20.2 KB

1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3​
4import {
5 clearThreadUi,
6 hydratePersistedProjectState,
7 markThreadVisited,
8 markThreadUnread,
9 PERSISTED_STATE_KEY,
10 type PersistedUiState,
11 persistState,
12 reorderProjects,
13 setProjectExpanded,
14 setThreadChangedFilesExpanded,
15 syncProjects,
16 syncThreads,
17 type UiState,
18} from "./uiStateStore";
19​
20function makeUiState(overrides: Partial<UiState> = {}): UiState {
21 return {
22 projectExpandedById: {},
23 projectOrder: [],
24 threadLastVisitedAtById: {},
25 threadChangedFilesExpandedById: {},
26 ...overrides,
27 };
29​
30describe("uiStateStore pure functions", () => {
31 it("markThreadVisited stores the provided server timestamp", () => {
32 const threadId = ThreadId.make("thread-1");
33 const initialState = makeUiState();
34​
35 const next = markThreadVisited(initialState, threadId, "2026-02-25T12:30:00.700Z");
36​
37 expect(next.threadLastVisitedAtById[threadId]).toBe("2026-02-25T12:30:00.700Z");
38 });
39​
40 it("markThreadVisited does not move visit state backwards under clock skew", () => {
41 const threadId = ThreadId.make("thread-1");
42 const initialState = makeUiState({
43 threadLastVisitedAtById: {
44 [threadId]: "2026-02-25T12:30:00.700Z",
45 },
46 });
47​
48 const next = markThreadVisited(initialState, threadId, "2026-02-25T12:30:00.000Z");
49​
50 expect(next).toBe(initialState);
51 });
52​
53 it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
54 const threadId = ThreadId.make("thread-1");
55 const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
56 const initialState = makeUiState({
57 threadLastVisitedAtById: {
58 [threadId]: "2026-02-25T12:35:00.000Z",
59 },
60 });
61​
62 const next = markThreadUnread(initialState, threadId, latestTurnCompletedAt);
63​
64 expect(next.threadLastVisitedAtById[threadId]).toBe("2026-02-25T12:29:59.999Z");
65 });
66​
67 it("markThreadUnread does not change a thread without a completed turn", () => {
68 const threadId = ThreadId.make("thread-1");
69 const initialState = makeUiState({
70 threadLastVisitedAtById: {
71 [threadId]: "2026-02-25T12:35:00.000Z",
72 },
73 });
74​
75 const next = markThreadUnread(initialState, threadId, null);
76​
77 expect(next).toBe(initialState);
78 });
79​
80 it("reorderProjects moves a project to a target index", () => {
81 const project1 = ProjectId.make("project-1");
82 const project2 = ProjectId.make("project-2");
83 const project3 = ProjectId.make("project-3");
84 const initialState = makeUiState({
85 projectOrder: [project1, project2, project3],
86 });
87​
88 const next = reorderProjects(initialState, [project1], [project3]);
89​
90 expect(next.projectOrder).toEqual([project2, project3, project1]);
91 });
92​
93 it("reorderProjects is a no-op when dragged key is not in projectOrder", () => {
94 const project1 = ProjectId.make("project-1");
95 const project2 = ProjectId.make("project-2");
96 const initialState = makeUiState({
97 projectOrder: [project1, project2],
98 });
99​
100 const next = reorderProjects(initialState, [ProjectId.make("missing")], [project2]);
101​
102 expect(next).toBe(initialState);
103 });
104​
105 it("reorderProjects moves all member keys of a multi-member group together", () => {
106 const keyALocal = "env-local:proj-a";
107 const keyARemote = "env-remote:proj-a";
108 const keyB = "env-local:proj-b";
109 const keyC = "env-local:proj-c";
110 const initialState = makeUiState({
111 projectOrder: [keyALocal, keyARemote, keyB, keyC],
112 });
113​
114 const next = reorderProjects(initialState, [keyALocal, keyARemote], [keyC]);
115​
116 expect(next.projectOrder).toEqual([keyB, keyC, keyALocal, keyARemote]);
117 });
118​
119 it("reorderProjects handles member keys scattered across projectOrder", () => {
120 const keyALocal = "env-local:proj-a";
121 const keyB = "env-local:proj-b";
122 const keyARemote = "env-remote:proj-a";
123 const keyC = "env-local:proj-c";
124 const initialState = makeUiState({
125 projectOrder: [keyALocal, keyB, keyARemote, keyC],
126 });
127​
128 const next = reorderProjects(initialState, [keyALocal, keyARemote], [keyC]);
129​
130 expect(next.projectOrder).toEqual([keyB, keyC, keyALocal, keyARemote]);
131 });
132​
133 it("reorderProjects places group after target when dragged from before a non-last target", () => {
134 const keyALocal = "env-local:proj-a";
135 const keyARemote = "env-remote:proj-a";
136 const keyB = "env-local:proj-b";
137 const keyC = "env-local:proj-c";
138 const keyD = "env-local:proj-d";
139 const initialState = makeUiState({
140 projectOrder: [keyALocal, keyARemote, keyB, keyC, keyD],
141 });
142​
143 const next = reorderProjects(initialState, [keyALocal, keyARemote], [keyC]);
144​
145 expect(next.projectOrder).toEqual([keyB, keyC, keyALocal, keyARemote, keyD]);
146 });
147​
148 it("reorderProjects places group before target when dragged from after", () => {
149 const keyB = "env-local:proj-b";
150 const keyC = "env-local:proj-c";
151 const keyALocal = "env-local:proj-a";
152 const keyARemote = "env-remote:proj-a";
153 const initialState = makeUiState({
154 projectOrder: [keyB, keyC, keyALocal, keyARemote],
155 });
156​
157 const next = reorderProjects(initialState, [keyALocal, keyARemote], [keyB]);
158​
159 expect(next.projectOrder).toEqual([keyALocal, keyARemote, keyB, keyC]);
160 });
161​
162 it("reorderProjects with multi-member target inserts after first target occurrence", () => {
163 const keyALocal = "env-local:proj-a";
164 const keyARemote = "env-remote:proj-a";
165 const keyBLocal = "env-local:proj-b";
166 const keyBRemote = "env-remote:proj-b";
167 const initialState = makeUiState({
168 projectOrder: [keyALocal, keyARemote, keyBLocal, keyBRemote],
169 });
170​
171 const next = reorderProjects(initialState, [keyALocal, keyARemote], [keyBLocal, keyBRemote]);
172​
173 // Target members may become non-contiguous; this is fine because the
174 // sidebar groups by logical key using first-occurrence positioning.
175 expect(next.projectOrder).toEqual([keyBLocal, keyALocal, keyARemote, keyBRemote]);
176 });
177​
178 it("reorderProjects is a no-op when dragged group equals target group", () => {
179 const key1 = "env-local:proj-a";
180 const key2 = "env-remote:proj-a";
181 const initialState = makeUiState({
182 projectOrder: [key1, key2, "env-local:proj-b"],
183 });
184​
185 const next = reorderProjects(initialState, [key1, key2], [key1, key2]);
186​
187 expect(next).toBe(initialState);
188 });
189​
190 it("reorderProjects is a no-op when dragged keys are not in projectOrder", () => {
191 const initialState = makeUiState({
192 projectOrder: ["env-local:proj-a", "env-local:proj-b"],
193 });
194​
195 const next = reorderProjects(initialState, ["env-local:missing"], ["env-local:proj-b"]);
196​
197 expect(next).toBe(initialState);
198 });
199​
200 it("syncProjects preserves current project order during snapshot recovery", () => {
201 const project1 = ProjectId.make("project-1");
202 const project2 = ProjectId.make("project-2");
203 const project3 = ProjectId.make("project-3");
204 const initialState = makeUiState({
205 projectExpandedById: {
206 [project1]: true,
207 [project2]: false,
208 },
209 projectOrder: [project2, project1],
210 });
211​
212 const next = syncProjects(initialState, [
213 { key: project1, logicalKey: project1, cwd: "/tmp/project-1" },
214 { key: project2, logicalKey: project2, cwd: "/tmp/project-2" },
215 { key: project3, logicalKey: project3, cwd: "/tmp/project-3" },
216 ]);
217​
218 expect(next.projectOrder).toEqual([project2, project1, project3]);
219 expect(next.projectExpandedById[project2]).toBe(false);
220 });
221​
222 it("syncProjects preserves manual order across project id churn at the same cwd", () => {
223 // Under the current design, physical key and logical key are both
224 // cwd-derived, so an internal project-id change doesn't alter the store
225 // keys. This test locks in that stability: re-syncing the same cwds keeps
226 // manual order and collapse state.
227 const keyProject1 = "env-local:/tmp/project-1";
228 const keyProject2 = "env-local:/tmp/project-2";
229 const initialState = syncProjects(
230 makeUiState({
231 projectExpandedById: {
232 [keyProject1]: true,
233 [keyProject2]: false,
234 },
235 projectOrder: [keyProject2, keyProject1],
236 }),
237 [
238 { key: keyProject1, logicalKey: keyProject1, cwd: "/tmp/project-1" },
239 { key: keyProject2, logicalKey: keyProject2, cwd: "/tmp/project-2" },
240 ],
241 );
242​
243 const next = syncProjects(initialState, [
244 { key: keyProject1, logicalKey: keyProject1, cwd: "/tmp/project-1" },
245 { key: keyProject2, logicalKey: keyProject2, cwd: "/tmp/project-2" },
246 ]);
247​
248 expect(next.projectOrder).toEqual([keyProject2, keyProject1]);
249 expect(next.projectExpandedById[keyProject2]).toBe(false);
250 });
251​
252 it("syncProjects returns a new state when only project cwd changes", () => {
253 const project1 = ProjectId.make("project-1");
254 const initialState = syncProjects(
255 makeUiState({
256 projectExpandedById: {
257 [project1]: false,
258 },
259 projectOrder: [project1],
260 }),
261 [{ key: project1, logicalKey: project1, cwd: "/tmp/project-1" }],
262 );
263​
264 const next = syncProjects(initialState, [
265 { key: project1, logicalKey: project1, cwd: "/tmp/project-1-renamed" },
266 ]);
267​
268 expect(next).not.toBe(initialState);
269 expect(next.projectOrder).toEqual([project1]);
270 expect(next.projectExpandedById[project1]).toBe(false);
271 });
272​
273 it("syncProjects keys projectExpandedById by the logical key, not the physical key", () => {
274 // In repository grouping mode, multiple physical projects (different
275 // environments or different repo-relative paths) collapse into one
276 // logical group. The group's expand state must be keyed by the logical
277 // key so clicks on the grouped row toggle the shared state, and so the
278 // state survives subsequent syncProjects calls (which rebuild the map
279 // from incoming inputs).
280 const physicalLocal = "env-local:/repo/project";
281 const physicalRemote = "env-remote:/repo/project";
282 const logicalKey = "repo-canonical-key";
283​
284 const initial = syncProjects(makeUiState(), [
285 { key: physicalLocal, logicalKey, cwd: "/repo/project" },
286 { key: physicalRemote, logicalKey, cwd: "/repo/project" },
287 ]);
288​
289 expect(initial.projectExpandedById).toEqual({ [logicalKey]: true });
290​
291 const afterCollapse = { ...initial, projectExpandedById: { [logicalKey]: false } };
292 const next = syncProjects(afterCollapse, [
293 { key: physicalLocal, logicalKey, cwd: "/repo/project" },
294 { key: physicalRemote, logicalKey, cwd: "/repo/project" },
295 ]);
296​
297 expect(next.projectExpandedById[logicalKey]).toBe(false);
298 });
299​
300 it("syncProjects preserves expand state when a project's logical key changes", () => {
301 // Example: late-arriving repo metadata flips grouping identity from the
302 // physical key to a canonical repository key. The row did not actually
303 // change, so the user's collapse choice must carry over.
304 const physicalKey = "env-local:/repo/project";
305 const previousLogicalKey = physicalKey;
306 const nextLogicalKey = "repo-canonical-key";
307​
308 const initial = syncProjects(makeUiState(), [
309 { key: physicalKey, logicalKey: previousLogicalKey, cwd: "/repo/project" },
310 ]);
311​
312 expect(initial.projectExpandedById[previousLogicalKey]).toBe(true);
313​
314 const afterCollapse = {
315 ...initial,
316 projectExpandedById: { [previousLogicalKey]: false },
317 };
318 const next = syncProjects(afterCollapse, [
319 { key: physicalKey, logicalKey: nextLogicalKey, cwd: "/repo/project" },
320 ]);
321​
322 expect(next.projectExpandedById[nextLogicalKey]).toBe(false);
323 });
324​
325 it("syncThreads prunes missing thread UI state", () => {
326 const thread1 = ThreadId.make("thread-1");
327 const thread2 = ThreadId.make("thread-2");
328 const initialState = makeUiState({
329 threadLastVisitedAtById: {
330 [thread1]: "2026-02-25T12:35:00.000Z",
331 [thread2]: "2026-02-25T12:36:00.000Z",
332 },
333 threadChangedFilesExpandedById: {
334 [thread1]: {
335 "turn-1": false,
336 },
337 [thread2]: {
338 "turn-2": false,
339 },
340 },
341 });
342​
343 const next = syncThreads(initialState, [{ key: thread1 }]);
344​
345 expect(next.threadLastVisitedAtById).toEqual({
346 [thread1]: "2026-02-25T12:35:00.000Z",
347 });
348 expect(next.threadChangedFilesExpandedById).toEqual({
349 [thread1]: {
350 "turn-1": false,
351 },
352 });
353 });
354​
355 it("syncThreads seeds visit state for unseen snapshot threads", () => {
356 const thread1 = ThreadId.make("thread-1");
357 const initialState = makeUiState();
358​
359 const next = syncThreads(initialState, [
360 {
361 key: thread1,
362 seedVisitedAt: "2026-02-25T12:35:00.000Z",
363 },
364 ]);
365​
366 expect(next.threadLastVisitedAtById).toEqual({
367 [thread1]: "2026-02-25T12:35:00.000Z",
368 });
369 });
370​
371 it("setProjectExpanded updates expansion without touching order", () => {
372 const project1 = ProjectId.make("project-1");
373 const initialState = makeUiState({
374 projectExpandedById: {
375 [project1]: true,
376 },
377 projectOrder: [project1],
378 });
379​
380 const next = setProjectExpanded(initialState, project1, false);
381​
382 expect(next.projectExpandedById[project1]).toBe(false);
383 expect(next.projectOrder).toEqual([project1]);
384 });
385​
386 it("clearThreadUi removes visit state for deleted threads", () => {
387 const thread1 = ThreadId.make("thread-1");
388 const initialState = makeUiState({
389 threadLastVisitedAtById: {
390 [thread1]: "2026-02-25T12:35:00.000Z",
391 },
392 threadChangedFilesExpandedById: {
393 [thread1]: {
394 "turn-1": false,
395 },
396 },
397 });
398​
399 const next = clearThreadUi(initialState, thread1);
400​
401 expect(next.threadLastVisitedAtById).toEqual({});
402 expect(next.threadChangedFilesExpandedById).toEqual({});
403 });
404​
405 it("setThreadChangedFilesExpanded stores collapsed turns per thread", () => {
406 const thread1 = ThreadId.make("thread-1");
407 const initialState = makeUiState();
408​
409 const next = setThreadChangedFilesExpanded(initialState, thread1, "turn-1", false);
410​
411 expect(next.threadChangedFilesExpandedById).toEqual({
412 [thread1]: {
413 "turn-1": false,
414 },
415 });
416 });
417​
418 it("setThreadChangedFilesExpanded removes thread overrides when expanded again", () => {
419 const thread1 = ThreadId.make("thread-1");
420 const initialState = makeUiState({
421 threadChangedFilesExpandedById: {
422 [thread1]: {
423 "turn-1": false,
424 },
425 },
426 });
427​
428 const next = setThreadChangedFilesExpanded(initialState, thread1, "turn-1", true);
429​
430 expect(next.threadChangedFilesExpandedById).toEqual({});
431 });
432});
433​
434describe("uiStateStore persistence round-trip", () => {
435 function createLocalStorageStub(): Storage {
436 const store = new Map<string, string>();
437 return {
438 clear: () => {
439 store.clear();
440 },
441 getItem: (key) => store.get(key) ?? null,
442 key: (index) => [...store.keys()][index] ?? null,
443 get length() {
444 return store.size;
445 },
446 removeItem: (key) => {
447 store.delete(key);
448 },
449 setItem: (key, value) => {
450 store.set(key, value);
451 },
452 };
453 }
454​
455 let localStorageStub: Storage;
456​
457 beforeEach(() => {
458 localStorageStub = createLocalStorageStub();
459 vi.stubGlobal("window", { localStorage: localStorageStub });
460 vi.stubGlobal("localStorage", localStorageStub);
461 // Reset module-level persistence state so tests don't bleed into each other.
462 hydratePersistedProjectState({ collapsedProjectCwds: [], expandedProjectCwds: [] });
463 });
464​
465 afterEach(() => {
466 vi.unstubAllGlobals();
467 });
468​
469 it("preserves all-collapsed project state across restart", () => {
470 // Regression: pre-fix, persistState only wrote `expandedProjectCwds`, so
471 // an empty array on rehydrate was indistinguishable from a fresh install
472 // and the syncProjects fallback re-expanded every row.
473 const projectA = { key: "kA", logicalKey: "kA", cwd: "/projA" };
474 const projectB = { key: "kB", logicalKey: "kB", cwd: "/projB" };
475​
476 let state = syncProjects(makeUiState(), [projectA, projectB]);
477 state = setProjectExpanded(state, projectA.key, false);
478 state = setProjectExpanded(state, projectB.key, false);
479 persistState(state);
480​
481 const persisted = JSON.parse(
482 localStorageStub.getItem(PERSISTED_STATE_KEY) ?? "{}",
483 ) as PersistedUiState;
484 hydratePersistedProjectState(persisted);
485 const rehydrated = syncProjects(makeUiState(), [projectA, projectB]);
486​
487 expect(rehydrated.projectExpandedById).toEqual({
488 [projectA.key]: false,
489 [projectB.key]: false,
490 });
491 });
492​
493 it("respects mixed expand state on rehydrate and defaults new projects to expanded", () => {
494 const projectA = { key: "kA", logicalKey: "kA", cwd: "/projA" };
495 const projectB = { key: "kB", logicalKey: "kB", cwd: "/projB" };
496 const projectC = { key: "kC", logicalKey: "kC", cwd: "/projC" };
497​
498 let state = syncProjects(makeUiState(), [projectA, projectB]);
499 state = setProjectExpanded(state, projectB.key, false);
500 persistState(state);
501​
502 const persisted = JSON.parse(
503 localStorageStub.getItem(PERSISTED_STATE_KEY) ?? "{}",
504 ) as PersistedUiState;
505 hydratePersistedProjectState(persisted);
506 const rehydrated = syncProjects(makeUiState(), [projectA, projectB, projectC]);
507​
508 expect(rehydrated.projectExpandedById).toEqual({
509 [projectA.key]: true,
510 [projectB.key]: false,
511 [projectC.key]: true,
512 });
513 });
514​
515 it("preserves legacy not-in-expanded-list = collapsed for one upgrade session", () => {
516 // Pre-fix shape only stored expandedProjectCwds. Absence of
517 // collapsedProjectCwds opts the session into the legacy fallback so
518 // upgrade users do not see previously collapsed rows pop open.
519 hydratePersistedProjectState({
520 expandedProjectCwds: ["/projA"],
521 });
522​
523 const rehydrated = syncProjects(makeUiState(), [
524 { key: "kA", logicalKey: "kA", cwd: "/projA" },
525 { key: "kB", logicalKey: "kB", cwd: "/projB" },
526 ]);
527​
528 expect(rehydrated.projectExpandedById).toEqual({
529 kA: true,
530 kB: false,
531 });
532 });
533​
534 it("preserves manual project order across restart", () => {
535 const projectA = { key: "kOrderA", logicalKey: "kOrderA", cwd: "/order-projA" };
536 const projectB = { key: "kOrderB", logicalKey: "kOrderB", cwd: "/order-projB" };
537 const projectC = { key: "kOrderC", logicalKey: "kOrderC", cwd: "/order-projC" };
538​
539 let state = syncProjects(makeUiState(), [projectA, projectB, projectC]);
540 state = reorderProjects(state, [projectC.key], [projectA.key]);
541 expect(state.projectOrder).toEqual([projectC.key, projectA.key, projectB.key]);
542 persistState(state);
543​
544 const persisted = JSON.parse(
545 localStorageStub.getItem(PERSISTED_STATE_KEY) ?? "{}",
546 ) as PersistedUiState;
547 expect(persisted.projectOrderCwds).toEqual([projectC.cwd, projectA.cwd, projectB.cwd]);
548​
549 hydratePersistedProjectState(persisted);
550 // Fresh state (empty projectOrder) so syncProjects derives order from
551 // persistedProjectOrderCwds rather than the in-memory projectOrder branch.
552 const rehydrated = syncProjects(makeUiState(), [projectA, projectB, projectC]);
553​
554 expect(rehydrated.projectOrder).toEqual([projectC.key, projectA.key, projectB.key]);
555 });
556​
557 it("preserves expand state across restart when project's logical key changes", () => {
558 // After restart, in-memory previousExpandedById is empty, so the
559 // previousLogicalKey-to-state bridge in syncProjects cannot help. The
560 // persisted-cwd fallback is the only mechanism that can carry collapse
561 // state across a restart that also flips a project into a new logical
562 // group (e.g. late-arriving repo metadata). This locks in that path.
563 const physicalKey = "env-local:/lk-restart-proj";
564 const previousLogicalKey = physicalKey;
565 const cwd = "/lk-restart-proj";
566​
567 let state = syncProjects(makeUiState(), [
568 { key: physicalKey, logicalKey: previousLogicalKey, cwd },
569 ]);
570 state = setProjectExpanded(state, previousLogicalKey, false);
571 persistState(state);
572​
573 const persisted = JSON.parse(
574 localStorageStub.getItem(PERSISTED_STATE_KEY) ?? "{}",
575 ) as PersistedUiState;
576 hydratePersistedProjectState(persisted);
577​
578 const nextLogicalKey = "lk-restart-canonical";
579 const rehydrated = syncProjects(makeUiState(), [
580 { key: physicalKey, logicalKey: nextLogicalKey, cwd },
581 ]);
582​
583 expect(rehydrated.projectExpandedById[nextLogicalKey]).toBe(false);
584 });
585});