Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import type {
3 ApplicationStoredEvent,
4 OrchestrationV2ShellSnapshot,
5 OrchestrationV2StoredEvent,
6 OrchestrationV2ThreadShell,
7} from "@t3tools/contracts";
8import { ProjectId, ThreadId } from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Stream from "effect/Stream";
11
12import {
13 archivedShellStreamItemFromThreadShell,
14 buildActiveShellSnapshot,
15 coalesceShellApplicationEvents,
16 coalesceStoredThreadEvents,
17 composeShellStreamWithEnrichment,
18 dedupeShellEnrichment,
19 shellStreamItemFromEnrichmentRefresh,
20 shellStreamItemFromThreadShell,
21 shellStreamItemsFromInitialSnapshot,
22 shellStreamItemsFromResumeSnapshot,
23} from "./ShellStream.ts";
24
25function project(sequence: number, id: string): ApplicationStoredEvent {
26 return {
27 sequence,
28 aggregateKind: "project",
29 aggregateId: id,
30 } as ApplicationStoredEvent;
31}
32
33function thread(sequence: number, id: string): ApplicationStoredEvent {
34 return {
35 sequence,
36 event: { threadId: id },
37 } as ApplicationStoredEvent;
38}
39
40const emptyShellSnapshot = {
41 schemaVersion: 1,
42 snapshotSequence: 0,
43 projects: [],
44 threads: [],
45 archivedThreads: [],
46} as OrchestrationV2ShellSnapshot;
47
48describe("buildActiveShellSnapshot", () => {
49 it("never duplicates archived rows into the regular shell", () => {
50 const active = shellFixture({ archivedAt: null });
51 const archived = shellFixture({
52 id: ThreadId.make("thread-archived"),
53 archivedAt: "2026-07-30T00:00:00.000Z" as never,
54 });
55
56 expect(
57 buildActiveShellSnapshot({
58 projects: [],
59 threads: {
60 schemaVersion: 1,
61 snapshotSequence: 4,
62 threads: [active],
63 archivedThreads: [archived],
64 },
65 snapshotSequence: 7,
66 }),
67 ).toEqual({
68 schemaVersion: 1,
69 snapshotSequence: 7,
70 projects: [],
71 threads: [active],
72 archivedThreads: [],
73 });
74 });
75});
76
77describe("coalesceShellApplicationEvents", () => {
78 it("keeps the newest event per aggregate and preserves sequence order", () => {
79 expect(
80 coalesceShellApplicationEvents([
81 thread(2, "thread-a"),
82 project(3, "project-a"),
83 thread(4, "thread-b"),
84 thread(5, "thread-a"),
85 project(6, "project-a"),
86 ]).map((event) => event.sequence),
87 ).toEqual([4, 5, 6]);
88 });
89});
90
91function storedThreadEvent(
92 sequence: number,
93 threadId: string,
94 event: Record<string, unknown> = {},
95): OrchestrationV2StoredEvent {
96 return { sequence, event: { threadId, ...event } } as OrchestrationV2StoredEvent;
97}
98
99function shellFixture(overrides: Partial<OrchestrationV2ThreadShell>): OrchestrationV2ThreadShell {
100 return { id: "thread-a", archivedAt: null, ...overrides } as OrchestrationV2ThreadShell;
101}
102
103describe("coalesceStoredThreadEvents", () => {
104 it("keeps the newest stored event per thread and preserves sequence order", () => {
105 expect(
106 coalesceStoredThreadEvents([
107 storedThreadEvent(2, "thread-a"),
108 storedThreadEvent(3, "thread-b"),
109 storedThreadEvent(5, "thread-a"),
110 ]).map((stored) => stored.sequence),
111 ).toEqual([3, 5]);
112 });
113});
114
115describe("shellStreamItemFromThreadShell", () => {
116 it("emits an active thread update when the shell is not archived", () => {
117 const shell = shellFixture({ archivedAt: null });
118 expect(
119 shellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
120 ).toEqual({
121 kind: "thread.updated",
122 sequence: 4,
123 location: "active",
124 thread: shell,
125 });
126 });
127
128 it("removes archived threads from the active-only shell", () => {
129 const shell = shellFixture({ archivedAt: "2026-07-30T00:00:00.000Z" as never });
130 expect(
131 shellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
132 ).toEqual({
133 kind: "thread.removed",
134 sequence: 4,
135 location: "active",
136 threadId: "thread-a",
137 });
138 });
139
140 it("emits an active-shell removal when an archived thread is deleted", () => {
141 expect(
142 shellStreamItemFromThreadShell({
143 stored: storedThreadEvent(6, "thread-a", {
144 type: "thread.deleted",
145 payload: { archivedAt: "2026-07-30T00:00:00.000Z" },
146 }),
147 shell: null,
148 }),
149 ).toEqual({
150 kind: "thread.removed",
151 sequence: 6,
152 location: "active",
153 threadId: "thread-a",
154 });
155 });
156
157 it("emits a removal from the active list for other missing shells", () => {
158 expect(
159 shellStreamItemFromThreadShell({
160 stored: storedThreadEvent(6, "thread-a", {
161 type: "thread.deleted",
162 payload: { archivedAt: null },
163 }),
164 shell: null,
165 }),
166 ).toEqual({
167 kind: "thread.removed",
168 sequence: 6,
169 location: "active",
170 threadId: "thread-a",
171 });
172 });
173});
174
175describe("archivedShellStreamItemFromThreadShell", () => {
176 it("emits an update for an archived shell", () => {
177 const shell = shellFixture({ archivedAt: "2026-07-30T00:00:00.000Z" as never });
178 expect(
179 archivedShellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
180 ).toEqual({ kind: "thread.updated", sequence: 4, thread: shell });
181 });
182
183 it("ignores active threads that never touched the archive", () => {
184 expect(
185 archivedShellStreamItemFromThreadShell({
186 stored: storedThreadEvent(4, "thread-a", { type: "thread.settled" }),
187 shell: shellFixture({ archivedAt: null }),
188 }),
189 ).toBeNull();
190 });
191
192 it("emits a removal when a thread leaves the archive", () => {
193 expect(
194 archivedShellStreamItemFromThreadShell({
195 stored: storedThreadEvent(4, "thread-a", { type: "thread.unarchived" }),
196 shell: shellFixture({ archivedAt: null }),
197 }),
198 ).toEqual({ kind: "thread.removed", sequence: 4, threadId: "thread-a" });
199 });
200
201 it("emits a removal when an archived thread is deleted", () => {
202 expect(
203 archivedShellStreamItemFromThreadShell({
204 stored: storedThreadEvent(4, "thread-a", {
205 type: "thread.deleted",
206 payload: { archivedAt: "2026-07-30T00:00:00.000Z" },
207 }),
208 shell: null,
209 }),
210 ).toEqual({ kind: "thread.removed", sequence: 4, threadId: "thread-a" });
211 });
212});
213
214describe("shellStreamItemFromEnrichmentRefresh", () => {
215 it("batches nearby completion roots onto one snapshot item", () => {
216 expect(
217 shellStreamItemFromEnrichmentRefresh({
218 snapshot: emptyShellSnapshot,
219 changes: [
220 { workspaceRoot: "/workspace/a" },
221 { workspaceRoot: "/workspace/b" },
222 { workspaceRoot: "/workspace/a" },
223 ],
224 }),
225 ).toEqual({
226 kind: "snapshot",
227 snapshot: emptyShellSnapshot,
228 resolvedRepositoryIdentityRoots: ["/workspace/a", "/workspace/b"],
229 });
230 });
231});
232
233describe("shellStreamItemsFromInitialSnapshot", () => {
234 it("keeps thread rows out of the metadata-only enrichment frame", () => {
235 const snapshot = {
236 ...emptyShellSnapshot,
237 projects: [
238 { id: "project-a", workspaceRoot: "/workspace/a" },
239 { id: "project-b", workspaceRoot: "/workspace/b" },
240 ],
241 threads: [shellFixture({})],
242 archivedThreads: [shellFixture({ id: ThreadId.make("thread-archived"), archivedAt: null })],
243 } as unknown as OrchestrationV2ShellSnapshot;
244
245 const items = shellStreamItemsFromInitialSnapshot({
246 snapshot,
247 resolvedRepositoryIdentityRoots: ["/workspace/a"],
248 });
249
250 expect(items[0]).toEqual({ kind: "snapshot", snapshot });
251 expect(items[1]).toMatchObject({
252 kind: "snapshot",
253 snapshot: {
254 projects: [{ id: "project-a", workspaceRoot: "/workspace/a" }],
255 threads: [],
256 archivedThreads: [],
257 },
258 resolvedRepositoryIdentityRoots: ["/workspace/a"],
259 });
260 expect(JSON.stringify(items[1]).length).toBeLessThan(JSON.stringify(items[0]).length);
261 });
262
263 it("emits unmarked authoritative then same-sequence marked enrichment when roots resolved", () => {
264 const snapshot = {
265 ...emptyShellSnapshot,
266 snapshotSequence: 7,
267 } as OrchestrationV2ShellSnapshot;
268
269 expect(
270 shellStreamItemsFromInitialSnapshot({
271 snapshot,
272 resolvedRepositoryIdentityRoots: ["/workspace/a", "/workspace/a"],
273 }),
274 ).toEqual([
275 { kind: "snapshot", snapshot },
276 {
277 kind: "snapshot",
278 snapshot,
279 resolvedRepositoryIdentityRoots: ["/workspace/a"],
280 },
281 ]);
282 });
283
284 it("emits only the unmarked authoritative snapshot when no roots resolved", () => {
285 expect(
286 shellStreamItemsFromInitialSnapshot({
287 snapshot: emptyShellSnapshot,
288 resolvedRepositoryIdentityRoots: [],
289 }),
290 ).toEqual([{ kind: "snapshot", snapshot: emptyShellSnapshot }]);
291 });
292});
293
294describe("shellStreamItemsFromResumeSnapshot", () => {
295 it("never repeats the authoritative shell snapshot", () => {
296 expect(
297 shellStreamItemsFromResumeSnapshot({
298 snapshot: {
299 ...emptyShellSnapshot,
300 threads: [shellFixture({})],
301 },
302 resolvedRepositoryIdentityRoots: [],
303 }),
304 ).toEqual([]);
305 });
306});
307
308describe("composeShellStreamWithEnrichment", () => {
309 it.effect(
310 "emits every initial item before enrichment even when enrichment is already ready",
311 () =>
312 Effect.gen(function* () {
313 const initialSnapshot = {
314 ...emptyShellSnapshot,
315 snapshotSequence: 5,
316 } as OrchestrationV2ShellSnapshot;
317 const enrichmentSnapshot = {
318 ...emptyShellSnapshot,
319 snapshotSequence: 10,
320 } as OrchestrationV2ShellSnapshot;
321
322 const initialItems = shellStreamItemsFromInitialSnapshot({
323 snapshot: initialSnapshot,
324 resolvedRepositoryIdentityRoots: ["/workspace/a"],
325 });
326 // Enrichment stream is fully ready before the composed stream is pulled.
327 const enrichment = Stream.make(
328 shellStreamItemFromEnrichmentRefresh({
329 snapshot: enrichmentSnapshot,
330 changes: [{ workspaceRoot: "/workspace/b" }],
331 }),
332 );
333 const tail = Stream.make(
334 { kind: "synchronized" as const },
335 {
336 kind: "project.removed" as const,
337 sequence: 6,
338 projectId: "project-a",
339 },
340 );
341
342 const items = Array.from(
343 yield* composeShellStreamWithEnrichment({
344 initial: Stream.fromIterable(initialItems),
345 tail,
346 enrichment,
347 }).pipe(Stream.runCollect),
348 );
349
350 expect(items.slice(0, initialItems.length)).toEqual(initialItems);
351
352 const enrichmentIndex = items.findIndex(
353 (item) =>
354 item.kind === "snapshot" &&
355 "resolvedRepositoryIdentityRoots" in item &&
356 item.resolvedRepositoryIdentityRoots?.includes("/workspace/b"),
357 );
358 expect(enrichmentIndex).toBeGreaterThanOrEqual(initialItems.length);
359
360 for (let index = 0; index < initialItems.length; index++) {
361 expect(items[index]).toEqual(initialItems[index]);
362 }
363 }),
364 );
365
366 it.effect("still interleaves enrichment with the post-prefix tail after initials drain", () =>
367 Effect.gen(function* () {
368 const items = Array.from(
369 yield* composeShellStreamWithEnrichment({
370 initial: Stream.make("initial-unmarked", "initial-marked"),
371 tail: Stream.make("tail-a", "tail-b"),
372 enrichment: Stream.make("enrichment"),
373 }).pipe(Stream.runCollect),
374 );
375
376 expect(items.slice(0, 2)).toEqual(["initial-unmarked", "initial-marked"]);
377 expect(items).toContain("enrichment");
378 expect(items).toContain("tail-a");
379 expect(items).toContain("tail-b");
380 expect(items.indexOf("enrichment")).toBeGreaterThanOrEqual(2);
381 }),
382 );
383});
384
385describe("dedupeShellEnrichment", () => {
386 const project = {
387 id: ProjectId.make("project-a"),
388 title: "A project",
389 workspaceRoot: "/workspace/a",
390 repositoryIdentity: null,
391 defaultModelSelection: null,
392 scripts: [],
393 createdAt: "2026-09-01T00:00:00Z",
394 updatedAt: "2026-09-01T00:00:00Z",
395 };
396 const initial = {
397 kind: "snapshot" as const,
398 snapshot: { ...emptyShellSnapshot, projects: [project] },
399 };
400 const marked = { ...initial, resolvedRepositoryIdentityRoots: [project.workspaceRoot] };
401
402 it.effect(
403 "keeps initial/resume resolution and real changes but drops sequence-only repeats",
404 () =>
405 Effect.gen(function* () {
406 const repeated = {
407 ...marked,
408 snapshot: { ...marked.snapshot, snapshotSequence: 10, projects: [{ ...project }] },
409 };
410 const progressed = {
411 ...repeated,
412 resolvedRepositoryIdentityRoots: [project.workspaceRoot, "/workspace/b"],
413 };
414 const changed = {
415 ...progressed,
416 snapshot: { ...progressed.snapshot, projects: [{ ...project, title: "Renamed" }] },
417 };
418 const items = yield* Stream.make(
419 initial,
420 marked,
421 repeated,
422 progressed,
423 changed,
424 changed,
425 ).pipe(dedupeShellEnrichment, Stream.runCollect);
426 expect(items).toEqual([initial, marked, progressed, changed]);
427 const resumed = Stream.make(marked, repeated).pipe(dedupeShellEnrichment);
428 expect(yield* Stream.runCollect(resumed)).toEqual([marked]);
429 expect(yield* Stream.runCollect(resumed)).toEqual([marked]);
430 }),
431 );
432
433 it.effect("keeps authoritative snapshots and invalidates metadata after project deltas", () =>
434 Effect.gen(function* () {
435 const removed = { kind: "project.removed" as const, projectId: project.id, sequence: 2 };
436 const updated = { kind: "project.updated" as const, project, sequence: 3 };
437 const withThreads = {
438 ...marked,
439 snapshot: { ...marked.snapshot, threads: [shellFixture({})] },
440 };
441 const values = [
442 initial,
443 initial,
444 marked,
445 removed,
446 marked,
447 updated,
448 marked,
449 withThreads,
450 withThreads,
451 ];
452 expect(
453 yield* Stream.fromIterable(values).pipe(dedupeShellEnrichment, Stream.runCollect),
454 ).toEqual(values);
455 }),
456 );
457
458 it.effect(
459 "deduplicates across thread deltas without dropping those deltas or identity clears",
460 () =>
461 Effect.gen(function* () {
462 const delta = {
463 kind: "thread.removed" as const,
464 threadId: ThreadId.make("thread-a"),
465 location: "active" as const,
466 sequence: 2,
467 };
468 const resolved = {
469 ...marked,
470 snapshot: {
471 ...marked.snapshot,
472 projects: [
473 {
474 ...project,
475 repositoryIdentity: {
476 canonicalKey: "github.com/test/repo",
477 locator: {
478 source: "git-remote" as const,
479 remoteName: "origin",
480 remoteUrl: "https://github.com/test/repo.git",
481 },
482 },
483 },
484 ],
485 },
486 };
487 expect(
488 yield* Stream.make(marked, delta, marked, resolved, marked).pipe(
489 dedupeShellEnrichment,
490 Stream.runCollect,
491 ),
492 ).toEqual([marked, delta, resolved, marked]);
493 }),
494 );
495});