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

shellReducer.test.ts

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