Open History to see the last commit
1import {
2 CheckpointRef,
3 EventId,
4 MessageId,
5 ProjectId,
6 ThreadId,
7 TurnId,
8 type OrchestrationEvent,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "vitest";
11
12import { deriveOrchestrationBatchEffects } from "./orchestrationEventEffects";
13
14function makeEvent<T extends OrchestrationEvent["type"]>(
15 type: T,
16 payload: Extract<OrchestrationEvent, { type: T }>["payload"],
17 overrides: Partial<Extract<OrchestrationEvent, { type: T }>> = {},
18): Extract<OrchestrationEvent, { type: T }> {
19 const sequence = overrides.sequence ?? 1;
20 return {
21 sequence,
22 eventId: EventId.makeUnsafe(`event-${sequence}`),
23 aggregateKind: "thread",
24 aggregateId:
25 "threadId" in payload
26 ? payload.threadId
27 : "projectId" in payload
28 ? payload.projectId
29 : ProjectId.makeUnsafe("project-1"),
30 occurredAt: "2026-02-27T00:00:00.000Z",
31 commandId: null,
32 causationEventId: null,
33 correlationId: null,
34 metadata: {},
35 type,
36 payload,
37 ...overrides,
38 } as Extract<OrchestrationEvent, { type: T }>;
39}
40
41describe("deriveOrchestrationBatchEffects", () => {
42 it("targets draft promotion and terminal cleanup from thread lifecycle events", () => {
43 const createdThreadId = ThreadId.makeUnsafe("thread-created");
44 const deletedThreadId = ThreadId.makeUnsafe("thread-deleted");
45 const archivedThreadId = ThreadId.makeUnsafe("thread-archived");
46
47 const effects = deriveOrchestrationBatchEffects([
48 makeEvent("thread.created", {
49 threadId: createdThreadId,
50 projectId: ProjectId.makeUnsafe("project-1"),
51 title: "Created thread",
52 modelSelection: { provider: "codex", model: "gpt-5-codex" },
53 runtimeMode: "full-access",
54 interactionMode: "default",
55 branch: null,
56 worktreePath: null,
57 createdAt: "2026-02-27T00:00:00.000Z",
58 updatedAt: "2026-02-27T00:00:00.000Z",
59 }),
60 makeEvent("thread.deleted", {
61 threadId: deletedThreadId,
62 deletedAt: "2026-02-27T00:00:01.000Z",
63 }),
64 makeEvent("thread.archived", {
65 threadId: archivedThreadId,
66 archivedAt: "2026-02-27T00:00:02.000Z",
67 updatedAt: "2026-02-27T00:00:02.000Z",
68 }),
69 ]);
70
71 expect(effects.clearPromotedDraftThreadIds).toEqual([createdThreadId]);
72 expect(effects.clearDeletedThreadIds).toEqual([deletedThreadId]);
73 expect(effects.removeTerminalStateThreadIds).toEqual([deletedThreadId, archivedThreadId]);
74 expect(effects.needsProviderInvalidation).toBe(false);
75 });
76
77 it("keeps only the final lifecycle outcome for a thread within one batch", () => {
78 const threadId = ThreadId.makeUnsafe("thread-1");
79
80 const effects = deriveOrchestrationBatchEffects([
81 makeEvent("thread.deleted", {
82 threadId,
83 deletedAt: "2026-02-27T00:00:01.000Z",
84 }),
85 makeEvent("thread.created", {
86 threadId,
87 projectId: ProjectId.makeUnsafe("project-1"),
88 title: "Recreated thread",
89 modelSelection: { provider: "codex", model: "gpt-5-codex" },
90 runtimeMode: "full-access",
91 interactionMode: "default",
92 branch: null,
93 worktreePath: null,
94 createdAt: "2026-02-27T00:00:02.000Z",
95 updatedAt: "2026-02-27T00:00:02.000Z",
96 }),
97 makeEvent("thread.turn-diff-completed", {
98 threadId,
99 turnId: TurnId.makeUnsafe("turn-1"),
100 checkpointTurnCount: 1,
101 checkpointRef: CheckpointRef.makeUnsafe("checkpoint-1"),
102 status: "ready",
103 files: [],
104 assistantMessageId: MessageId.makeUnsafe("assistant-1"),
105 completedAt: "2026-02-27T00:00:03.000Z",
106 }),
107 ]);
108
109 expect(effects.clearPromotedDraftThreadIds).toEqual([threadId]);
110 expect(effects.clearDeletedThreadIds).toEqual([]);
111 expect(effects.removeTerminalStateThreadIds).toEqual([]);
112 expect(effects.needsProviderInvalidation).toBe(true);
113 });
114
115 it("does not retain archive cleanup when a thread is unarchived later in the same batch", () => {
116 const threadId = ThreadId.makeUnsafe("thread-1");
117
118 const effects = deriveOrchestrationBatchEffects([
119 makeEvent("thread.archived", {
120 threadId,
121 archivedAt: "2026-02-27T00:00:01.000Z",
122 updatedAt: "2026-02-27T00:00:01.000Z",
123 }),
124 makeEvent("thread.unarchived", {
125 threadId,
126 updatedAt: "2026-02-27T00:00:02.000Z",
127 }),
128 ]);
129
130 expect(effects.clearPromotedDraftThreadIds).toEqual([]);
131 expect(effects.clearDeletedThreadIds).toEqual([]);
132 expect(effects.removeTerminalStateThreadIds).toEqual([]);
133 });
134});