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 { expect, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as NodeServices from "@effect/platform-node/NodeServices";
14
15import { decideOrchestrationCommand } from "./decider.ts";
16import { createEmptyReadModel, projectEvent } from "./projector.ts";
17
18const asEventId = (value: string): EventId => EventId.make(value);
19const asProjectId = (value: string): ProjectId => ProjectId.make(value);
20const asMessageId = (value: string): MessageId => MessageId.make(value);
21it.layer(NodeServices.layer)("decider project scripts", (it) => {
22 it.effect("emits empty scripts on project.create", () =>
23 Effect.gen(function* () {
24 const now = "2026-01-01T00:00:00.000Z";
25 const readModel = createEmptyReadModel(now);
26
27 const result = yield* 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 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
45 it.effect("propagates scripts in project.meta.update payload", () =>
46 Effect.gen(function* () {
47 const now = "2026-01-01T00:00:00.000Z";
48 const initial = createEmptyReadModel(now);
49 const readModel = yield* 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 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 = yield* decideOrchestrationCommand({
82 command: {
83 type: "project.meta.update",
84 commandId: CommandId.make("cmd-project-update-scripts"),
85 projectId: asProjectId("project-scripts"),
86 scripts: Array.from(scripts),
87 },
88 readModel,
89 });
90
91 const event = Array.isArray(result) ? result[0] : result;
92 expect(event.type).toBe("project.meta-updated");
93 expect((event.payload as { scripts?: unknown[] }).scripts).toEqual(scripts);
94 }),
95 );
96
97 it.effect("rejects project.create for an active workspace root that already exists", () =>
98 Effect.gen(function* () {
99 const now = "2026-01-01T00:00:00.000Z";
100 const initial = createEmptyReadModel(now);
101 const readModel = yield* projectEvent(initial, {
102 sequence: 1,
103 eventId: asEventId("evt-project-create"),
104 aggregateKind: "project",
105 aggregateId: asProjectId("project-existing"),
106 type: "project.created",
107 occurredAt: now,
108 commandId: CommandId.make("cmd-project-create"),
109 causationEventId: null,
110 correlationId: CommandId.make("cmd-project-create"),
111 metadata: {},
112 payload: {
113 projectId: asProjectId("project-existing"),
114 title: "Project",
115 workspaceRoot: "/tmp/project",
116 defaultModelSelection: null,
117 scripts: [],
118 createdAt: now,
119 updatedAt: now,
120 },
121 });
122
123 const failure = yield* Effect.flip(
124 decideOrchestrationCommand({
125 command: {
126 type: "project.create",
127 commandId: CommandId.make("cmd-project-create-duplicate-root"),
128 projectId: asProjectId("project-duplicate-root"),
129 title: "Duplicate Project",
130 workspaceRoot: "/tmp/project/",
131 createdAt: now,
132 },
133 readModel,
134 }),
135 );
136
137 expect(failure.message).toContain(
138 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
139 );
140 }),
141 );
142
143 it.effect("rejects project.meta.update when moving onto another active workspace root", () =>
144 Effect.gen(function* () {
145 const now = "2026-01-01T00:00:00.000Z";
146 const initial = createEmptyReadModel(now);
147 const withFirstProject = yield* projectEvent(initial, {
148 sequence: 1,
149 eventId: asEventId("evt-project-create-first"),
150 aggregateKind: "project",
151 aggregateId: asProjectId("project-first"),
152 type: "project.created",
153 occurredAt: now,
154 commandId: CommandId.make("cmd-project-create-first"),
155 causationEventId: null,
156 correlationId: CommandId.make("cmd-project-create-first"),
157 metadata: {},
158 payload: {
159 projectId: asProjectId("project-first"),
160 title: "First",
161 workspaceRoot: "/tmp/project-first",
162 defaultModelSelection: null,
163 scripts: [],
164 createdAt: now,
165 updatedAt: now,
166 },
167 });
168 const readModel = yield* projectEvent(withFirstProject, {
169 sequence: 2,
170 eventId: asEventId("evt-project-create-second"),
171 aggregateKind: "project",
172 aggregateId: asProjectId("project-second"),
173 type: "project.created",
174 occurredAt: now,
175 commandId: CommandId.make("cmd-project-create-second"),
176 causationEventId: null,
177 correlationId: CommandId.make("cmd-project-create-second"),
178 metadata: {},
179 payload: {
180 projectId: asProjectId("project-second"),
181 title: "Second",
182 workspaceRoot: "/tmp/project-second",
183 defaultModelSelection: null,
184 scripts: [],
185 createdAt: now,
186 updatedAt: now,
187 },
188 });
189
190 const failure = yield* Effect.flip(
191 decideOrchestrationCommand({
192 command: {
193 type: "project.meta.update",
194 commandId: CommandId.make("cmd-project-update-duplicate-root"),
195 projectId: asProjectId("project-second"),
196 workspaceRoot: "/tmp/project-first",
197 },
198 readModel,
199 }),
200 );
201
202 expect(failure.message).toContain(
203 "Active project 'project-first' already exists for workspace root '/tmp/project-first'.",
204 );
205 }),
206 );
207
208 it.effect("emits user message and turn-start-requested events for thread.turn.start", () =>
209 Effect.gen(function* () {
210 const now = "2026-01-01T00:00:00.000Z";
211 const initial = createEmptyReadModel(now);
212 const withProject = yield* projectEvent(initial, {
213 sequence: 1,
214 eventId: asEventId("evt-project-create"),
215 aggregateKind: "project",
216 aggregateId: asProjectId("project-1"),
217 type: "project.created",
218 occurredAt: now,
219 commandId: CommandId.make("cmd-project-create"),
220 causationEventId: null,
221 correlationId: CommandId.make("cmd-project-create"),
222 metadata: {},
223 payload: {
224 projectId: asProjectId("project-1"),
225 title: "Project",
226 workspaceRoot: "/tmp/project",
227 defaultModelSelection: null,
228 scripts: [],
229 createdAt: now,
230 updatedAt: now,
231 },
232 });
233 const readModel = yield* projectEvent(withProject, {
234 sequence: 2,
235 eventId: asEventId("evt-thread-create"),
236 aggregateKind: "thread",
237 aggregateId: ThreadId.make("thread-1"),
238 type: "thread.created",
239 occurredAt: now,
240 commandId: CommandId.make("cmd-thread-create"),
241 causationEventId: null,
242 correlationId: CommandId.make("cmd-thread-create"),
243 metadata: {},
244 payload: {
245 threadId: ThreadId.make("thread-1"),
246 projectId: asProjectId("project-1"),
247 title: "Thread",
248 modelSelection: {
249 instanceId: ProviderInstanceId.make("codex"),
250 model: "gpt-5-codex",
251 },
252 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
253 runtimeMode: "approval-required",
254 branch: null,
255 worktreePath: null,
256 createdAt: now,
257 updatedAt: now,
258 },
259 });
260
261 const result = yield* decideOrchestrationCommand({
262 command: {
263 type: "thread.turn.start",
264 commandId: CommandId.make("cmd-turn-start"),
265 threadId: ThreadId.make("thread-1"),
266 message: {
267 messageId: asMessageId("message-user-1"),
268 role: "user",
269 text: "hello",
270 attachments: [],
271 },
272 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
273 { id: "reasoningEffort", value: "high" },
274 { id: "fastMode", value: true },
275 ]),
276 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
277 runtimeMode: "approval-required",
278 createdAt: now,
279 },
280 readModel,
281 });
282
283 expect(Array.isArray(result)).toBe(true);
284 const events = Array.isArray(result) ? result : [result];
285 expect(events).toHaveLength(2);
286 expect(events[0]?.type).toBe("thread.message-sent");
287 const turnStartEvent = events[1];
288 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
289 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
290 if (turnStartEvent?.type !== "thread.turn-start-requested") {
291 return;
292 }
293 expect(turnStartEvent.payload).toMatchObject({
294 threadId: ThreadId.make("thread-1"),
295 messageId: asMessageId("message-user-1"),
296 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
297 { id: "reasoningEffort", value: "high" },
298 { id: "fastMode", value: true },
299 ]),
300 runtimeMode: "approval-required",
301 });
302 }),
303 );
304
305 it.effect("emits thread.runtime-mode-set from thread.runtime-mode.set", () =>
306 Effect.gen(function* () {
307 const now = "2026-01-01T00:00:00.000Z";
308 const initial = createEmptyReadModel(now);
309 const withProject = yield* projectEvent(initial, {
310 sequence: 1,
311 eventId: asEventId("evt-project-create"),
312 aggregateKind: "project",
313 aggregateId: asProjectId("project-1"),
314 type: "project.created",
315 occurredAt: now,
316 commandId: CommandId.make("cmd-project-create"),
317 causationEventId: null,
318 correlationId: CommandId.make("cmd-project-create"),
319 metadata: {},
320 payload: {
321 projectId: asProjectId("project-1"),
322 title: "Project",
323 workspaceRoot: "/tmp/project",
324 defaultModelSelection: null,
325 scripts: [],
326 createdAt: now,
327 updatedAt: now,
328 },
329 });
330 const readModel = yield* projectEvent(withProject, {
331 sequence: 2,
332 eventId: asEventId("evt-thread-create"),
333 aggregateKind: "thread",
334 aggregateId: ThreadId.make("thread-1"),
335 type: "thread.created",
336 occurredAt: now,
337 commandId: CommandId.make("cmd-thread-create"),
338 causationEventId: null,
339 correlationId: CommandId.make("cmd-thread-create"),
340 metadata: {},
341 payload: {
342 threadId: ThreadId.make("thread-1"),
343 projectId: asProjectId("project-1"),
344 title: "Thread",
345 modelSelection: {
346 instanceId: ProviderInstanceId.make("codex"),
347 model: "gpt-5-codex",
348 },
349 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
350 runtimeMode: "full-access",
351 branch: null,
352 worktreePath: null,
353 createdAt: now,
354 updatedAt: now,
355 },
356 });
357
358 const result = yield* decideOrchestrationCommand({
359 command: {
360 type: "thread.runtime-mode.set",
361 commandId: CommandId.make("cmd-runtime-mode-set"),
362 threadId: ThreadId.make("thread-1"),
363 runtimeMode: "approval-required",
364 createdAt: now,
365 },
366 readModel,
367 });
368
369 const singleResult = Array.isArray(result) ? null : result;
370 if (singleResult === null) {
371 throw new Error("Expected a single runtime-mode-set event.");
372 }
373 expect(singleResult).toMatchObject({
374 type: "thread.runtime-mode-set",
375 payload: {
376 threadId: ThreadId.make("thread-1"),
377 runtimeMode: "approval-required",
378 },
379 });
380 }),
381 );
382
383 it.effect("emits thread.interaction-mode-set from thread.interaction-mode.set", () =>
384 Effect.gen(function* () {
385 const now = "2026-01-01T00:00:00.000Z";
386 const initial = createEmptyReadModel(now);
387 const withProject = yield* projectEvent(initial, {
388 sequence: 1,
389 eventId: asEventId("evt-project-create"),
390 aggregateKind: "project",
391 aggregateId: asProjectId("project-1"),
392 type: "project.created",
393 occurredAt: now,
394 commandId: CommandId.make("cmd-project-create"),
395 causationEventId: null,
396 correlationId: CommandId.make("cmd-project-create"),
397 metadata: {},
398 payload: {
399 projectId: asProjectId("project-1"),
400 title: "Project",
401 workspaceRoot: "/tmp/project",
402 defaultModelSelection: null,
403 scripts: [],
404 createdAt: now,
405 updatedAt: now,
406 },
407 });
408 const readModel = yield* projectEvent(withProject, {
409 sequence: 2,
410 eventId: asEventId("evt-thread-create"),
411 aggregateKind: "thread",
412 aggregateId: ThreadId.make("thread-1"),
413 type: "thread.created",
414 occurredAt: now,
415 commandId: CommandId.make("cmd-thread-create"),
416 causationEventId: null,
417 correlationId: CommandId.make("cmd-thread-create"),
418 metadata: {},
419 payload: {
420 threadId: ThreadId.make("thread-1"),
421 projectId: asProjectId("project-1"),
422 title: "Thread",
423 modelSelection: {
424 instanceId: ProviderInstanceId.make("codex"),
425 model: "gpt-5-codex",
426 },
427 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
428 runtimeMode: "approval-required",
429 branch: null,
430 worktreePath: null,
431 createdAt: now,
432 updatedAt: now,
433 },
434 });
435
436 const result = yield* decideOrchestrationCommand({
437 command: {
438 type: "thread.interaction-mode.set",
439 commandId: CommandId.make("cmd-interaction-mode-set"),
440 threadId: ThreadId.make("thread-1"),
441 interactionMode: "plan",
442 createdAt: now,
443 },
444 readModel,
445 });
446
447 const singleResult = Array.isArray(result) ? null : result;
448 if (singleResult === null) {
449 throw new Error("Expected a single interaction-mode-set event.");
450 }
451 expect(singleResult).toMatchObject({
452 type: "thread.interaction-mode-set",
453 payload: {
454 threadId: ThreadId.make("thread-1"),
455 interactionMode: "plan",
456 },
457 });
458 }),
459 );
460});