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

decider.projectScripts.test.ts

507 lines17.1 KB

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