Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationEventEffects.test.ts

135 lines4.5 KB

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