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