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

shellReducer.test.ts

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