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

shellReducer.test.ts

564 lines19.6 KB

1import { ProjectId, ThreadId, type ThreadPullRequestLink } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { v2Project, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
5import {
6 applyShellStreamEvent,
7 mergeShellSnapshotProjects,
8 reuseUnchangedThreadShells,
9} from "./shellReducer.ts";
10​
11const repositoryIdentity = {
12 canonicalKey: "github.com/example/repo",
13 locator: {
14 source: "git-remote" as const,
15 remoteName: "origin",
16 remoteUrl: "https://github.com/example/repo.git",
17 },
18};
19​
20const otherIdentity = {
21 canonicalKey: "github.com/example/other",
22 locator: {
23 source: "git-remote" as const,
24 remoteName: "origin",
25 remoteUrl: "https://github.com/example/other.git",
26 },
27};
28​
29describe("applyShellStreamEvent", () => {
30 it("updates a thread in place without moving its siblings", () => {
31 const threads = ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) }));
32 const updated = { ...threads[1]!, title: "Streaming" };
33 const next = applyShellStreamEvent(
34 { ...v2ShellSnapshot, threads },
35 {
36 kind: "thread.updated",
37 sequence: 1,
38 location: "active",
39 thread: updated,
40 },
41 );
42 expect(next.threads.map((thread) => thread.id)).toEqual(["a", "b", "c"]);
43 expect(next.threads[0]).toBe(threads[0]);
44 expect(next.threads[1]).toBe(updated);
45 expect(next.threads[2]).toBe(threads[2]);
46 });
47​
48 it("keeps the thread list and object when an update changes nothing", () => {
49 const threads = ["a", "b"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) }));
50 const next = applyShellStreamEvent(
51 { ...v2ShellSnapshot, threads },
52 {
53 kind: "thread.updated",
54 sequence: 1,
55 location: "active",
56 thread: { ...threads[1]! },
57 },
58 );
59 expect(next.threads).toBe(threads);
60 expect(next.snapshotSequence).toBe(1);
61 });
62​
63 it("ignores stale project updates without mutating the snapshot", () => {
64 const snapshotWithProject = {
65 ...v2ShellSnapshot,
66 snapshotSequence: 4,
67 projects: [v2Project],
68 };
69​
70 for (const sequence of [3, 4]) {
71 const next = applyShellStreamEvent(snapshotWithProject, {
72 kind: "project.updated",
73 sequence,
74 project: { ...v2Project, title: "Stale Title" },
75 });
76​
77 expect(next).toBe(snapshotWithProject);
78 expect(next.snapshotSequence).toBe(4);
79 expect(next.projects[0]?.title).toBe(v2Project.title);
80 }
81 });
82​
83 it("applies project updates and removals", () => {
84 const updated = applyShellStreamEvent(v2ShellSnapshot, {
85 kind: "project.updated",
86 sequence: 1,
87 project: { ...v2Project, title: "Updated" },
88 });
89 expect(updated.projects[0]?.title).toBe("Updated");
90 expect(updated.snapshotSequence).toBe(1);
91​
92 const removed = applyShellStreamEvent(updated, {
93 kind: "project.removed",
94 sequence: 2,
95 projectId: ProjectId.make(v2Project.id),
96 });
97 expect(removed.projects).toEqual([]);
98 });
99​
100 it("keeps prior repositoryIdentity when a delta arrives with null identity", () => {
101 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
102 kind: "project.updated",
103 sequence: 1,
104 project: { ...v2Project, repositoryIdentity },
105 });
106 expect(withIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
107​
108 const withoutIdentity = applyShellStreamEvent(withIdentity, {
109 kind: "project.updated",
110 sequence: 2,
111 project: { ...v2Project, title: "Still same root", repositoryIdentity: null },
112 });
113 expect(withoutIdentity.projects[0]?.title).toBe("Still same root");
114 expect(withoutIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
115 });
116​
117 it("does not keep prior repositoryIdentity after a workspace root move", () => {
118 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
119 kind: "project.updated",
120 sequence: 1,
121 project: { ...v2Project, repositoryIdentity },
122 });
123​
124 const moved = applyShellStreamEvent(withIdentity, {
125 kind: "project.updated",
126 sequence: 2,
127 project: {
128 ...v2Project,
129 workspaceRoot: "/tmp/other-root",
130 repositoryIdentity: null,
131 },
132 });
133 expect(moved.projects[0]?.workspaceRoot).toBe("/tmp/other-root");
134 expect(moved.projects[0]?.repositoryIdentity).toBeNull();
135 });
136​
137 it("merges full snapshot projects without dropping known repositoryIdentity", () => {
138 const previous = {
139 ...v2ShellSnapshot,
140 projects: [{ ...v2Project, repositoryIdentity }],
141 };
142 const next = mergeShellSnapshotProjects(previous, {
143 ...v2ShellSnapshot,
144 snapshotSequence: 2,
145 projects: [{ ...v2Project, title: "Refreshed", repositoryIdentity: null }],
146 });
147 expect(next.projects[0]?.title).toBe("Refreshed");
148 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
149 });
150​
151 it("authoritative lower-sequence reset replaces client-ahead structural state", () => {
152 const previous = {
153 ...v2ShellSnapshot,
154 snapshotSequence: 9,
155 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
156 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
157 };
158 const next = mergeShellSnapshotProjects(previous, {
159 ...v2ShellSnapshot,
160 snapshotSequence: 3,
161 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
162 threads: [{ ...v2ThreadShell, title: "Server thread" }],
163 });
164​
165 expect(next.snapshotSequence).toBe(3);
166 expect(next.projects[0]?.title).toBe("Server reset");
167 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
168 expect(next.threads[0]?.title).toBe("Server thread");
169 });
170​
171 it("client-ahead survives lower marked enrichment then still resets on unmarked authoritative", () => {
172 const previous = {
173 ...v2ShellSnapshot,
174 snapshotSequence: 20,
175 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
176 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
177 };
178 const serverSnapshot = {
179 ...v2ShellSnapshot,
180 snapshotSequence: 3,
181 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
182 threads: [{ ...v2ThreadShell, title: "Server thread" }],
183 };
184​
185 const afterEnrichment = mergeShellSnapshotProjects(previous, serverSnapshot, {
186 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
187 });
188 expect(afterEnrichment.snapshotSequence).toBe(20);
189 expect(afterEnrichment.projects[0]?.title).toBe("Client ahead");
190 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
191 expect(afterEnrichment.threads[0]?.title).toBe("Local-only thread");
192​
193 const afterAuthoritative = mergeShellSnapshotProjects(afterEnrichment, serverSnapshot);
194 expect(afterAuthoritative.snapshotSequence).toBe(3);
195 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
196 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toBeNull();
197 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
198 });
199​
200 it("authoritative null retains identity then same-sequence marked enrichment clears it", () => {
201 const previous = {
202 ...v2ShellSnapshot,
203 snapshotSequence: 9,
204 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
205 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
206 };
207 const serverSnapshot = {
208 ...v2ShellSnapshot,
209 snapshotSequence: 3,
210 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
211 threads: [{ ...v2ThreadShell, title: "Server thread" }],
212 };
213​
214 const afterAuthoritative = mergeShellSnapshotProjects(previous, serverSnapshot);
215 expect(afterAuthoritative.snapshotSequence).toBe(3);
216 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
217 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
218 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
219​
220 const afterEnrichment = mergeShellSnapshotProjects(afterAuthoritative, serverSnapshot, {
221 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
222 });
223 expect(afterEnrichment.snapshotSequence).toBe(3);
224 expect(afterEnrichment.projects[0]?.title).toBe("Server reset");
225 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
226 expect(afterEnrichment.threads[0]?.title).toBe("Server thread");
227 });
228​
229 it("keeps the project object when an enrichment re-resolves an unchanged identity", () => {
230 const project = { ...v2Project, repositoryIdentity };
231 const previous = { ...v2ShellSnapshot, snapshotSequence: 5, projects: [project] };
232 const next = mergeShellSnapshotProjects(
233 previous,
234 {
235 ...v2ShellSnapshot,
236 snapshotSequence: 5,
237 projects: [{ ...v2Project, repositoryIdentity: { ...repositoryIdentity } }],
238 },
239 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
240 );
241​
242 expect(next.projects[0]).toBe(project);
243 });
244​
245 it("takes identity from a lower-sequence enrichment without reverting live thread state", () => {
246 const previous = {
247 ...v2ShellSnapshot,
248 snapshotSequence: 5,
249 projects: [{ ...v2Project, repositoryIdentity: null }],
250 threads: [{ ...v2ThreadShell, title: "Newest title" }],
251 };
252 const next = mergeShellSnapshotProjects(
253 previous,
254 {
255 ...v2ShellSnapshot,
256 snapshotSequence: 4,
257 projects: [{ ...v2Project, repositoryIdentity }],
258 threads: [{ ...v2ThreadShell, title: "Stale title" }],
259 },
260 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
261 );
262​
263 expect(next.snapshotSequence).toBe(5);
264 expect(next.threads[0]?.title).toBe("Newest title");
265 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
266 });
267​
268 it("treats higher-sequence compact enrichment as metadata-only", () => {
269 const previous = {
270 ...v2ShellSnapshot,
271 snapshotSequence: 5,
272 projects: [{ ...v2Project, repositoryIdentity: null }],
273 threads: [{ ...v2ThreadShell, title: "Newest title" }],
274 };
275 const next = mergeShellSnapshotProjects(
276 previous,
277 {
278 ...v2ShellSnapshot,
279 snapshotSequence: 8,
280 projects: [{ ...v2Project, repositoryIdentity }],
281 threads: [],
282 archivedThreads: [],
283 },
284 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
285 );
286​
287 expect(next.snapshotSequence).toBe(5);
288 expect(next.threads[0]?.title).toBe("Newest title");
289 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
290 });
291​
292 it("lower-sequence enrichment cannot resurrect empty structural state", () => {
293 const previous = {
294 ...v2ShellSnapshot,
295 snapshotSequence: 7,
296 projects: [],
297 threads: [],
298 archivedThreads: [],
299 };
300 const next = mergeShellSnapshotProjects(
301 previous,
302 {
303 ...v2ShellSnapshot,
304 snapshotSequence: 4,
305 projects: [{ ...v2Project, repositoryIdentity }],
306 threads: [v2ThreadShell],
307 },
308 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
309 );
310​
311 expect(next.snapshotSequence).toBe(7);
312 expect(next.projects).toEqual([]);
313 expect(next.threads).toEqual([]);
314 });
315​
316 it("resolved null clears identity only for the marked root", () => {
317 const otherProject = {
318 ...v2Project,
319 id: ProjectId.make("project-other"),
320 workspaceRoot: "/workspace/other",
321 repositoryIdentity: otherIdentity,
322 };
323 const previous = {
324 ...v2ShellSnapshot,
325 snapshotSequence: 5,
326 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
327 };
328 const next = mergeShellSnapshotProjects(
329 previous,
330 {
331 ...v2ShellSnapshot,
332 snapshotSequence: 4,
333 projects: [
334 { ...v2Project, repositoryIdentity: null },
335 { ...otherProject, repositoryIdentity: null },
336 ],
337 },
338 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
339 );
340​
341 expect(next.snapshotSequence).toBe(5);
342 expect(next.projects).toHaveLength(2);
343 expect(next.projects[0]?.repositoryIdentity).toBeNull();
344 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
345 });
346​
347 it("unrelated unresolved roots retain identity in the same enrichment snapshot", () => {
348 const otherProject = {
349 ...v2Project,
350 id: ProjectId.make("project-other"),
351 workspaceRoot: "/workspace/other",
352 repositoryIdentity: otherIdentity,
353 };
354 const previous = {
355 ...v2ShellSnapshot,
356 snapshotSequence: 3,
357 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
358 threads: [{ ...v2ThreadShell, title: "Current thread" }],
359 };
360 const next = mergeShellSnapshotProjects(
361 previous,
362 {
363 ...v2ShellSnapshot,
364 snapshotSequence: 3,
365 projects: [
366 { ...v2Project, repositoryIdentity },
367 { ...otherProject, repositoryIdentity: null },
368 ],
369 threads: [{ ...v2ThreadShell, title: "Stale thread" }],
370 },
371 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
372 );
373​
374 expect(next.threads[0]?.title).toBe("Current thread");
375 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
376 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
377 });
378​
379 it("clears identity for multiple roots resolved in one enrichment batch", () => {
380 const otherProject = {
381 ...v2Project,
382 id: ProjectId.make("project-other"),
383 workspaceRoot: "/workspace/other",
384 repositoryIdentity: otherIdentity,
385 };
386 const previous = {
387 ...v2ShellSnapshot,
388 snapshotSequence: 3,
389 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
390 };
391 const next = mergeShellSnapshotProjects(
392 previous,
393 {
394 ...v2ShellSnapshot,
395 snapshotSequence: 3,
396 projects: [
397 { ...v2Project, repositoryIdentity: null },
398 { ...otherProject, repositoryIdentity: null },
399 ],
400 },
401 {
402 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot, otherProject.workspaceRoot],
403 },
404 );
405​
406 expect(next.projects.map((project) => project.repositoryIdentity)).toEqual([null, null]);
407 });
408​
409 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
410 const liveIdentity = {
411 canonicalKey: "github.com/example/live",
412 locator: {
413 source: "git-remote" as const,
414 remoteName: "origin",
415 remoteUrl: "https://github.com/example/live.git",
416 },
417 };
418 const staleIdentity = {
419 canonicalKey: "github.com/example/stale",
420 locator: {
421 source: "git-remote" as const,
422 remoteName: "origin",
423 remoteUrl: "https://github.com/example/stale.git",
424 },
425 };
426 const next = mergeShellSnapshotProjects(
427 {
428 ...v2ShellSnapshot,
429 snapshotSequence: 5,
430 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
431 },
432 {
433 ...v2ShellSnapshot,
434 snapshotSequence: 4,
435 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
436 },
437 { resolvedRepositoryIdentityRoots: [] },
438 );
439​
440 expect(next.snapshotSequence).toBe(5);
441 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
442 });
443​
444 it("keeps archive deltas out of the normal shell cache", () => {
445 const archived = applyShellStreamEvent(v2ShellSnapshot, {
446 kind: "thread.updated",
447 sequence: 3,
448 location: "archive",
449 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
450 });
451 expect(archived.threads).toEqual([]);
452 expect(archived.archivedThreads).toEqual([]);
453​
454 const active = applyShellStreamEvent(archived, {
455 kind: "thread.updated",
456 sequence: 4,
457 location: "active",
458 thread: v2ThreadShell,
459 });
460 expect(active.threads).toHaveLength(1);
461 expect(active.archivedThreads).toEqual([]);
462 });
463​
464 it("removes a thread from either collection", () => {
465 const next = applyShellStreamEvent(v2ShellSnapshot, {
466 kind: "thread.removed",
467 sequence: 5,
468 location: "active",
469 threadId: ThreadId.make(v2ThreadShell.id),
470 });
471 expect(next.threads).toEqual([]);
472 expect(next.snapshotSequence).toBe(5);
473 });
474​
475 it("leaves unknown future events unchanged", () => {
476 const next = applyShellStreamEvent(v2ShellSnapshot, {
477 kind: "future.event",
478 sequence: 99,
479 } as never);
480​
481 expect(next).toBe(v2ShellSnapshot);
482 });
483});
484​
485describe("reuseUnchangedThreadShells", () => {
486 const link: ThreadPullRequestLink = {
487 host: "github.com",
488 repository: "pingdotgg/t3code",
489 number: 3,
490 url: "https://github.com/pingdotgg/t3code/pull/3",
491 source: "agent",
492 linkedAt: "2026-06-20T00:00:00.000Z",
493 snapshot: null,
494 stack: null,
495 };
496 const linked = { ...v2ThreadShell, pullRequests: [link] };
497 const other = { ...v2ThreadShell, id: ThreadId.make("thread-other"), title: "Other" };
498​
499 it("keeps the previous object for an unchanged row, even without its deferred links", () => {
500 const previous = { ...v2ShellSnapshot, threads: [linked, other] };
501 const { pullRequests: _links, ...withoutLinks } = linked;
502 const next = reuseUnchangedThreadShells(previous, {
503 ...v2ShellSnapshot,
504 snapshotSequence: 5,
505 threads: [withoutLinks, { ...other, title: "Renamed" }],
506 });
507 expect(next.snapshotSequence).toBe(5);
508 expect(next.threads[0]).toBe(linked);
509 expect(next.threads[1]).not.toBe(other);
510 expect(next.threads[1]?.title).toBe("Renamed");
511 });
512​
513 it("returns the incoming snapshot when no row is unchanged", () => {
514 const incoming = { ...v2ShellSnapshot, threads: [{ ...other, title: "Renamed" }] };
515 expect(reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [other] }, incoming)).toBe(
516 incoming,
517 );
518 });
519​
520 it("retains deferred links when the only row changes status", () => {
521 const { pullRequests: _links, ...withoutLinks } = linked;
522 const next = reuseUnchangedThreadShells(
523 { ...v2ShellSnapshot, threads: [linked] },
524 {
525 ...v2ShellSnapshot,
526 snapshotSequence: 5,
527 threads: [{ ...withoutLinks, status: "running" }],
528 },
529 );
530 expect(next.snapshotSequence).toBe(5);
531 expect(next.threads[0]?.status).toBe("running");
532 expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
533 });
534​
535 it("retains deferred links on a renamed row alongside an unchanged sibling", () => {
536 const { pullRequests: _links, ...withoutLinks } = linked;
537 const next = reuseUnchangedThreadShells(
538 { ...v2ShellSnapshot, threads: [linked, other] },
539 { ...v2ShellSnapshot, threads: [{ ...withoutLinks, title: "Renamed" }, { ...other }] },
540 );
541 expect(next.threads[0]?.title).toBe("Renamed");
542 expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
543 expect(next.threads[1]).toBe(other);
544 });
545​
546 it("keeps an explicit link removal when metadata also changes", () => {
547 const updated = { ...linked, title: "Renamed", pullRequests: [] };
548 const incoming = { ...v2ShellSnapshot, threads: [updated] };
549 const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
550 expect(next).toBe(incoming);
551 expect(next.threads[0]?.pullRequests).toEqual([]);
552 });
553​
554 it("keeps explicitly refreshed links instead of the previous links", () => {
555 const updated = {
556 ...linked,
557 pullRequests: [{ ...link, number: 4, url: "https://github.com/pingdotgg/t3code/pull/4" }],
558 };
559 const incoming = { ...v2ShellSnapshot, threads: [updated] };
560 const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
561 expect(next).toBe(incoming);
562 expect(next.threads[0]?.pullRequests).toBe(updated.pullRequests);
563 });
564});