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("emits user message and turn-start-requested events for thread.turn.start", () =>
98 Effect.gen(function* () {
99 const now = "2026-01-01T00:00:00.000Z";
100 const initial = createEmptyReadModel(now);
101 const withProject = yield* projectEvent(initial, {
102 sequence: 1,
103 eventId: asEventId("evt-project-create"),
104 aggregateKind: "project",
105 aggregateId: asProjectId("project-1"),
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-1"),
114 title: "Project",
115 workspaceRoot: "/tmp/project",
116 defaultModelSelection: null,
117 scripts: [],
118 createdAt: now,
119 updatedAt: now,
120 },
121 });
122 const readModel = yield* projectEvent(withProject, {
123 sequence: 2,
124 eventId: asEventId("evt-thread-create"),
125 aggregateKind: "thread",
126 aggregateId: ThreadId.make("thread-1"),
127 type: "thread.created",
128 occurredAt: now,
129 commandId: CommandId.make("cmd-thread-create"),
130 causationEventId: null,
131 correlationId: CommandId.make("cmd-thread-create"),
132 metadata: {},
133 payload: {
134 threadId: ThreadId.make("thread-1"),
135 projectId: asProjectId("project-1"),
136 title: "Thread",
137 modelSelection: {
138 instanceId: ProviderInstanceId.make("codex"),
139 model: "gpt-5-codex",
140 },
141 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
142 runtimeMode: "approval-required",
143 branch: null,
144 worktreePath: null,
145 createdAt: now,
146 updatedAt: now,
147 },
148 });
149
150 const result = yield* decideOrchestrationCommand({
151 command: {
152 type: "thread.turn.start",
153 commandId: CommandId.make("cmd-turn-start"),
154 threadId: ThreadId.make("thread-1"),
155 message: {
156 messageId: asMessageId("message-user-1"),
157 role: "user",
158 text: "hello",
159 attachments: [],
160 },
161 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
162 { id: "reasoningEffort", value: "high" },
163 { id: "fastMode", value: true },
164 ]),
165 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
166 runtimeMode: "approval-required",
167 createdAt: now,
168 },
169 readModel,
170 });
171
172 expect(Array.isArray(result)).toBe(true);
173 const events = Array.isArray(result) ? result : [result];
174 expect(events).toHaveLength(2);
175 expect(events[0]?.type).toBe("thread.message-sent");
176 const turnStartEvent = events[1];
177 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
178 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
179 if (turnStartEvent?.type !== "thread.turn-start-requested") {
180 return;
181 }
182 expect(turnStartEvent.payload).toMatchObject({
183 threadId: ThreadId.make("thread-1"),
184 messageId: asMessageId("message-user-1"),
185 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
186 { id: "reasoningEffort", value: "high" },
187 { id: "fastMode", value: true },
188 ]),
189 runtimeMode: "approval-required",
190 });
191 }),
192 );
193
194 it.effect("emits thread.runtime-mode-set from thread.runtime-mode.set", () =>
195 Effect.gen(function* () {
196 const now = "2026-01-01T00:00:00.000Z";
197 const initial = createEmptyReadModel(now);
198 const withProject = yield* projectEvent(initial, {
199 sequence: 1,
200 eventId: asEventId("evt-project-create"),
201 aggregateKind: "project",
202 aggregateId: asProjectId("project-1"),
203 type: "project.created",
204 occurredAt: now,
205 commandId: CommandId.make("cmd-project-create"),
206 causationEventId: null,
207 correlationId: CommandId.make("cmd-project-create"),
208 metadata: {},
209 payload: {
210 projectId: asProjectId("project-1"),
211 title: "Project",
212 workspaceRoot: "/tmp/project",
213 defaultModelSelection: null,
214 scripts: [],
215 createdAt: now,
216 updatedAt: now,
217 },
218 });
219 const readModel = yield* projectEvent(withProject, {
220 sequence: 2,
221 eventId: asEventId("evt-thread-create"),
222 aggregateKind: "thread",
223 aggregateId: ThreadId.make("thread-1"),
224 type: "thread.created",
225 occurredAt: now,
226 commandId: CommandId.make("cmd-thread-create"),
227 causationEventId: null,
228 correlationId: CommandId.make("cmd-thread-create"),
229 metadata: {},
230 payload: {
231 threadId: ThreadId.make("thread-1"),
232 projectId: asProjectId("project-1"),
233 title: "Thread",
234 modelSelection: {
235 instanceId: ProviderInstanceId.make("codex"),
236 model: "gpt-5-codex",
237 },
238 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
239 runtimeMode: "full-access",
240 branch: null,
241 worktreePath: null,
242 createdAt: now,
243 updatedAt: now,
244 },
245 });
246
247 const result = yield* decideOrchestrationCommand({
248 command: {
249 type: "thread.runtime-mode.set",
250 commandId: CommandId.make("cmd-runtime-mode-set"),
251 threadId: ThreadId.make("thread-1"),
252 runtimeMode: "approval-required",
253 createdAt: now,
254 },
255 readModel,
256 });
257
258 const singleResult = Array.isArray(result) ? null : result;
259 if (singleResult === null) {
260 throw new Error("Expected a single runtime-mode-set event.");
261 }
262 expect(singleResult).toMatchObject({
263 type: "thread.runtime-mode-set",
264 payload: {
265 threadId: ThreadId.make("thread-1"),
266 runtimeMode: "approval-required",
267 },
268 });
269 }),
270 );
271
272 it.effect("emits thread.interaction-mode-set from thread.interaction-mode.set", () =>
273 Effect.gen(function* () {
274 const now = "2026-01-01T00:00:00.000Z";
275 const initial = createEmptyReadModel(now);
276 const withProject = yield* projectEvent(initial, {
277 sequence: 1,
278 eventId: asEventId("evt-project-create"),
279 aggregateKind: "project",
280 aggregateId: asProjectId("project-1"),
281 type: "project.created",
282 occurredAt: now,
283 commandId: CommandId.make("cmd-project-create"),
284 causationEventId: null,
285 correlationId: CommandId.make("cmd-project-create"),
286 metadata: {},
287 payload: {
288 projectId: asProjectId("project-1"),
289 title: "Project",
290 workspaceRoot: "/tmp/project",
291 defaultModelSelection: null,
292 scripts: [],
293 createdAt: now,
294 updatedAt: now,
295 },
296 });
297 const readModel = yield* projectEvent(withProject, {
298 sequence: 2,
299 eventId: asEventId("evt-thread-create"),
300 aggregateKind: "thread",
301 aggregateId: ThreadId.make("thread-1"),
302 type: "thread.created",
303 occurredAt: now,
304 commandId: CommandId.make("cmd-thread-create"),
305 causationEventId: null,
306 correlationId: CommandId.make("cmd-thread-create"),
307 metadata: {},
308 payload: {
309 threadId: ThreadId.make("thread-1"),
310 projectId: asProjectId("project-1"),
311 title: "Thread",
312 modelSelection: {
313 instanceId: ProviderInstanceId.make("codex"),
314 model: "gpt-5-codex",
315 },
316 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
317 runtimeMode: "approval-required",
318 branch: null,
319 worktreePath: null,
320 createdAt: now,
321 updatedAt: now,
322 },
323 });
324
325 const result = yield* decideOrchestrationCommand({
326 command: {
327 type: "thread.interaction-mode.set",
328 commandId: CommandId.make("cmd-interaction-mode-set"),
329 threadId: ThreadId.make("thread-1"),
330 interactionMode: "plan",
331 createdAt: now,
332 },
333 readModel,
334 });
335
336 const singleResult = Array.isArray(result) ? null : result;
337 if (singleResult === null) {
338 throw new Error("Expected a single interaction-mode-set event.");
339 }
340 expect(singleResult).toMatchObject({
341 type: "thread.interaction-mode-set",
342 payload: {
343 threadId: ThreadId.make("thread-1"),
344 interactionMode: "plan",
345 },
346 });
347 }),
348 );
349});