Open History to see the last commit
1import {
2 CommandId,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 EventId,
5 ProjectId,
6 ThreadId,
7 type OrchestrationCommand,
8 type OrchestrationEvent,
9 type OrchestrationReadModel,
10} from "@t3tools/contracts";
11import { Effect } from "effect";
12import { describe, expect, it } from "vitest";
13
14import { decideOrchestrationCommand } from "./decider.ts";
15import { createEmptyReadModel, projectEvent } from "./projector.ts";
16
17const asCommandId = (value: string): CommandId => CommandId.make(value);
18const asEventId = (value: string): EventId => EventId.make(value);
19const asProjectId = (value: string): ProjectId => ProjectId.make(value);
20const asThreadId = (value: string): ThreadId => ThreadId.make(value);
21
22async function seedReadModel(): Promise<OrchestrationReadModel> {
23 const now = new Date().toISOString();
24 const initial = createEmptyReadModel(now);
25 const withProject = await Effect.runPromise(
26 projectEvent(initial, {
27 sequence: 1,
28 eventId: asEventId("evt-project-create"),
29 aggregateKind: "project",
30 aggregateId: asProjectId("project-delete"),
31 type: "project.created",
32 occurredAt: now,
33 commandId: asCommandId("cmd-project-create"),
34 causationEventId: null,
35 correlationId: asCommandId("cmd-project-create"),
36 metadata: {},
37 payload: {
38 projectId: asProjectId("project-delete"),
39 title: "Project Delete",
40 workspaceRoot: "/tmp/project-delete",
41 defaultModelSelection: null,
42 scripts: [],
43 createdAt: now,
44 updatedAt: now,
45 },
46 }),
47 );
48
49 const withFirstThread = await Effect.runPromise(
50 projectEvent(withProject, {
51 sequence: 2,
52 eventId: asEventId("evt-thread-create-1"),
53 aggregateKind: "thread",
54 aggregateId: asThreadId("thread-delete-1"),
55 type: "thread.created",
56 occurredAt: now,
57 commandId: asCommandId("cmd-thread-create-1"),
58 causationEventId: null,
59 correlationId: asCommandId("cmd-thread-create-1"),
60 metadata: {},
61 payload: {
62 threadId: asThreadId("thread-delete-1"),
63 projectId: asProjectId("project-delete"),
64 title: "Thread Delete 1",
65 modelSelection: {
66 provider: "codex",
67 model: "gpt-5-codex",
68 },
69 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
70 runtimeMode: "approval-required",
71 branch: null,
72 worktreePath: null,
73 createdAt: now,
74 updatedAt: now,
75 },
76 }),
77 );
78
79 return Effect.runPromise(
80 projectEvent(withFirstThread, {
81 sequence: 3,
82 eventId: asEventId("evt-thread-create-2"),
83 aggregateKind: "thread",
84 aggregateId: asThreadId("thread-delete-2"),
85 type: "thread.created",
86 occurredAt: now,
87 commandId: asCommandId("cmd-thread-create-2"),
88 causationEventId: null,
89 correlationId: asCommandId("cmd-thread-create-2"),
90 metadata: {},
91 payload: {
92 threadId: asThreadId("thread-delete-2"),
93 projectId: asProjectId("project-delete"),
94 title: "Thread Delete 2",
95 modelSelection: {
96 provider: "codex",
97 model: "gpt-5-codex",
98 },
99 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
100 runtimeMode: "approval-required",
101 branch: null,
102 worktreePath: null,
103 createdAt: now,
104 updatedAt: now,
105 },
106 }),
107 );
108}
109
110type PlannedEvent = Omit<OrchestrationEvent, "sequence">;
111
112function normalizeDeleteEvent(event: PlannedEvent | ReadonlyArray<PlannedEvent>) {
113 const events = Array.isArray(event) ? event : [event];
114 return events.map((entry) => {
115 switch (entry.type) {
116 case "thread.deleted":
117 return {
118 type: entry.type,
119 aggregateKind: entry.aggregateKind,
120 aggregateId: entry.aggregateId,
121 commandId: entry.commandId,
122 correlationId: entry.correlationId,
123 payload: {
124 threadId: entry.payload.threadId,
125 },
126 };
127 case "project.deleted":
128 return {
129 type: entry.type,
130 aggregateKind: entry.aggregateKind,
131 aggregateId: entry.aggregateId,
132 commandId: entry.commandId,
133 correlationId: entry.correlationId,
134 payload: {
135 projectId: entry.payload.projectId,
136 },
137 };
138 default:
139 return entry;
140 }
141 });
142}
143
144describe("decider deletion flows", () => {
145 it("rejects deleting a non-empty project without force", async () => {
146 const readModel = await seedReadModel();
147
148 await expect(
149 Effect.runPromise(
150 decideOrchestrationCommand({
151 command: {
152 type: "project.delete",
153 commandId: asCommandId("cmd-project-delete-no-force"),
154 projectId: asProjectId("project-delete"),
155 },
156 readModel,
157 }),
158 ),
159 ).rejects.toThrow("cannot be deleted without force=true");
160 });
161
162 it("reuses thread.delete semantics when force-deleting a non-empty project", async () => {
163 const readModel = await seedReadModel();
164 const projectDeleteCommand: Extract<OrchestrationCommand, { type: "project.delete" }> = {
165 type: "project.delete",
166 commandId: asCommandId("cmd-project-delete-force"),
167 projectId: asProjectId("project-delete"),
168 force: true,
169 };
170
171 const forcedResult = await Effect.runPromise(
172 decideOrchestrationCommand({
173 command: projectDeleteCommand,
174 readModel,
175 }),
176 );
177 const forcedEvents = Array.isArray(forcedResult) ? forcedResult : [forcedResult];
178
179 expect(forcedEvents.map((event) => event.type)).toEqual([
180 "thread.deleted",
181 "thread.deleted",
182 "project.deleted",
183 ]);
184
185 let sequentialReadModel = readModel;
186 let nextSequence = readModel.snapshotSequence;
187 const sequentialEvents: PlannedEvent[] = [];
188 for (const nextCommand of [
189 {
190 type: "thread.delete",
191 commandId: projectDeleteCommand.commandId,
192 threadId: asThreadId("thread-delete-1"),
193 },
194 {
195 type: "thread.delete",
196 commandId: projectDeleteCommand.commandId,
197 threadId: asThreadId("thread-delete-2"),
198 },
199 {
200 type: "project.delete",
201 commandId: projectDeleteCommand.commandId,
202 projectId: asProjectId("project-delete"),
203 },
204 ] satisfies ReadonlyArray<OrchestrationCommand>) {
205 const decided = await Effect.runPromise(
206 decideOrchestrationCommand({
207 command: nextCommand,
208 readModel: sequentialReadModel,
209 }),
210 );
211 const nextEvents = Array.isArray(decided) ? decided : [decided];
212 sequentialEvents.push(...nextEvents);
213 for (const nextEvent of nextEvents) {
214 nextSequence += 1;
215 sequentialReadModel = await Effect.runPromise(
216 projectEvent(sequentialReadModel, {
217 ...nextEvent,
218 sequence: nextSequence,
219 }),
220 );
221 }
222 }
223
224 expect(normalizeDeleteEvent(forcedResult)).toEqual(normalizeDeleteEvent(sequentialEvents));
225 });
226});