Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 type OrchestrationV2DomainEvent,
4 type OrchestrationV2Run,
5 type OrchestrationV2ThreadProjection,
6 type OrchestrationV2TurnItem,
7 MessageId,
8 ProjectId,
9 ProviderInstanceId,
10 RunId,
11 ThreadId,
12 TurnItemId,
13} from "@t3tools/contracts";
14import * as DateTime from "effect/DateTime";
15
16import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
17
18const now = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
19const threadId = ThreadId.make("thread-reducer");
20const runId = RunId.make("run-reducer");
21const run = {
22 id: runId,
23 threadId,
24 ordinal: 1,
25 providerInstanceId: ProviderInstanceId.make("codex"),
26 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
27 providerThreadId: null,
28 userMessageId: MessageId.make("message-reducer"),
29 rootNodeId: null,
30 activeAttemptId: null,
31 status: "completed",
32 requestedAt: now,
33 startedAt: now,
34 completedAt: now,
35 checkpointId: null,
36 contextHandoffId: null,
37} satisfies OrchestrationV2Run;
38
39function commandItem(id: string, output = "done"): OrchestrationV2TurnItem {
40 return {
41 id: TurnItemId.make(id),
42 threadId,
43 runId,
44 nodeId: null,
45 providerThreadId: null,
46 providerTurnId: null,
47 nativeItemRef: null,
48 parentItemId: null,
49 ordinal: 1,
50 status: "completed",
51 title: null,
52 startedAt: now,
53 completedAt: now,
54 updatedAt: now,
55 type: "command_execution",
56 input: "pwd",
57 output,
58 exitCode: 0,
59 };
60}
61const emptyProjection = {
62 thread: {
63 id: threadId,
64 projectId: ProjectId.make("project-reducer"),
65 title: "Reducer",
66 providerInstanceId: ProviderInstanceId.make("codex"),
67 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
68 runtimeMode: "full-access",
69 interactionMode: "default",
70 branch: null,
71 worktreePath: null,
72 activeProviderThreadId: null,
73 lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
74 forkedFrom: null,
75 createdBy: "user",
76 creationSource: "web",
77 createdAt: now,
78 updatedAt: now,
79 archivedAt: null,
80 deletedAt: null,
81 },
82 runs: [],
83 attempts: [],
84 nodes: [],
85 subagents: [],
86 providerSessions: [],
87 providerThreads: [],
88 providerTurns: [],
89 runtimeRequests: [],
90 messages: [],
91 plans: [],
92 turnItems: [],
93 checkpointScopes: [],
94 checkpoints: [],
95 contextHandoffs: [],
96 contextTransfers: [],
97 visibleTurnItems: [],
98 updatedAt: now,
99} as OrchestrationV2ThreadProjection;
100
101describe("applyOrchestrationV2ProjectionEvent", () => {
102 it("applies thread lifecycle payloads instead of leaving stale metadata", () => {
103 const archivedAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
104 const event = {
105 id: "event-archive",
106 type: "thread.archived",
107 threadId,
108 occurredAt: archivedAt,
109 payload: { ...emptyProjection.thread, archivedAt, updatedAt: archivedAt },
110 } as OrchestrationV2DomainEvent;
111
112 const next = applyOrchestrationV2ProjectionEvent(emptyProjection, event);
113 expect(next?.thread.archivedAt).toEqual(archivedAt);
114 expect(next?.updatedAt).toEqual(archivedAt);
115 });
116
117 it("ignores events for another thread", () => {
118 const event = {
119 id: "event-other",
120 type: "thread.deleted",
121 threadId: ThreadId.make("thread-other"),
122 occurredAt: now,
123 payload: { ...emptyProjection.thread, id: ThreadId.make("thread-other"), deletedAt: now },
124 } as OrchestrationV2DomainEvent;
125
126 expect(applyOrchestrationV2ProjectionEvent(emptyProjection, event)).toBe(emptyProjection);
127 });
128
129 it("preserves visible row identity when run updates do not change membership", () => {
130 const item = commandItem("item-stable");
131 const visibleTurnItems = [
132 {
133 position: 0,
134 visibility: "local" as const,
135 sourceThreadId: threadId,
136 sourceItemId: item.id,
137 item,
138 },
139 ];
140 const projection = {
141 ...emptyProjection,
142 runs: [run],
143 turnItems: [item],
144 visibleTurnItems,
145 };
146 const event = {
147 id: "event-run-update",
148 type: "run.updated",
149 threadId,
150 runId,
151 occurredAt: now,
152 payload: { ...run, status: "completed" },
153 } as OrchestrationV2DomainEvent;
154
155 const next = applyOrchestrationV2ProjectionEvent(projection, event);
156 expect(next?.visibleTurnItems).toBe(visibleTurnItems);
157 expect(next?.visibleTurnItems[0]).toBe(visibleTurnItems[0]);
158 });
159
160 it("replaces only the updated visible item when membership is unchanged", () => {
161 const first = commandItem("item-first", "first");
162 const second = commandItem("item-second", "second");
163 const firstRow = {
164 position: 0,
165 visibility: "local" as const,
166 sourceThreadId: threadId,
167 sourceItemId: first.id,
168 item: first,
169 };
170 const secondRow = {
171 position: 1,
172 visibility: "local" as const,
173 sourceThreadId: threadId,
174 sourceItemId: second.id,
175 item: second,
176 };
177 const updated = commandItem("item-first", "streamed output");
178 const projection = {
179 ...emptyProjection,
180 runs: [run],
181 turnItems: [first, second],
182 visibleTurnItems: [firstRow, secondRow],
183 };
184 const event = {
185 id: "event-item-update",
186 type: "turn-item.updated",
187 threadId,
188 runId,
189 occurredAt: now,
190 payload: updated,
191 } as OrchestrationV2DomainEvent;
192
193 const next = applyOrchestrationV2ProjectionEvent(projection, event);
194 expect(next?.visibleTurnItems).not.toBe(projection.visibleTurnItems);
195 expect(next?.visibleTurnItems[0]).not.toBe(firstRow);
196 expect(next?.visibleTurnItems[0]?.item).toBe(updated);
197 expect(next?.visibleTurnItems[1]).toBe(secondRow);
198 });
199
200 it("removes only hidden local items while preserving inherited rows", () => {
201 const inherited = commandItem("item-inherited");
202 const local = commandItem("item-local");
203 const inheritedRow = {
204 position: 0,
205 visibility: "inherited" as const,
206 sourceThreadId: ThreadId.make("thread-source"),
207 sourceItemId: inherited.id,
208 item: inherited,
209 };
210 const localRow = {
211 position: 1,
212 visibility: "local" as const,
213 sourceThreadId: threadId,
214 sourceItemId: local.id,
215 item: local,
216 };
217 const projection = {
218 ...emptyProjection,
219 runs: [run],
220 turnItems: [local],
221 visibleTurnItems: [inheritedRow, localRow],
222 };
223 const event = {
224 id: "event-run-rollback",
225 type: "run.updated",
226 threadId,
227 runId,
228 occurredAt: now,
229 payload: { ...run, status: "rolled_back" },
230 } as OrchestrationV2DomainEvent;
231
232 const next = applyOrchestrationV2ProjectionEvent(projection, event);
233 expect(next?.visibleTurnItems).toEqual([inheritedRow]);
234 expect(next?.visibleTurnItems[0]).toBe(inheritedRow);
235 });
236});