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 { 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 shellStreamItemFromEnrichmentRefresh,
19 shellStreamItemFromThreadShell,
20 shellStreamItemsFromInitialSnapshot,
21 shellStreamItemsFromResumeSnapshot,
22} from "./ShellStream.ts";
23
24function project(sequence: number, id: string): ApplicationStoredEvent {
25 return {
26 sequence,
27 aggregateKind: "project",
28 aggregateId: id,
29 } as ApplicationStoredEvent;
30}
31
32function thread(sequence: number, id: string): ApplicationStoredEvent {
33 return {
34 sequence,
35 event: { threadId: id },
36 } as ApplicationStoredEvent;
37}
38
39const emptyShellSnapshot = {
40 schemaVersion: 1,
41 snapshotSequence: 0,
42 projects: [],
43 threads: [],
44 archivedThreads: [],
45} as OrchestrationV2ShellSnapshot;
46
47describe("buildActiveShellSnapshot", () => {
48 it("never duplicates archived rows into the regular shell", () => {
49 const active = shellFixture({ archivedAt: null });
50 const archived = shellFixture({
51 id: ThreadId.make("thread-archived"),
52 archivedAt: "2026-07-30T00:00:00.000Z" as never,
53 });
54
55 expect(
56 buildActiveShellSnapshot({
57 projects: [],
58 threads: {
59 schemaVersion: 1,
60 snapshotSequence: 4,
61 threads: [active],
62 archivedThreads: [archived],
63 },
64 snapshotSequence: 7,
65 }),
66 ).toEqual({
67 schemaVersion: 1,
68 snapshotSequence: 7,
69 projects: [],
70 threads: [active],
71 archivedThreads: [],
72 });
73 });
74});
75
76describe("coalesceShellApplicationEvents", () => {
77 it("keeps the newest event per aggregate and preserves sequence order", () => {
78 expect(
79 coalesceShellApplicationEvents([
80 thread(2, "thread-a"),
81 project(3, "project-a"),
82 thread(4, "thread-b"),
83 thread(5, "thread-a"),
84 project(6, "project-a"),
85 ]).map((event) => event.sequence),
86 ).toEqual([4, 5, 6]);
87 });
88});
89
90function storedThreadEvent(
91 sequence: number,
92 threadId: string,
93 event: Record<string, unknown> = {},
94): OrchestrationV2StoredEvent {
95 return { sequence, event: { threadId, ...event } } as OrchestrationV2StoredEvent;
96}
97
98function shellFixture(overrides: Partial<OrchestrationV2ThreadShell>): OrchestrationV2ThreadShell {
99 return { id: "thread-a", archivedAt: null, ...overrides } as OrchestrationV2ThreadShell;
100}
101
102describe("coalesceStoredThreadEvents", () => {
103 it("keeps the newest stored event per thread and preserves sequence order", () => {
104 expect(
105 coalesceStoredThreadEvents([
106 storedThreadEvent(2, "thread-a"),
107 storedThreadEvent(3, "thread-b"),
108 storedThreadEvent(5, "thread-a"),
109 ]).map((stored) => stored.sequence),
110 ).toEqual([3, 5]);
111 });
112});
113
114describe("shellStreamItemFromThreadShell", () => {
115 it("emits an active thread update when the shell is not archived", () => {
116 const shell = shellFixture({ archivedAt: null });
117 expect(
118 shellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
119 ).toEqual({
120 kind: "thread.updated",
121 sequence: 4,
122 location: "active",
123 thread: shell,
124 });
125 });
126
127 it("removes archived threads from the active-only shell", () => {
128 const shell = shellFixture({ archivedAt: "2026-07-30T00:00:00.000Z" as never });
129 expect(
130 shellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
131 ).toEqual({
132 kind: "thread.removed",
133 sequence: 4,
134 location: "active",
135 threadId: "thread-a",
136 });
137 });
138
139 it("emits an active-shell removal when an archived thread is deleted", () => {
140 expect(
141 shellStreamItemFromThreadShell({
142 stored: storedThreadEvent(6, "thread-a", {
143 type: "thread.deleted",
144 payload: { archivedAt: "2026-07-30T00:00:00.000Z" },
145 }),
146 shell: null,
147 }),
148 ).toEqual({
149 kind: "thread.removed",
150 sequence: 6,
151 location: "active",
152 threadId: "thread-a",
153 });
154 });
155
156 it("emits a removal from the active list for other missing shells", () => {
157 expect(
158 shellStreamItemFromThreadShell({
159 stored: storedThreadEvent(6, "thread-a", {
160 type: "thread.deleted",
161 payload: { archivedAt: null },
162 }),
163 shell: null,
164 }),
165 ).toEqual({
166 kind: "thread.removed",
167 sequence: 6,
168 location: "active",
169 threadId: "thread-a",
170 });
171 });
172});
173
174describe("archivedShellStreamItemFromThreadShell", () => {
175 it("emits an update for an archived shell", () => {
176 const shell = shellFixture({ archivedAt: "2026-07-30T00:00:00.000Z" as never });
177 expect(
178 archivedShellStreamItemFromThreadShell({ stored: storedThreadEvent(4, "thread-a"), shell }),
179 ).toEqual({ kind: "thread.updated", sequence: 4, thread: shell });
180 });
181
182 it("ignores active threads that never touched the archive", () => {
183 expect(
184 archivedShellStreamItemFromThreadShell({
185 stored: storedThreadEvent(4, "thread-a", { type: "thread.settled" }),
186 shell: shellFixture({ archivedAt: null }),
187 }),
188 ).toBeNull();
189 });
190
191 it("emits a removal when a thread leaves the archive", () => {
192 expect(
193 archivedShellStreamItemFromThreadShell({
194 stored: storedThreadEvent(4, "thread-a", { type: "thread.unarchived" }),
195 shell: shellFixture({ archivedAt: null }),
196 }),
197 ).toEqual({ kind: "thread.removed", sequence: 4, threadId: "thread-a" });
198 });
199
200 it("emits a removal when an archived thread is deleted", () => {
201 expect(
202 archivedShellStreamItemFromThreadShell({
203 stored: storedThreadEvent(4, "thread-a", {
204 type: "thread.deleted",
205 payload: { archivedAt: "2026-07-30T00:00:00.000Z" },
206 }),
207 shell: null,
208 }),
209 ).toEqual({ kind: "thread.removed", sequence: 4, threadId: "thread-a" });
210 });
211});
212
213describe("shellStreamItemFromEnrichmentRefresh", () => {
214 it("batches nearby completion roots onto one snapshot item", () => {
215 expect(
216 shellStreamItemFromEnrichmentRefresh({
217 snapshot: emptyShellSnapshot,
218 changes: [
219 { workspaceRoot: "/workspace/a" },
220 { workspaceRoot: "/workspace/b" },
221 { workspaceRoot: "/workspace/a" },
222 ],
223 }),
224 ).toEqual({
225 kind: "snapshot",
226 snapshot: emptyShellSnapshot,
227 resolvedRepositoryIdentityRoots: ["/workspace/a", "/workspace/b"],
228 });
229 });
230});
231
232describe("shellStreamItemsFromInitialSnapshot", () => {
233 it("keeps thread rows out of the metadata-only enrichment frame", () => {
234 const snapshot = {
235 ...emptyShellSnapshot,
236 projects: [
237 { id: "project-a", workspaceRoot: "/workspace/a" },
238 { id: "project-b", workspaceRoot: "/workspace/b" },
239 ],
240 threads: [shellFixture({})],
241 archivedThreads: [shellFixture({ id: ThreadId.make("thread-archived"), archivedAt: null })],
242 } as unknown as OrchestrationV2ShellSnapshot;
243
244 const items = shellStreamItemsFromInitialSnapshot({
245 snapshot,
246 resolvedRepositoryIdentityRoots: ["/workspace/a"],
247 });
248
249 expect(items[0]).toEqual({ kind: "snapshot", snapshot });
250 expect(items[1]).toMatchObject({
251 kind: "snapshot",
252 snapshot: {
253 projects: [{ id: "project-a", workspaceRoot: "/workspace/a" }],
254 threads: [],
255 archivedThreads: [],
256 },
257 resolvedRepositoryIdentityRoots: ["/workspace/a"],
258 });
259 expect(JSON.stringify(items[1]).length).toBeLessThan(JSON.stringify(items[0]).length);
260 });
261
262 it("emits unmarked authoritative then same-sequence marked enrichment when roots resolved", () => {
263 const snapshot = {
264 ...emptyShellSnapshot,
265 snapshotSequence: 7,
266 } as OrchestrationV2ShellSnapshot;
267
268 expect(
269 shellStreamItemsFromInitialSnapshot({
270 snapshot,
271 resolvedRepositoryIdentityRoots: ["/workspace/a", "/workspace/a"],
272 }),
273 ).toEqual([
274 { kind: "snapshot", snapshot },
275 {
276 kind: "snapshot",
277 snapshot,
278 resolvedRepositoryIdentityRoots: ["/workspace/a"],
279 },
280 ]);
281 });
282
283 it("emits only the unmarked authoritative snapshot when no roots resolved", () => {
284 expect(
285 shellStreamItemsFromInitialSnapshot({
286 snapshot: emptyShellSnapshot,
287 resolvedRepositoryIdentityRoots: [],
288 }),
289 ).toEqual([{ kind: "snapshot", snapshot: emptyShellSnapshot }]);
290 });
291});
292
293describe("shellStreamItemsFromResumeSnapshot", () => {
294 it("never repeats the authoritative shell snapshot", () => {
295 expect(
296 shellStreamItemsFromResumeSnapshot({
297 snapshot: {
298 ...emptyShellSnapshot,
299 threads: [shellFixture({})],
300 },
301 resolvedRepositoryIdentityRoots: [],
302 }),
303 ).toEqual([]);
304 });
305});
306
307describe("composeShellStreamWithEnrichment", () => {
308 it.effect(
309 "emits every initial item before enrichment even when enrichment is already ready",
310 () =>
311 Effect.gen(function* () {
312 const initialSnapshot = {
313 ...emptyShellSnapshot,
314 snapshotSequence: 5,
315 } as OrchestrationV2ShellSnapshot;
316 const enrichmentSnapshot = {
317 ...emptyShellSnapshot,
318 snapshotSequence: 10,
319 } as OrchestrationV2ShellSnapshot;
320
321 const initialItems = shellStreamItemsFromInitialSnapshot({
322 snapshot: initialSnapshot,
323 resolvedRepositoryIdentityRoots: ["/workspace/a"],
324 });
325 // Enrichment stream is fully ready before the composed stream is pulled.
326 const enrichment = Stream.make(
327 shellStreamItemFromEnrichmentRefresh({
328 snapshot: enrichmentSnapshot,
329 changes: [{ workspaceRoot: "/workspace/b" }],
330 }),
331 );
332 const tail = Stream.make(
333 { kind: "synchronized" as const },
334 {
335 kind: "project.removed" as const,
336 sequence: 6,
337 projectId: "project-a",
338 },
339 );
340
341 const items = Array.from(
342 yield* composeShellStreamWithEnrichment({
343 initial: Stream.fromIterable(initialItems),
344 tail,
345 enrichment,
346 }).pipe(Stream.runCollect),
347 );
348
349 expect(items.slice(0, initialItems.length)).toEqual(initialItems);
350
351 const enrichmentIndex = items.findIndex(
352 (item) =>
353 item.kind === "snapshot" &&
354 "resolvedRepositoryIdentityRoots" in item &&
355 item.resolvedRepositoryIdentityRoots?.includes("/workspace/b"),
356 );
357 expect(enrichmentIndex).toBeGreaterThanOrEqual(initialItems.length);
358
359 for (let index = 0; index < initialItems.length; index++) {
360 expect(items[index]).toEqual(initialItems[index]);
361 }
362 }),
363 );
364
365 it.effect("still interleaves enrichment with the post-prefix tail after initials drain", () =>
366 Effect.gen(function* () {
367 const items = Array.from(
368 yield* composeShellStreamWithEnrichment({
369 initial: Stream.make("initial-unmarked", "initial-marked"),
370 tail: Stream.make("tail-a", "tail-b"),
371 enrichment: Stream.make("enrichment"),
372 }).pipe(Stream.runCollect),
373 );
374
375 expect(items.slice(0, 2)).toEqual(["initial-unmarked", "initial-marked"]);
376 expect(items).toContain("enrichment");
377 expect(items).toContain("tail-a");
378 expect(items).toContain("tail-b");
379 expect(items.indexOf("enrichment")).toBeGreaterThanOrEqual(2);
380 }),
381 );
382});