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