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

shellReducer.test.ts

408 lines14.0 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("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("lower-sequence enrichment cannot resurrect empty structural state", () => {
218 const previous = {
219 ...v2ShellSnapshot,
220 snapshotSequence: 7,
221 projects: [],
222 threads: [],
223 archivedThreads: [],
224 };
225 const next = mergeShellSnapshotProjects(
226 previous,
227 {
228 ...v2ShellSnapshot,
229 snapshotSequence: 4,
230 projects: [{ ...v2Project, repositoryIdentity }],
231 threads: [v2ThreadShell],
232 },
233 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
234 );
235​
236 expect(next.snapshotSequence).toBe(7);
237 expect(next.projects).toEqual([]);
238 expect(next.threads).toEqual([]);
239 });
240​
241 it("resolved null clears identity only for the marked root", () => {
242 const otherProject = {
243 ...v2Project,
244 id: ProjectId.make("project-other"),
245 workspaceRoot: "/workspace/other",
246 repositoryIdentity: otherIdentity,
247 };
248 const previous = {
249 ...v2ShellSnapshot,
250 snapshotSequence: 5,
251 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
252 };
253 const next = mergeShellSnapshotProjects(
254 previous,
255 {
256 ...v2ShellSnapshot,
257 snapshotSequence: 4,
258 projects: [
259 { ...v2Project, repositoryIdentity: null },
260 { ...otherProject, repositoryIdentity: null },
261 ],
262 },
263 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
264 );
265​
266 expect(next.snapshotSequence).toBe(5);
267 expect(next.projects).toHaveLength(2);
268 expect(next.projects[0]?.repositoryIdentity).toBeNull();
269 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
270 });
271​
272 it("unrelated unresolved roots retain identity in the same enrichment snapshot", () => {
273 const otherProject = {
274 ...v2Project,
275 id: ProjectId.make("project-other"),
276 workspaceRoot: "/workspace/other",
277 repositoryIdentity: otherIdentity,
278 };
279 const previous = {
280 ...v2ShellSnapshot,
281 snapshotSequence: 3,
282 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
283 threads: [{ ...v2ThreadShell, title: "Current thread" }],
284 };
285 const next = mergeShellSnapshotProjects(
286 previous,
287 {
288 ...v2ShellSnapshot,
289 snapshotSequence: 3,
290 projects: [
291 { ...v2Project, repositoryIdentity },
292 { ...otherProject, repositoryIdentity: null },
293 ],
294 threads: [{ ...v2ThreadShell, title: "Stale thread" }],
295 },
296 { resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot] },
297 );
298​
299 expect(next.threads[0]?.title).toBe("Current thread");
300 expect(next.projects[0]?.repositoryIdentity).toEqual(repositoryIdentity);
301 expect(next.projects[1]?.repositoryIdentity).toEqual(otherIdentity);
302 });
303​
304 it("clears identity for multiple roots resolved in one enrichment batch", () => {
305 const otherProject = {
306 ...v2Project,
307 id: ProjectId.make("project-other"),
308 workspaceRoot: "/workspace/other",
309 repositoryIdentity: otherIdentity,
310 };
311 const previous = {
312 ...v2ShellSnapshot,
313 snapshotSequence: 3,
314 projects: [{ ...v2Project, repositoryIdentity }, otherProject],
315 };
316 const next = mergeShellSnapshotProjects(
317 previous,
318 {
319 ...v2ShellSnapshot,
320 snapshotSequence: 3,
321 projects: [
322 { ...v2Project, repositoryIdentity: null },
323 { ...otherProject, repositoryIdentity: null },
324 ],
325 },
326 {
327 resolvedRepositoryIdentityRoots: [v2Project.workspaceRoot, otherProject.workspaceRoot],
328 },
329 );
330​
331 expect(next.projects.map((project) => project.repositoryIdentity)).toEqual([null, null]);
332 });
333​
334 it("does not replace a live identity with one from an unmarked lower-sequence enrichment", () => {
335 const liveIdentity = {
336 canonicalKey: "github.com/example/live",
337 locator: {
338 source: "git-remote" as const,
339 remoteName: "origin",
340 remoteUrl: "https://github.com/example/live.git",
341 },
342 };
343 const staleIdentity = {
344 canonicalKey: "github.com/example/stale",
345 locator: {
346 source: "git-remote" as const,
347 remoteName: "origin",
348 remoteUrl: "https://github.com/example/stale.git",
349 },
350 };
351 const next = mergeShellSnapshotProjects(
352 {
353 ...v2ShellSnapshot,
354 snapshotSequence: 5,
355 projects: [{ ...v2Project, repositoryIdentity: liveIdentity }],
356 },
357 {
358 ...v2ShellSnapshot,
359 snapshotSequence: 4,
360 projects: [{ ...v2Project, repositoryIdentity: staleIdentity }],
361 },
362 { resolvedRepositoryIdentityRoots: [] },
363 );
364​
365 expect(next.snapshotSequence).toBe(5);
366 expect(next.projects[0]?.repositoryIdentity).toEqual(liveIdentity);
367 });
368​
369 it("moves a thread between active and archive without duplicating it", () => {
370 const archived = applyShellStreamEvent(v2ShellSnapshot, {
371 kind: "thread.updated",
372 sequence: 3,
373 location: "archive",
374 thread: { ...v2ThreadShell, archivedAt: v2ThreadShell.updatedAt },
375 });
376 expect(archived.threads).toEqual([]);
377 expect(archived.archivedThreads).toHaveLength(1);
378​
379 const active = applyShellStreamEvent(archived, {
380 kind: "thread.updated",
381 sequence: 4,
382 location: "active",
383 thread: v2ThreadShell,
384 });
385 expect(active.threads).toHaveLength(1);
386 expect(active.archivedThreads).toEqual([]);
387 });
388​
389 it("removes a thread from either collection", () => {
390 const next = applyShellStreamEvent(v2ShellSnapshot, {
391 kind: "thread.removed",
392 sequence: 5,
393 location: "active",
394 threadId: ThreadId.make(v2ThreadShell.id),
395 });
396 expect(next.threads).toEqual([]);
397 expect(next.snapshotSequence).toBe(5);
398 });
399​
400 it("leaves unknown future events unchanged", () => {
401 const next = applyShellStreamEvent(v2ShellSnapshot, {
402 kind: "future.event",
403 sequence: 99,
404 } as never);
405​
406 expect(next).toBe(v2ShellSnapshot);
407 });
408});