Open History to see the last commit
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("keeps the object of a project a full snapshot left unchanged", () => {
152 const prior = { ...v2Project, repositoryIdentity };
153 const next = mergeShellSnapshotProjects(
154 { ...v2ShellSnapshot, projects: [prior] },
155 {
156 ...v2ShellSnapshot,
157 snapshotSequence: 2,
158 projects: [{ ...v2Project, repositoryIdentity: structuredClone(repositoryIdentity) }],
159 },
160 );
161 expect(next.projects[0]).toBe(prior);
162 });
163
164 it("authoritative lower-sequence reset replaces client-ahead structural state", () => {
165 const previous = {
166 ...v2ShellSnapshot,
167 snapshotSequence: 9,
168 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
169 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
170 };
171 const next = mergeShellSnapshotProjects(previous, {
172 ...v2ShellSnapshot,
173 snapshotSequence: 3,
174 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
175 threads: [{ ...v2ThreadShell, title: "Server thread" }],
176 });
177
178 expect(next.snapshotSequence).toBe(3);
179 expect(next.projects[0]?.title).toBe("Server reset");
180 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
181 expect(next.threads[0]?.title).toBe("Server thread");
182 });
183
184 it("client-ahead survives lower marked enrichment then still resets on unmarked authoritative", () => {
185 const previous = {
186 ...v2ShellSnapshot,
187 snapshotSequence: 20,
188 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
189 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
190 };
191 const serverSnapshot = {
192 ...v2ShellSnapshot,
193 snapshotSequence: 3,
194 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
195 threads: [{ ...v2ThreadShell, title: "Server thread" }],
196 };
197
198 const afterEnrichment = mergeShellSnapshotProjects(previous, serverSnapshot, {
199 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
200 });
201 expect(afterEnrichment.snapshotSequence).toBe(20);
202 expect(afterEnrichment.projects[0]?.title).toBe("Client ahead");
203 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
204 expect(afterEnrichment.threads[0]?.title).toBe("Local-only thread");
205
206 const afterAuthoritative = mergeShellSnapshotProjects(afterEnrichment, serverSnapshot);
207 expect(afterAuthoritative.snapshotSequence).toBe(3);
208 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
209 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toBeNull();
210 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
211 });
212
213 it("authoritative null retains identity then same-sequence marked enrichment clears it", () => {
214 const previous = {
215 ...v2ShellSnapshot,
216 snapshotSequence: 9,
217 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
218 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
219 };
220 const serverSnapshot = {
221 ...v2ShellSnapshot,
222 snapshotSequence: 3,
223 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
224 threads: [{ ...v2ThreadShell, title: "Server thread" }],
225 };
226
227 const afterAuthoritative = mergeShellSnapshotProjects(previous, serverSnapshot);
228 expect(afterAuthoritative.snapshotSequence).toBe(3);
229 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
230 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
231 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
232
233 const afterEnrichment = mergeShellSnapshotProjects(afterAuthoritative, serverSnapshot, {
234 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
235 });
236 expect(afterEnrichment.snapshotSequence).toBe(3);
237 expect(afterEnrichment.projects[0]?.title).toBe("Server reset");
238 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
239 expect(afterEnrichment.threads[0]?.title).toBe("Server thread");
240 });
241
242 it("keeps the project object when an enrichment re-resolves an unchanged identity", () => {
243 const project = { ...v2Project, repositoryIdentity };
244 const previous = { ...v2ShellSnapshot, snapshotSequence: 5, projects: [project] };
245 const next = mergeShellSnapshotProjects(
246 previous,
247 {
248 ...v2ShellSnapshot,
249 snapshotSequence: 5,
250 projects: [{ ...v2Project, repositoryIdentity: { ...repositoryIdentity } }],
251 },
252 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
253 );
254
255 expect(next.projects[0]).toBe(project);
256 });
257
258 it("takes identity from a lower-sequence enrichment without reverting live thread state", () => {
259 const previous = {
260 ...v2ShellSnapshot,
261 snapshotSequence: 5,
262 projects: [{ ...v2Project, repositoryIdentity: null }],
263 threads: [{ ...v2ThreadShell, title: "Newest title" }],
264 };
265 const next = mergeShellSnapshotProjects(
266 previous,
267 {
268 ...v2ShellSnapshot,
269 snapshotSequence: 4,
270 projects: [{ ...v2Project, repositoryIdentity }],
271 threads: [{ ...v2ThreadShell, title: "Stale title" }],
272 },
273 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
274 );
275
276 expect(next.snapshotSequence).toBe(5);
277 expect(next.threads[0]?.title).toBe("Newest title");
278 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
279 });
280
281 it("treats higher-sequence compact enrichment as metadata-only", () => {
282 const previous = {
283 ...v2ShellSnapshot,
284 snapshotSequence: 5,
285 projects: [{ ...v2Project, repositoryIdentity: null }],
286 threads: [{ ...v2ThreadShell, title: "Newest title" }],
287 };
288 const next = mergeShellSnapshotProjects(
289 previous,
290 {
291 ...v2ShellSnapshot,
292 snapshotSequence: 8,
293 projects: [{ ...v2Project, repositoryIdentity }],
294 threads: [],
295 archivedThreads: [],
296 },
297 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
298 );
299
300 expect(next.snapshotSequence).toBe(5);
301 expect(next.threads[0]?.title).toBe("Newest title");
302 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
303 });
304
305 it("lower-sequence enrichment cannot resurrect empty structural state", () => {
306 const previous = {
307 ...v2ShellSnapshot,
308 snapshotSequence: 7,
309 projects: [],
310 threads: [],
311 archivedThreads: [],
312 };
313 const next = mergeShellSnapshotProjects(
314 previous,
315 {
316 ...v2ShellSnapshot,
317 snapshotSequence: 4,
318 projects: [{ ...v2Project, repositoryIdentity }],
319 threads: [v2ThreadShell],
320 },
321 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
322 );
323
324 expect(next.snapshotSequence).toBe(7);
325 expect(next.projects).toEqual([]);
326 expect(next.threads).toEqual([]);
327 });
328
329 it("resolved null clears identity only for the marked root", () => {
330 const otherProject = {
331 ...v2Project,
332 id: ProjectId.make("project-other"),
333 workspaceRoot: "/workspace/other",
334 repositoryIdentity: otherIdentity,
335 };
336 const previous = {
337 ...v2ShellSnapshot,
338 snapshotSequence: 5,
339 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
340 };
341 const next = mergeShellSnapshotProjects(
342 previous,
343 {
344 ...v2ShellSnapshot,
345 snapshotSequence: 4,
346 projects: [
347 { ...v2Project, repositoryIdentity: null },
348 { ...otherProject, repositoryIdentity: null },
349 ],
350 },
351 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
352 );
353
354 expect(next.snapshotSequence).toBe(5);
355 expect(next.projects).toHaveLength(2);
356 expect(next.projects[0]?.repositoryIdentity).toBeNull();
357 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
358 });
359
360 it("unrelated unresolved roots retain identity in the same enrichment snapshot", () => {
361 const otherProject = {
362 ...v2Project,
363 id: ProjectId.make("project-other"),
364 workspaceRoot: "/workspace/other",
365 repositoryIdentity: otherIdentity,
366 };
367 const previous = {
368 ...v2ShellSnapshot,
369 snapshotSequence: 3,
370 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
371 threads: [{ ...v2ThreadShell, title: "Current thread" }],
372 };
373 const next = mergeShellSnapshotProjects(
374 previous,
375 {
376 ...v2ShellSnapshot,
377 snapshotSequence: 3,
378 projects: [
379 { ...v2Project, repositoryIdentity },
380 { ...otherProject, repositoryIdentity: null },
381 ],
382 threads: [{ ...v2ThreadShell, title: "Stale thread" }],
383 },
384 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
385 );
386
387 expect(next.threads[0]?.title).toBe("Current thread");
388 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
389 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
390 });
391
392 it("clears identity for multiple roots resolved in one enrichment batch", () => {
393 const otherProject = {
394 ...v2Project,
395 id: ProjectId.make("project-other"),
396 workspaceRoot: "/workspace/other",
397 repositoryIdentity: otherIdentity,
398 };
399 const previous = {
400 ...v2ShellSnapshot,
401 snapshotSequence: 3,
402 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
403 };
404 const next = mergeShellSnapshotProjects(
405 previous,
406 {
407 ...v2ShellSnapshot,
408 snapshotSequence: 3,
409 projects: [
410 { ...v2Project, repositoryIdentity: null },
411 { ...otherProject, repositoryIdentity: null },
412 ],
413 },
414 {
415 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot, otherProject.workspaceRoot],
416 },
417 );
418
419 expect(next.projects.map((project) => project.repositoryIdentity)).toEqual([null, null]);
420 });
421
422 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
423 const liveIdentity = {
424 canonicalKey: "github.com/example/live",
425 locator: {
426 source: "git-remote" as const,
427 remoteName: "origin",
428 remoteUrl: "https://github.com/example/live.git",
429 },
430 };
431 const staleIdentity = {
432 canonicalKey: "github.com/example/stale",
433 locator: {
434 source: "git-remote" as const,
435 remoteName: "origin",
436 remoteUrl: "https://github.com/example/stale.git",
437 },
438 };
439 const next = mergeShellSnapshotProjects(
440 {
441 ...v2ShellSnapshot,
442 snapshotSequence: 5,
443 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
444 },
445 {
446 ...v2ShellSnapshot,
447 snapshotSequence: 4,
448 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
449 },
450 { resolvedRepositoryIdentityRoots: [] },
451 );
452
453 expect(next.snapshotSequence).toBe(5);
454 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
455 });
456
457 it("keeps archive deltas out of the normal shell cache", () => {
458 const archived = applyShellStreamEvent(v2ShellSnapshot, {
459 kind: "thread.updated",
460 sequence: 3,
461 location: "archive",
462 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
463 });
464 expect(archived.threads).toEqual([]);
465 expect(archived.archivedThreads).toEqual([]);
466
467 const active = applyShellStreamEvent(archived, {
468 kind: "thread.updated",
469 sequence: 4,
470 location: "active",
471 thread: v2ThreadShell,
472 });
473 expect(active.threads).toHaveLength(1);
474 expect(active.archivedThreads).toEqual([]);
475 });
476
477 it("removes a thread from either collection", () => {
478 const next = applyShellStreamEvent(v2ShellSnapshot, {
479 kind: "thread.removed",
480 sequence: 5,
481 location: "active",
482 threadId: ThreadId.make(v2ThreadShell.id),
483 });
484 expect(next.threads).toEqual([]);
485 expect(next.snapshotSequence).toBe(5);
486 });
487
488 it("leaves unknown future events unchanged", () => {
489 const next = applyShellStreamEvent(v2ShellSnapshot, {
490 kind: "future.event",
491 sequence: 99,
492 } as never);
493
494 expect(next).toBe(v2ShellSnapshot);
495 });
496});
497
498describe("reuseUnchangedThreadShells", () => {
499 const link: ThreadPullRequestLink = {
500 host: "github.com",
501 repository: "pingdotgg/t3code",
502 number: 3,
503 url: "https://github.com/pingdotgg/t3code/pull/3",
504 source: "agent",
505 linkedAt: "2026-06-20T00:00:00.000Z",
506 snapshot: null,
507 stack: null,
508 };
509 const linked = { ...v2ThreadShell, pullRequests: [link] };
510 const other = { ...v2ThreadShell, id: ThreadId.make("thread-other"), title: "Other" };
511
512 it("keeps the previous object for an unchanged row, even without its deferred links", () => {
513 const previous = { ...v2ShellSnapshot, threads: [linked, other] };
514 const { pullRequests: _links, ...withoutLinks } = linked;
515 const next = reuseUnchangedThreadShells(previous, {
516 ...v2ShellSnapshot,
517 snapshotSequence: 5,
518 threads: [withoutLinks, { ...other, title: "Renamed" }],
519 });
520 expect(next.snapshotSequence).toBe(5);
521 expect(next.threads[0]).toBe(linked);
522 expect(next.threads[1]).not.toBe(other);
523 expect(next.threads[1]?.title).toBe("Renamed");
524 });
525
526 it("returns the incoming snapshot when no row is unchanged", () => {
527 const incoming = { ...v2ShellSnapshot, threads: [{ ...other, title: "Renamed" }] };
528 expect(reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [other] }, incoming)).toBe(
529 incoming,
530 );
531 });
532
533 it("retains deferred links when the only row changes status", () => {
534 const { pullRequests: _links, ...withoutLinks } = linked;
535 const next = reuseUnchangedThreadShells(
536 { ...v2ShellSnapshot, threads: [linked] },
537 {
538 ...v2ShellSnapshot,
539 snapshotSequence: 5,
540 threads: [{ ...withoutLinks, status: "running" }],
541 },
542 );
543 expect(next.snapshotSequence).toBe(5);
544 expect(next.threads[0]?.status).toBe("running");
545 expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
546 });
547
548 it("retains deferred links on a renamed row alongside an unchanged sibling", () => {
549 const { pullRequests: _links, ...withoutLinks } = linked;
550 const next = reuseUnchangedThreadShells(
551 { ...v2ShellSnapshot, threads: [linked, other] },
552 { ...v2ShellSnapshot, threads: [{ ...withoutLinks, title: "Renamed" }, { ...other }] },
553 );
554 expect(next.threads[0]?.title).toBe("Renamed");
555 expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
556 expect(next.threads[1]).toBe(other);
557 });
558
559 it("keeps an explicit link removal when metadata also changes", () => {
560 const updated = { ...linked, title: "Renamed", pullRequests: [] };
561 const incoming = { ...v2ShellSnapshot, threads: [updated] };
562 const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
563 expect(next).toBe(incoming);
564 expect(next.threads[0]?.pullRequests).toEqual([]);
565 });
566
567 it("keeps explicitly refreshed links instead of the previous links", () => {
568 const updated = {
569 ...linked,
570 pullRequests: [{ ...link, number: 4, url: "https://github.com/pingdotgg/t3code/pull/4" }],
571 };
572 const incoming = { ...v2ShellSnapshot, threads: [updated] };
573 const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
574 expect(next).toBe(incoming);
575 expect(next.threads[0]?.pullRequests).toBe(updated.pullRequests);
576 });
577});