Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

decider.projectScripts.test.ts

369 lines11.7 KB

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