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