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

shellReducer.test.ts

450 lines15.4 KB

1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { v2Project, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
5import { applyShellStreamEvent, mergeShellSnapshotProjects } from "./shellReducer.ts";
6​
7const repositoryIdentity = {
8 canonicalKey: "github.com/example/repo",
9 locator: {
10 source: "git-remote" as const,
11 remoteName: "origin",
12 remoteUrl: "https://github.com/example/repo.git",
13 },
14};
15​
16const otherIdentity = {
17 canonicalKey: "github.com/example/other",
18 locator: {
19 source: "git-remote" as const,
20 remoteName: "origin",
21 remoteUrl: "https://github.com/example/other.git",
22 },
23};
24​
25describe("applyShellStreamEvent", () => {
26 it("updates a thread in place without moving its siblings", () => {
27 const threads = ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) }));
28 const updated = { ...threads[1]!, title: "Streaming" };
29 const next = applyShellStreamEvent(
30 { ...v2ShellSnapshot, threads },
31 {
32 kind: "thread.updated",
33 sequence: 1,
34 location: "active",
35 thread: updated,
36 },
37 );
38 expect(next.threads.map((thread) => thread.id)).toEqual(["a", "b", "c"]);
39 expect(next.threads[0]).toBe(threads[0]);
40 expect(next.threads[1]).toBe(updated);
41 expect(next.threads[2]).toBe(threads[2]);
42 });
43​
44 it("ignores stale project updates without mutating the snapshot", () => {
45 const snapshotWithProject = {
46 ...v2ShellSnapshot,
47 snapshotSequence: 4,
48 projects: [v2Project],
49 };
50​
51 for (const sequence of [3, 4]) {
52 const next = applyShellStreamEvent(snapshotWithProject, {
53 kind: "project.updated",
54 sequence,
55 project: { ...v2Project, title: "Stale Title" },
56 });
57​
58 expect(next).toBe(snapshotWithProject);
59 expect(next.snapshotSequence).toBe(4);
60 expect(next.projects[0]?.title).toBe(v2Project.title);
61 }
62 });
63​
64 it("applies project updates and removals", () => {
65 const updated = applyShellStreamEvent(v2ShellSnapshot, {
66 kind: "project.updated",
67 sequence: 1,
68 project: { ...v2Project, title: "Updated" },
69 });
70 expect(updated.projects[0]?.title).toBe("Updated");
71 expect(updated.snapshotSequence).toBe(1);
72​
73 const removed = applyShellStreamEvent(updated, {
74 kind: "project.removed",
75 sequence: 2,
76 projectId: ProjectId.make(v2Project.id),
77 });
78 expect(removed.projects).toEqual([]);
79 });
80​
81 it("keeps prior repositoryIdentity when a delta arrives with null identity", () => {
82 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
83 kind: "project.updated",
84 sequence: 1,
85 project: { ...v2Project, repositoryIdentity },
86 });
87 expect(withIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
88​
89 const withoutIdentity = applyShellStreamEvent(withIdentity, {
90 kind: "project.updated",
91 sequence: 2,
92 project: { ...v2Project, title: "Still same root", repositoryIdentity: null },
93 });
94 expect(withoutIdentity.projects[0]?.title).toBe("Still same root");
95 expect(withoutIdentity.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
96 });
97​
98 it("does not keep prior repositoryIdentity after a workspace root move", () => {
99 const withIdentity = applyShellStreamEvent(v2ShellSnapshot, {
100 kind: "project.updated",
101 sequence: 1,
102 project: { ...v2Project, repositoryIdentity },
103 });
104​
105 const moved = applyShellStreamEvent(withIdentity, {
106 kind: "project.updated",
107 sequence: 2,
108 project: {
109 ...v2Project,
110 workspaceRoot: "/tmp/other-root",
111 repositoryIdentity: null,
112 },
113 });
114 expect(moved.projects[0]?.workspaceRoot).toBe("/tmp/other-root");
115 expect(moved.projects[0]?.repositoryIdentity).toBeNull();
116 });
117​
118 it("merges full snapshot projects without dropping known repositoryIdentity", () => {
119 const previous = {
120 ...v2ShellSnapshot,
121 projects: [{ ...v2Project, repositoryIdentity }],
122 };
123 const next = mergeShellSnapshotProjects(previous, {
124 ...v2ShellSnapshot,
125 snapshotSequence: 2,
126 projects: [{ ...v2Project, title: "Refreshed", repositoryIdentity: null }],
127 });
128 expect(next.projects[0]?.title).toBe("Refreshed");
129 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
130 });
131​
132 it("authoritative lower-sequence reset replaces client-ahead structural state", () => {
133 const previous = {
134 ...v2ShellSnapshot,
135 snapshotSequence: 9,
136 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
137 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
138 };
139 const next = mergeShellSnapshotProjects(previous, {
140 ...v2ShellSnapshot,
141 snapshotSequence: 3,
142 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
143 threads: [{ ...v2ThreadShell, title: "Server thread" }],
144 });
145​
146 expect(next.snapshotSequence).toBe(3);
147 expect(next.projects[0]?.title).toBe("Server reset");
148 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
149 expect(next.threads[0]?.title).toBe("Server thread");
150 });
151​
152 it("client-ahead survives lower marked enrichment then still resets on unmarked authoritative", () => {
153 const previous = {
154 ...v2ShellSnapshot,
155 snapshotSequence: 20,
156 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
157 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
158 };
159 const serverSnapshot = {
160 ...v2ShellSnapshot,
161 snapshotSequence: 3,
162 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
163 threads: [{ ...v2ThreadShell, title: "Server thread" }],
164 };
165​
166 // Lower-sequence marked enrichment is identity-only and must not replace structure.
167 const afterEnrichment = mergeShellSnapshotProjects(previous, serverSnapshot, {
168 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
169 });
170 expect(afterEnrichment.snapshotSequence).toBe(20);
171 expect(afterEnrichment.projects[0]?.title).toBe("Client ahead");
172 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
173 expect(afterEnrichment.threads[0]?.title).toBe("Local-only thread");
174​
175 // Unmarked authoritative at the same lower sequence still performs the reset.
176 const afterAuthoritative = mergeShellSnapshotProjects(afterEnrichment, serverSnapshot);
177 expect(afterAuthoritative.snapshotSequence).toBe(3);
178 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
179 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toBeNull();
180 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
181 });
182​
183 it("authoritative null retains identity then same-sequence marked enrichment clears it", () => {
184 const previous = {
185 ...v2ShellSnapshot,
186 snapshotSequence: 9,
187 projects: [{ ...v2Project, title: "Client ahead", repositoryIdentity }],
188 threads: [{ ...v2ThreadShell, title: "Local-only thread" }],
189 };
190 const serverSnapshot = {
191 ...v2ShellSnapshot,
192 snapshotSequence: 3,
193 projects: [{ ...v2Project, title: "Server reset", repositoryIdentity: null }],
194 threads: [{ ...v2ThreadShell, title: "Server thread" }],
195 };
196​
197 const afterAuthoritative = mergeShellSnapshotProjects(previous, serverSnapshot);
198 expect(afterAuthoritative.snapshotSequence).toBe(3);
199 expect(afterAuthoritative.projects[0]?.title).toBe("Server reset");
200 expect(afterAuthoritative.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
201 expect(afterAuthoritative.threads[0]?.title).toBe("Server thread");
202​
203 const afterEnrichment = mergeShellSnapshotProjects(afterAuthoritative, serverSnapshot, {
204 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot],
205 });
206 expect(afterEnrichment.snapshotSequence).toBe(3);
207 expect(afterEnrichment.projects[0]?.title).toBe("Server reset");
208 expect(afterEnrichment.projects[0]?.repositoryIdentity).toBeNull();
209 expect(afterEnrichment.threads[0]?.title).toBe("Server thread");
210 });
211​
212 it("takes identity from a lower-sequence enrichment without reverting live thread state", () => {
213 const previous = {
214 ...v2ShellSnapshot,
215 snapshotSequence: 5,
216 projects: [{ ...v2Project, repositoryIdentity: null }],
217 threads: [{ ...v2ThreadShell, title: "Newest title" }],
218 };
219 const next = mergeShellSnapshotProjects(
220 previous,
221 {
222 ...v2ShellSnapshot,
223 snapshotSequence: 4,
224 projects: [{ ...v2Project, repositoryIdentity }],
225 threads: [{ ...v2ThreadShell, title: "Stale title" }],
226 },
227 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
228 );
229​
230 expect(next.snapshotSequence).toBe(5);
231 expect(next.threads[0]?.title).toBe("Newest title");
232 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
233 });
234​
235 it("treats higher-sequence compact enrichment as metadata-only", () => {
236 const previous = {
237 ...v2ShellSnapshot,
238 snapshotSequence: 5,
239 projects: [{ ...v2Project, repositoryIdentity: null }],
240 threads: [{ ...v2ThreadShell, title: "Newest title" }],
241 };
242 const next = mergeShellSnapshotProjects(
243 previous,
244 {
245 ...v2ShellSnapshot,
246 snapshotSequence: 8,
247 projects: [{ ...v2Project, repositoryIdentity }],
248 threads: [],
249 archivedThreads: [],
250 },
251 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
252 );
253​
254 expect(next.snapshotSequence).toBe(5);
255 expect(next.threads[0]?.title).toBe("Newest title");
256 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
257 });
258​
259 it("lower-sequence enrichment cannot resurrect empty structural state", () => {
260 const previous = {
261 ...v2ShellSnapshot,
262 snapshotSequence: 7,
263 projects: [],
264 threads: [],
265 archivedThreads: [],
266 };
267 const next = mergeShellSnapshotProjects(
268 previous,
269 {
270 ...v2ShellSnapshot,
271 snapshotSequence: 4,
272 projects: [{ ...v2Project, repositoryIdentity }],
273 threads: [v2ThreadShell],
274 },
275 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
276 );
277​
278 expect(next.snapshotSequence).toBe(7);
279 expect(next.projects).toEqual([]);
280 expect(next.threads).toEqual([]);
281 });
282​
283 it("resolved null clears identity only for the marked root", () => {
284 const otherProject = {
285 ...v2Project,
286 id: ProjectId.make("project-other"),
287 workspaceRoot: "/workspace/other",
288 repositoryIdentity: otherIdentity,
289 };
290 const previous = {
291 ...v2ShellSnapshot,
292 snapshotSequence: 5,
293 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
294 };
295 const next = mergeShellSnapshotProjects(
296 previous,
297 {
298 ...v2ShellSnapshot,
299 snapshotSequence: 4,
300 projects: [
301 { ...v2Project, repositoryIdentity: null },
302 { ...otherProject, repositoryIdentity: null },
303 ],
304 },
305 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
306 );
307​
308 expect(next.snapshotSequence).toBe(5);
309 expect(next.projects).toHaveLength(2);
310 expect(next.projects[0]?.repositoryIdentity).toBeNull();
311 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
312 });
313​
314 it("unrelated unresolved roots retain identity in the same enrichment snapshot", () => {
315 const otherProject = {
316 ...v2Project,
317 id: ProjectId.make("project-other"),
318 workspaceRoot: "/workspace/other",
319 repositoryIdentity: otherIdentity,
320 };
321 const previous = {
322 ...v2ShellSnapshot,
323 snapshotSequence: 3,
324 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
325 threads: [{ ...v2ThreadShell, title: "Current thread" }],
326 };
327 const next = mergeShellSnapshotProjects(
328 previous,
329 {
330 ...v2ShellSnapshot,
331 snapshotSequence: 3,
332 projects: [
333 { ...v2Project, repositoryIdentity },
334 { ...otherProject, repositoryIdentity: null },
335 ],
336 threads: [{ ...v2ThreadShell, title: "Stale thread" }],
337 },
338 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
339 );
340​
341 expect(next.threads[0]?.title).toBe("Current thread");
342 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
343 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
344 });
345​
346 it("clears identity for multiple roots resolved in one enrichment batch", () => {
347 const otherProject = {
348 ...v2Project,
349 id: ProjectId.make("project-other"),
350 workspaceRoot: "/workspace/other",
351 repositoryIdentity: otherIdentity,
352 };
353 const previous = {
354 ...v2ShellSnapshot,
355 snapshotSequence: 3,
356 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
357 };
358 const next = mergeShellSnapshotProjects(
359 previous,
360 {
361 ...v2ShellSnapshot,
362 snapshotSequence: 3,
363 projects: [
364 { ...v2Project, repositoryIdentity: null },
365 { ...otherProject, repositoryIdentity: null },
366 ],
367 },
368 {
369 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot, otherProject.workspaceRoot],
370 },
371 );
372​
373 expect(next.projects.map((project) => project.repositoryIdentity)).toEqual([null, null]);
374 });
375​
376 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
377 const liveIdentity = {
378 canonicalKey: "github.com/example/live",
379 locator: {
380 source: "git-remote" as const,
381 remoteName: "origin",
382 remoteUrl: "https://github.com/example/live.git",
383 },
384 };
385 const staleIdentity = {
386 canonicalKey: "github.com/example/stale",
387 locator: {
388 source: "git-remote" as const,
389 remoteName: "origin",
390 remoteUrl: "https://github.com/example/stale.git",
391 },
392 };
393 const next = mergeShellSnapshotProjects(
394 {
395 ...v2ShellSnapshot,
396 snapshotSequence: 5,
397 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
398 },
399 {
400 ...v2ShellSnapshot,
401 snapshotSequence: 4,
402 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
403 },
404 { resolvedRepositoryIdentityRoots: [] },
405 );
406​
407 expect(next.snapshotSequence).toBe(5);
408 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
409 });
410​
411 it("keeps archive deltas out of the normal shell cache", () => {
412 const archived = applyShellStreamEvent(v2ShellSnapshot, {
413 kind: "thread.updated",
414 sequence: 3,
415 location: "archive",
416 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
417 });
418 expect(archived.threads).toEqual([]);
419 expect(archived.archivedThreads).toEqual([]);
420​
421 const active = applyShellStreamEvent(archived, {
422 kind: "thread.updated",
423 sequence: 4,
424 location: "active",
425 thread: v2ThreadShell,
426 });
427 expect(active.threads).toHaveLength(1);
428 expect(active.archivedThreads).toEqual([]);
429 });
430​
431 it("removes a thread from either collection", () => {
432 const next = applyShellStreamEvent(v2ShellSnapshot, {
433 kind: "thread.removed",
434 sequence: 5,
435 location: "active",
436 threadId: ThreadId.make(v2ThreadShell.id),
437 });
438 expect(next.threads).toEqual([]);
439 expect(next.snapshotSequence).toBe(5);
440 });
441​
442 it("leaves unknown future events unchanged", () => {
443 const next = applyShellStreamEvent(v2ShellSnapshot, {
444 kind: "future.event",
445 sequence: 99,
446 } as never);
447​
448 expect(next).toBe(v2ShellSnapshot);
449 });
450});