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

shellReducer.test.ts

503 lines17.1 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("takes identity from a lower-sequence enrichment without reverting live thread state", () => {
230 const previous = {
231 ...v2ShellSnapshot,
232 snapshotSequence: 5,
233 projects: [{ ...v2Project, repositoryIdentity: null }],
234 threads: [{ ...v2ThreadShell, title: "Newest title" }],
235 };
236 const next = mergeShellSnapshotProjects(
237 previous,
238 {
239 ...v2ShellSnapshot,
240 snapshotSequence: 4,
241 projects: [{ ...v2Project, repositoryIdentity }],
242 threads: [{ ...v2ThreadShell, title: "Stale title" }],
243 },
244 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
245 );
246​
247 expect(next.snapshotSequence).toBe(5);
248 expect(next.threads[0]?.title).toBe("Newest title");
249 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
250 });
251​
252 it("treats higher-sequence compact enrichment as metadata-only", () => {
253 const previous = {
254 ...v2ShellSnapshot,
255 snapshotSequence: 5,
256 projects: [{ ...v2Project, repositoryIdentity: null }],
257 threads: [{ ...v2ThreadShell, title: "Newest title" }],
258 };
259 const next = mergeShellSnapshotProjects(
260 previous,
261 {
262 ...v2ShellSnapshot,
263 snapshotSequence: 8,
264 projects: [{ ...v2Project, repositoryIdentity }],
265 threads: [],
266 archivedThreads: [],
267 },
268 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
269 );
270​
271 expect(next.snapshotSequence).toBe(5);
272 expect(next.threads[0]?.title).toBe("Newest title");
273 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
274 });
275​
276 it("lower-sequence enrichment cannot resurrect empty structural state", () => {
277 const previous = {
278 ...v2ShellSnapshot,
279 snapshotSequence: 7,
280 projects: [],
281 threads: [],
282 archivedThreads: [],
283 };
284 const next = mergeShellSnapshotProjects(
285 previous,
286 {
287 ...v2ShellSnapshot,
288 snapshotSequence: 4,
289 projects: [{ ...v2Project, repositoryIdentity }],
290 threads: [v2ThreadShell],
291 },
292 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
293 );
294​
295 expect(next.snapshotSequence).toBe(7);
296 expect(next.projects).toEqual([]);
297 expect(next.threads).toEqual([]);
298 });
299​
300 it("resolved null clears identity only for the marked root", () => {
301 const otherProject = {
302 ...v2Project,
303 id: ProjectId.make("project-other"),
304 workspaceRoot: "/workspace/other",
305 repositoryIdentity: otherIdentity,
306 };
307 const previous = {
308 ...v2ShellSnapshot,
309 snapshotSequence: 5,
310 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
311 };
312 const next = mergeShellSnapshotProjects(
313 previous,
314 {
315 ...v2ShellSnapshot,
316 snapshotSequence: 4,
317 projects: [
318 { ...v2Project, repositoryIdentity: null },
319 { ...otherProject, repositoryIdentity: null },
320 ],
321 },
322 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
323 );
324​
325 expect(next.snapshotSequence).toBe(5);
326 expect(next.projects).toHaveLength(2);
327 expect(next.projects[0]?.repositoryIdentity).toBeNull();
328 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
329 });
330​
331 it("unrelated unresolved roots retain identity in the same enrichment snapshot", () => {
332 const otherProject = {
333 ...v2Project,
334 id: ProjectId.make("project-other"),
335 workspaceRoot: "/workspace/other",
336 repositoryIdentity: otherIdentity,
337 };
338 const previous = {
339 ...v2ShellSnapshot,
340 snapshotSequence: 3,
341 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
342 threads: [{ ...v2ThreadShell, title: "Current thread" }],
343 };
344 const next = mergeShellSnapshotProjects(
345 previous,
346 {
347 ...v2ShellSnapshot,
348 snapshotSequence: 3,
349 projects: [
350 { ...v2Project, repositoryIdentity },
351 { ...otherProject, repositoryIdentity: null },
352 ],
353 threads: [{ ...v2ThreadShell, title: "Stale thread" }],
354 },
355 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
356 );
357​
358 expect(next.threads[0]?.title).toBe("Current thread");
359 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
360 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
361 });
362​
363 it("clears identity for multiple roots resolved in one enrichment batch", () => {
364 const otherProject = {
365 ...v2Project,
366 id: ProjectId.make("project-other"),
367 workspaceRoot: "/workspace/other",
368 repositoryIdentity: otherIdentity,
369 };
370 const previous = {
371 ...v2ShellSnapshot,
372 snapshotSequence: 3,
373 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
374 };
375 const next = mergeShellSnapshotProjects(
376 previous,
377 {
378 ...v2ShellSnapshot,
379 snapshotSequence: 3,
380 projects: [
381 { ...v2Project, repositoryIdentity: null },
382 { ...otherProject, repositoryIdentity: null },
383 ],
384 },
385 {
386 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot, otherProject.workspaceRoot],
387 },
388 );
389​
390 expect(next.projects.map((project) => project.repositoryIdentity)).toEqual([null, null]);
391 });
392​
393 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
394 const liveIdentity = {
395 canonicalKey: "github.com/example/live",
396 locator: {
397 source: "git-remote" as const,
398 remoteName: "origin",
399 remoteUrl: "https://github.com/example/live.git",
400 },
401 };
402 const staleIdentity = {
403 canonicalKey: "github.com/example/stale",
404 locator: {
405 source: "git-remote" as const,
406 remoteName: "origin",
407 remoteUrl: "https://github.com/example/stale.git",
408 },
409 };
410 const next = mergeShellSnapshotProjects(
411 {
412 ...v2ShellSnapshot,
413 snapshotSequence: 5,
414 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
415 },
416 {
417 ...v2ShellSnapshot,
418 snapshotSequence: 4,
419 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
420 },
421 { resolvedRepositoryIdentityRoots: [] },
422 );
423​
424 expect(next.snapshotSequence).toBe(5);
425 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
426 });
427​
428 it("keeps archive deltas out of the normal shell cache", () => {
429 const archived = applyShellStreamEvent(v2ShellSnapshot, {
430 kind: "thread.updated",
431 sequence: 3,
432 location: "archive",
433 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
434 });
435 expect(archived.threads).toEqual([]);
436 expect(archived.archivedThreads).toEqual([]);
437​
438 const active = applyShellStreamEvent(archived, {
439 kind: "thread.updated",
440 sequence: 4,
441 location: "active",
442 thread: v2ThreadShell,
443 });
444 expect(active.threads).toHaveLength(1);
445 expect(active.archivedThreads).toEqual([]);
446 });
447​
448 it("removes a thread from either collection", () => {
449 const next = applyShellStreamEvent(v2ShellSnapshot, {
450 kind: "thread.removed",
451 sequence: 5,
452 location: "active",
453 threadId: ThreadId.make(v2ThreadShell.id),
454 });
455 expect(next.threads).toEqual([]);
456 expect(next.snapshotSequence).toBe(5);
457 });
458​
459 it("leaves unknown future events unchanged", () => {
460 const next = applyShellStreamEvent(v2ShellSnapshot, {
461 kind: "future.event",
462 sequence: 99,
463 } as never);
464​
465 expect(next).toBe(v2ShellSnapshot);
466 });
467});
468​
469describe("reuseUnchangedThreadShells", () => {
470 const link: ThreadPullRequestLink = {
471 host: "github.com",
472 repository: "pingdotgg/t3code",
473 number: 3,
474 url: "https://github.com/pingdotgg/t3code/pull/3",
475 source: "agent",
476 linkedAt: "2026-06-20T00:00:00.000Z",
477 snapshot: null,
478 stack: null,
479 };
480 const linked = { ...v2ThreadShell, pullRequests: [link] };
481 const other = { ...v2ThreadShell, id: ThreadId.make("thread-other"), title: "Other" };
482​
483 it("keeps the previous object for an unchanged row, even without its deferred links", () => {
484 const previous = { ...v2ShellSnapshot, threads: [linked, other] };
485 const { pullRequests: _links, ...withoutLinks } = linked;
486 const next = reuseUnchangedThreadShells(previous, {
487 ...v2ShellSnapshot,
488 snapshotSequence: 5,
489 threads: [withoutLinks, { ...other, title: "Renamed" }],
490 });
491 expect(next.snapshotSequence).toBe(5);
492 expect(next.threads[0]).toBe(linked);
493 expect(next.threads[1]).not.toBe(other);
494 expect(next.threads[1]?.title).toBe("Renamed");
495 });
496​
497 it("returns the incoming snapshot when no row is unchanged", () => {
498 const incoming = { ...v2ShellSnapshot, threads: [{ ...other, title: "Renamed" }] };
499 expect(reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [other] }, incoming)).toBe(
500 incoming,
501 );
502 });
503});