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