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

shellReducer.test.ts

349 lines11.8 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("restores an omitted parent atomically with its new side chat", () => {
31 const child = {
32 ...v2ThreadShell,
33 id: ThreadId.make("child"),
34 sideChat: true,
35 lineage: { ...v2ThreadShell.lineage, parentThreadId: v2ThreadShell.id },
36 };
37 const next = applyShellStreamEvent(
38 { ...v2ShellSnapshot, threads: [] },
39 {
40 kind: "thread.updated",
41 sequence: 1,
42 location: "active",
43 thread: child,
44 parentThread: v2ThreadShell,
45 },
46 );
47 expect(next.threads.map((thread) => thread.id)).toEqual([v2ThreadShell.id, child.id]);
48 expect(next.snapshotSequence).toBe(1);
49 });
50 it("ignores stale project updates without mutating the snapshot", () => {
51 const snapshotWithProject = {
52 ...v2ShellSnapshot,
53 snapshotSequence: 4,
54 projects: [v2Project],
55 };
56​
57 for (const sequence of [3, 4]) {
58 const next = applyShellStreamEvent(snapshotWithProject, {
59 kind: "project.updated",
60 sequence,
61 project: { ...v2Project, title: "Stale Title" },
62 });
63​
64 expect(next).toBe(snapshotWithProject);
65 expect(next.snapshotSequence).toBe(4);
66 expect(next.projects[0]?.title).toBe(v2Project.title);
67 }
68 });
69​
70 it("keeps prior repositoryIdentity when a delta arrives with null identity", () => {
71 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
72 kind: "project.updated",
73 sequence: 1,
74 project: { ...v2Project, repositoryIdentity },
75 });
76 expect(withIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
77​
78 const withoutIdentity = applyShellStreamEvent(withIdentity, {
79 kind: "project.updated",
80 sequence: 2,
81 project: { ...v2Project, title: "Still same root", repositoryIdentity: null },
82 });
83 expect(withoutIdentity.projects[0]?.title).toBe("Still same root");
84 expect(withoutIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
85 });
86​
87 it("does not keep prior repositoryIdentity after a workspace root move", () => {
88 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
89 kind: "project.updated",
90 sequence: 1,
91 project: { ...v2Project, repositoryIdentity },
92 });
93​
94 const moved = applyShellStreamEvent(withIdentity, {
95 kind: "project.updated",
96 sequence: 2,
97 project: {
98 ...v2Project,
99 workspaceRoot: "/tmp/other-root",
100 repositoryIdentity: null,
101 },
102 });
103 expect(moved.projects[0]?.workspaceRoot).toBe("/tmp/other-root");
104 expect(moved.projects[0]?.repositoryIdentity).toBeNull();
105 });
106​
107 it("authoritative lower-sequence reset replaces client-ahead structural state", () => {
108 const previous = {
109 ...v2ShellSnapshot,
110 snapshotSequence: 9,
111 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
112 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
113 };
114 const next = mergeShellSnapshotProjects(previous, {
115 ...v2ShellSnapshot,
116 snapshotSequence: 3,
117 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
118 threads: [{ ...v2ThreadShell, title: "Server thread" }],
119 });
120​
121 expect(next.snapshotSequence).toBe(3);
122 expect(next.projects[0]?.title).toBe("Server reset");
123 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
124 expect(next.threads[0]?.title).toBe("Server thread");
125 });
126​
127 it("client-ahead survives lower marked enrichment then still resets on unmarked authoritative", () => {
128 const previous = {
129 ...v2ShellSnapshot,
130 snapshotSequence: 20,
131 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
132 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
133 };
134 const serverSnapshot = {
135 ...v2ShellSnapshot,
136 snapshotSequence: 3,
137 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
138 threads: [{ ...v2ThreadShell, title: "Server thread" }],
139 };
140​
141 const afterEnrichment = mergeShellSnapshotProjects(previous, serverSnapshot, {
142 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
143 });
144 expect(afterEnrichment.snapshotSequence).toBe(20);
145 expect(afterEnrichment.projects[0]?.title).toBe("Client ahead");
146 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
147 expect(afterEnrichment.threads[0]?.title).toBe("Local-only thread");
148​
149 const afterAuthoritative = mergeShellSnapshotProjects(afterEnrichment, serverSnapshot);
150 expect(afterAuthoritative.snapshotSequence).toBe(3);
151 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
152 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toBeNull();
153 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
154 });
155​
156 it("takes identity from a lower-sequence enrichment without reverting live thread state", () => {
157 const previous = {
158 ...v2ShellSnapshot,
159 snapshotSequence: 5,
160 projects: [{ ...v2Project, repositoryIdentity: null }],
161 threads: [{ ...v2ThreadShell, title: "Newest title" }],
162 };
163 const next = mergeShellSnapshotProjects(
164 previous,
165 {
166 ...v2ShellSnapshot,
167 snapshotSequence: 4,
168 projects: [{ ...v2Project, repositoryIdentity }],
169 threads: [{ ...v2ThreadShell, title: "Stale title" }],
170 },
171 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
172 );
173​
174 expect(next.snapshotSequence).toBe(5);
175 expect(next.threads[0]?.title).toBe("Newest title");
176 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
177 });
178​
179 it("lower-sequence enrichment cannot resurrect empty structural state", () => {
180 const previous = {
181 ...v2ShellSnapshot,
182 snapshotSequence: 7,
183 projects: [],
184 threads: [],
185 archivedThreads: [],
186 };
187 const next = mergeShellSnapshotProjects(
188 previous,
189 {
190 ...v2ShellSnapshot,
191 snapshotSequence: 4,
192 projects: [{ ...v2Project, repositoryIdentity }],
193 threads: [v2ThreadShell],
194 },
195 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
196 );
197​
198 expect(next.snapshotSequence).toBe(7);
199 expect(next.projects).toEqual([]);
200 expect(next.threads).toEqual([]);
201 });
202​
203 it("resolved null clears identity only for the marked root", () => {
204 const otherProject = {
205 ...v2Project,
206 id: ProjectId.make("project-other"),
207 workspaceRoot: "/workspace/other",
208 repositoryIdentity: otherIdentity,
209 };
210 const previous = {
211 ...v2ShellSnapshot,
212 snapshotSequence: 5,
213 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
214 };
215 const next = mergeShellSnapshotProjects(
216 previous,
217 {
218 ...v2ShellSnapshot,
219 snapshotSequence: 4,
220 projects: [
221 { ...v2Project, repositoryIdentity: null },
222 { ...otherProject, repositoryIdentity: null },
223 ],
224 },
225 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
226 );
227​
228 expect(next.snapshotSequence).toBe(5);
229 expect(next.projects).toHaveLength(2);
230 expect(next.projects[0]?.repositoryIdentity).toBeNull();
231 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
232 });
233​
234 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
235 const liveIdentity = {
236 canonicalKey: "github.com/example/live",
237 locator: {
238 source: "git-remote" as const,
239 remoteName: "origin",
240 remoteUrl: "https://github.com/example/live.git",
241 },
242 };
243 const staleIdentity = {
244 canonicalKey: "github.com/example/stale",
245 locator: {
246 source: "git-remote" as const,
247 remoteName: "origin",
248 remoteUrl: "https://github.com/example/stale.git",
249 },
250 };
251 const next = mergeShellSnapshotProjects(
252 {
253 ...v2ShellSnapshot,
254 snapshotSequence: 5,
255 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
256 },
257 {
258 ...v2ShellSnapshot,
259 snapshotSequence: 4,
260 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
261 },
262 { resolvedRepositoryIdentityRoots: [] },
263 );
264​
265 expect(next.snapshotSequence).toBe(5);
266 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
267 });
268​
269 it("keeps archive deltas out of the normal shell cache", () => {
270 const archived = applyShellStreamEvent(v2ShellSnapshot, {
271 kind: "thread.updated",
272 sequence: 3,
273 location: "archive",
274 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
275 });
276 expect(archived.threads).toEqual([]);
277 expect(archived.archivedThreads).toEqual([]);
278​
279 const active = applyShellStreamEvent(archived, {
280 kind: "thread.updated",
281 sequence: 4,
282 location: "active",
283 thread: v2ThreadShell,
284 });
285 expect(active.threads).toHaveLength(1);
286 expect(active.archivedThreads).toEqual([]);
287 });
288​
289 it("leaves unknown future events unchanged", () => {
290 const next = applyShellStreamEvent(v2ShellSnapshot, {
291 kind: "future.event",
292 sequence: 99,
293 } as never);
294​
295 expect(next).toBe(v2ShellSnapshot);
296 });
297});
298​
299describe("reuseUnchangedThreadShells", () => {
300 const link: ThreadPullRequestLink = {
301 host: "github.com",
302 repository: "pingdotgg/t3code",
303 number: 3,
304 url: "https://github.com/pingdotgg/t3code/pull/3",
305 source: "agent",
306 linkedAt: "2026-06-20T00:00:00.000Z",
307 snapshot: null,
308 stack: null,
309 };
310 const linked = { ...v2ThreadShell, pullRequests: [link] };
311 const other = { ...v2ThreadShell, id: ThreadId.make("thread-other"), title: "Other" };
312​
313 it("keeps the previous object for an unchanged row, even without its deferred links", () => {
314 const previous = { ...v2ShellSnapshot, threads: [linked, other] };
315 const { pullRequests: _links, ...withoutLinks } = linked;
316 const next = reuseUnchangedThreadShells(previous, {
317 ...v2ShellSnapshot,
318 snapshotSequence: 5,
319 threads: [withoutLinks, { ...other, title: "Renamed" }],
320 });
321 expect(next.snapshotSequence).toBe(5);
322 expect(next.threads[0]).toBe(linked);
323 expect(next.threads[1]).not.toBe(other);
324 expect(next.threads[1]?.title).toBe("Renamed");
325 });
326​
327 it("retains deferred links when the only row changes status", () => {
328 const { pullRequests: _links, ...withoutLinks } = linked;
329 const next = reuseUnchangedThreadShells(
330 { ...v2ShellSnapshot, threads: [linked] },
331 {
332 ...v2ShellSnapshot,
333 snapshotSequence: 5,
334 threads: [{ ...withoutLinks, status: "running" }],
335 },
336 );
337 expect(next.snapshotSequence).toBe(5);
338 expect(next.threads[0]?.status).toBe("running");
339 expect(next.threads[0]?.pullRequests).toBe(linked.pullRequests);
340 });
341​
342 it("keeps an explicit link removal when metadata also changes", () => {
343 const updated = { ...linked, title: "Renamed", pullRequests: [] };
344 const incoming = { ...v2ShellSnapshot, threads: [updated] };
345 const next = reuseUnchangedThreadShells({ ...v2ShellSnapshot, threads: [linked] }, incoming);
346 expect(next).toBe(incoming);
347 expect(next.threads[0]?.pullRequests).toEqual([]);
348 });
349});