Open History to see the last commit
1import {
2 CommandId,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 EventId,
5 MessageId,
6 ProjectId,
7 ThreadId,
8} from "@t3tools/contracts";
9import { createModelSelection } from "@t3tools/shared/model";
10import { describe, expect, it } from "vitest";
11import { Effect } from "effect";
12
13import { decideOrchestrationCommand } from "./decider.ts";
14import { createEmptyReadModel, projectEvent } from "./projector.ts";
15
16const asEventId = (value: string): EventId => EventId.make(value);
17const asProjectId = (value: string): ProjectId => ProjectId.make(value);
18const asMessageId = (value: string): MessageId => MessageId.make(value);
19
20describe("decider project scripts", () => {
21 it("emits empty scripts on project.create", async () => {
22 const now = new Date().toISOString();
23 const readModel = createEmptyReadModel(now);
24
25 const result = await Effect.runPromise(
26 decideOrchestrationCommand({
27 command: {
28 type: "project.create",
29 commandId: CommandId.make("cmd-project-create-scripts"),
30 projectId: asProjectId("project-scripts"),
31 title: "Scripts",
32 workspaceRoot: "/tmp/scripts",
33 createdAt: now,
34 },
35 readModel,
36 }),
37 );
38
39 const event = Array.isArray(result) ? result[0] : result;
40 expect(event.type).toBe("project.created");
41 expect((event.payload as { scripts: unknown[] }).scripts).toEqual([]);
42 });
43
44 it("propagates scripts in project.meta.update payload", async () => {
45 const now = new Date().toISOString();
46 const initial = createEmptyReadModel(now);
47 const readModel = await Effect.runPromise(
48 projectEvent(initial, {
49 sequence: 1,
50 eventId: asEventId("evt-project-create-scripts"),
51 aggregateKind: "project",
52 aggregateId: asProjectId("project-scripts"),
53 type: "project.created",
54 occurredAt: now,
55 commandId: CommandId.make("cmd-project-create-scripts"),
56 causationEventId: null,
57 correlationId: CommandId.make("cmd-project-create-scripts"),
58 metadata: {},
59 payload: {
60 projectId: asProjectId("project-scripts"),
61 title: "Scripts",
62 workspaceRoot: "/tmp/scripts",
63 defaultModelSelection: null,
64 scripts: [],
65 createdAt: now,
66 updatedAt: now,
67 },
68 }),
69 );
70
71 const scripts = [
72 {
73 id: "lint",
74 name: "Lint",
75 command: "bun run lint",
76 icon: "lint",
77 runOnWorktreeCreate: false,
78 },
79 ] as const;
80
81 const result = await Effect.runPromise(
82 decideOrchestrationCommand({
83 command: {
84 type: "project.meta.update",
85 commandId: CommandId.make("cmd-project-update-scripts"),
86 projectId: asProjectId("project-scripts"),
87 scripts: Array.from(scripts),
88 },
89 readModel,
90 }),
91 );
92
93 const event = Array.isArray(result) ? result[0] : result;
94 expect(event.type).toBe("project.meta-updated");
95 expect((event.payload as { scripts?: unknown[] }).scripts).toEqual(scripts);
96 });
97
98 it("emits user message and turn-start-requested events for thread.turn.start", async () => {
99 const now = new Date().toISOString();
100 const initial = createEmptyReadModel(now);
101 const withProject = await Effect.runPromise(
102 projectEvent(initial, {
103 sequence: 1,
104 eventId: asEventId("evt-project-create"),
105 aggregateKind: "project",
106 aggregateId: asProjectId("project-1"),
107 type: "project.created",
108 occurredAt: now,
109 commandId: CommandId.make("cmd-project-create"),
110 causationEventId: null,
111 correlationId: CommandId.make("cmd-project-create"),
112 metadata: {},
113 payload: {
114 projectId: asProjectId("project-1"),
115 title: "Project",
116 workspaceRoot: "/tmp/project",
117 defaultModelSelection: null,
118 scripts: [],
119 createdAt: now,
120 updatedAt: now,
121 },
122 }),
123 );
124 const readModel = await Effect.runPromise(
125 projectEvent(withProject, {
126 sequence: 2,
127 eventId: asEventId("evt-thread-create"),
128 aggregateKind: "thread",
129 aggregateId: ThreadId.make("thread-1"),
130 type: "thread.created",
131 occurredAt: now,
132 commandId: CommandId.make("cmd-thread-create"),
133 causationEventId: null,
134 correlationId: CommandId.make("cmd-thread-create"),
135 metadata: {},
136 payload: {
137 threadId: ThreadId.make("thread-1"),
138 projectId: asProjectId("project-1"),
139 title: "Thread",
140 modelSelection: {
141 provider: "codex",
142 model: "gpt-5-codex",
143 },
144 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
145 runtimeMode: "approval-required",
146 branch: null,
147 worktreePath: null,
148 createdAt: now,
149 updatedAt: now,
150 },
151 }),
152 );
153
154 const result = await Effect.runPromise(
155 decideOrchestrationCommand({
156 command: {
157 type: "thread.turn.start",
158 commandId: CommandId.make("cmd-turn-start"),
159 threadId: ThreadId.make("thread-1"),
160 message: {
161 messageId: asMessageId("message-user-1"),
162 role: "user",
163 text: "hello",
164 attachments: [],
165 },
166 modelSelection: createModelSelection("codex", "gpt-5.3-codex", [
167 { id: "reasoningEffort", value: "high" },
168 { id: "fastMode", value: true },
169 ]),
170 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
171 runtimeMode: "approval-required",
172 createdAt: now,
173 },
174 readModel,
175 }),
176 );
177
178 expect(Array.isArray(result)).toBe(true);
179 const events = Array.isArray(result) ? result : [result];
180 expect(events).toHaveLength(2);
181 expect(events[0]?.type).toBe("thread.message-sent");
182 const turnStartEvent = events[1];
183 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
184 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
185 if (turnStartEvent?.type !== "thread.turn-start-requested") {
186 return;
187 }
188 expect(turnStartEvent.payload).toMatchObject({
189 threadId: ThreadId.make("thread-1"),
190 messageId: asMessageId("message-user-1"),
191 modelSelection: createModelSelection("codex", "gpt-5.3-codex", [
192 { id: "reasoningEffort", value: "high" },
193 { id: "fastMode", value: true },
194 ]),
195 runtimeMode: "approval-required",
196 });
197 });
198
199 it("emits thread.runtime-mode-set from thread.runtime-mode.set", async () => {
200 const now = new Date().toISOString();
201 const initial = createEmptyReadModel(now);
202 const withProject = await Effect.runPromise(
203 projectEvent(initial, {
204 sequence: 1,
205 eventId: asEventId("evt-project-create"),
206 aggregateKind: "project",
207 aggregateId: asProjectId("project-1"),
208 type: "project.created",
209 occurredAt: now,
210 commandId: CommandId.make("cmd-project-create"),
211 causationEventId: null,
212 correlationId: CommandId.make("cmd-project-create"),
213 metadata: {},
214 payload: {
215 projectId: asProjectId("project-1"),
216 title: "Project",
217 workspaceRoot: "/tmp/project",
218 defaultModelSelection: null,
219 scripts: [],
220 createdAt: now,
221 updatedAt: now,
222 },
223 }),
224 );
225 const readModel = await Effect.runPromise(
226 projectEvent(withProject, {
227 sequence: 2,
228 eventId: asEventId("evt-thread-create"),
229 aggregateKind: "thread",
230 aggregateId: ThreadId.make("thread-1"),
231 type: "thread.created",
232 occurredAt: now,
233 commandId: CommandId.make("cmd-thread-create"),
234 causationEventId: null,
235 correlationId: CommandId.make("cmd-thread-create"),
236 metadata: {},
237 payload: {
238 threadId: ThreadId.make("thread-1"),
239 projectId: asProjectId("project-1"),
240 title: "Thread",
241 modelSelection: {
242 provider: "codex",
243 model: "gpt-5-codex",
244 },
245 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
246 runtimeMode: "full-access",
247 branch: null,
248 worktreePath: null,
249 createdAt: now,
250 updatedAt: now,
251 },
252 }),
253 );
254
255 const result = await Effect.runPromise(
256 decideOrchestrationCommand({
257 command: {
258 type: "thread.runtime-mode.set",
259 commandId: CommandId.make("cmd-runtime-mode-set"),
260 threadId: ThreadId.make("thread-1"),
261 runtimeMode: "approval-required",
262 createdAt: now,
263 },
264 readModel,
265 }),
266 );
267
268 const singleResult = Array.isArray(result) ? null : result;
269 if (singleResult === null) {
270 throw new Error("Expected a single runtime-mode-set event.");
271 }
272 expect(singleResult).toMatchObject({
273 type: "thread.runtime-mode-set",
274 payload: {
275 threadId: ThreadId.make("thread-1"),
276 runtimeMode: "approval-required",
277 },
278 });
279 });
280
281 it("emits thread.interaction-mode-set from thread.interaction-mode.set", async () => {
282 const now = new Date().toISOString();
283 const initial = createEmptyReadModel(now);
284 const withProject = await Effect.runPromise(
285 projectEvent(initial, {
286 sequence: 1,
287 eventId: asEventId("evt-project-create"),
288 aggregateKind: "project",
289 aggregateId: asProjectId("project-1"),
290 type: "project.created",
291 occurredAt: now,
292 commandId: CommandId.make("cmd-project-create"),
293 causationEventId: null,
294 correlationId: CommandId.make("cmd-project-create"),
295 metadata: {},
296 payload: {
297 projectId: asProjectId("project-1"),
298 title: "Project",
299 workspaceRoot: "/tmp/project",
300 defaultModelSelection: null,
301 scripts: [],
302 createdAt: now,
303 updatedAt: now,
304 },
305 }),
306 );
307 const readModel = await Effect.runPromise(
308 projectEvent(withProject, {
309 sequence: 2,
310 eventId: asEventId("evt-thread-create"),
311 aggregateKind: "thread",
312 aggregateId: ThreadId.make("thread-1"),
313 type: "thread.created",
314 occurredAt: now,
315 commandId: CommandId.make("cmd-thread-create"),
316 causationEventId: null,
317 correlationId: CommandId.make("cmd-thread-create"),
318 metadata: {},
319 payload: {
320 threadId: ThreadId.make("thread-1"),
321 projectId: asProjectId("project-1"),
322 title: "Thread",
323 modelSelection: {
324 provider: "codex",
325 model: "gpt-5-codex",
326 },
327 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
328 runtimeMode: "approval-required",
329 branch: null,
330 worktreePath: null,
331 createdAt: now,
332 updatedAt: now,
333 },
334 }),
335 );
336
337 const result = await Effect.runPromise(
338 decideOrchestrationCommand({
339 command: {
340 type: "thread.interaction-mode.set",
341 commandId: CommandId.make("cmd-interaction-mode-set"),
342 threadId: ThreadId.make("thread-1"),
343 interactionMode: "plan",
344 createdAt: now,
345 },
346 readModel,
347 }),
348 );
349
350 const singleResult = Array.isArray(result) ? null : result;
351 if (singleResult === null) {
352 throw new Error("Expected a single interaction-mode-set event.");
353 }
354 expect(singleResult).toMatchObject({
355 type: "thread.interaction-mode-set",
356 payload: {
357 threadId: ThreadId.make("thread-1"),
358 interactionMode: "plan",
359 },
360 });
361 });
362});