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

decider.projectScripts.test.ts

617 lines20.9 KB

1import {
2 CommandId,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 EventId,
5 MessageId,
6 ProjectId,
7 ThreadId,
8 ProviderInstanceId,
9 type ProjectScript,
10} from "@t3tools/contracts";
11import { createModelSelection } from "@t3tools/shared/model";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14import * as NodeServices from "@effect/platform-node/NodeServices";
15​
16import { decideOrchestrationCommand } from "./decider.ts";
17import { createEmptyReadModel, projectEvent } from "./projector.ts";
18​
19const asEventId = (value: string): EventId => EventId.make(value);
20const asProjectId = (value: string): ProjectId => ProjectId.make(value);
21const asMessageId = (value: string): MessageId => MessageId.make(value);
22it.layer(NodeServices.layer)("decider project scripts", (it) => {
23 it.effect("emits empty scripts on project.create", () =>
24 Effect.gen(function* () {
25 const now = "2026-01-01T00:00:00.000Z";
26 const readModel = createEmptyReadModel(now);
27​
28 const result = yield* decideOrchestrationCommand({
29 command: {
30 type: "project.create",
31 commandId: CommandId.make("cmd-project-create-scripts"),
32 projectId: asProjectId("project-scripts"),
33 title: "Scripts",
34 workspaceRoot: "/tmp/scripts",
35 createdAt: now,
36 },
37 readModel,
38 });
39​
40 const event = Array.isArray(result) ? result[0] : result;
41 expect(event.type).toBe("project.created");
42 expect((event.payload as { scripts: unknown[] }).scripts).toEqual([]);
43 }),
44 );
45​
46 it.effect("propagates scripts in project.meta.update payload", () =>
47 Effect.gen(function* () {
48 const now = "2026-01-01T00:00:00.000Z";
49 const initial = createEmptyReadModel(now);
50 const readModel = yield* projectEvent(initial, {
51 sequence: 1,
52 eventId: asEventId("evt-project-create-scripts"),
53 aggregateKind: "project",
54 aggregateId: asProjectId("project-scripts"),
55 type: "project.created",
56 occurredAt: now,
57 commandId: CommandId.make("cmd-project-create-scripts"),
58 causationEventId: null,
59 correlationId: CommandId.make("cmd-project-create-scripts"),
60 metadata: {},
61 payload: {
62 projectId: asProjectId("project-scripts"),
63 title: "Scripts",
64 workspaceRoot: "/tmp/scripts",
65 defaultModelSelection: null,
66 scripts: [],
67 createdAt: now,
68 updatedAt: now,
69 },
70 });
71​
72 const scripts = [
73 {
74 id: "lint",
75 name: "Lint",
76 command: "bun run lint",
77 icon: "lint",
78 runOnWorktreeCreate: false,
79 },
80 ] as const;
81​
82 const result = yield* decideOrchestrationCommand({
83 command: {
84 type: "project.meta.update",
85 commandId: CommandId.make("cmd-project-update-scripts"),
86 projectId: asProjectId("project-scripts"),
87 scripts: Array.from(scripts),
88 },
89 readModel,
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​
98 const script = (id: string): ProjectScript => ({
99 id,
100 name: "Install dependencies",
101 command: "vp i",
102 icon: "configure",
103 runOnWorktreeCreate: false,
104 });
105​
106 const projectWithScripts = (scripts: ReadonlyArray<ProjectScript>) => {
107 const now = "2026-01-01T00:00:00.000Z";
108 return projectEvent(createEmptyReadModel(now), {
109 sequence: 1,
110 eventId: asEventId("evt-legacy-scripts"),
111 aggregateKind: "project",
112 aggregateId: asProjectId("project-scripts"),
113 type: "project.created",
114 occurredAt: now,
115 commandId: CommandId.make("cmd-legacy-scripts"),
116 causationEventId: null,
117 correlationId: CommandId.make("cmd-legacy-scripts"),
118 metadata: {},
119 payload: {
120 projectId: asProjectId("project-scripts"),
121 title: "Scripts",
122 workspaceRoot: "/tmp/scripts",
123 defaultModelSelection: null,
124 scripts,
125 createdAt: now,
126 updatedAt: now,
127 },
128 });
129 };
130​
131 for (const id of ["install-javascript-dependencies", "A", "a.b", "a b", "-a", "a".repeat(25)]) {
132 it.effect(`rejects a new script ID that cannot have a shortcut: ${id}`, () =>
133 Effect.gen(function* () {
134 const readModel = yield* projectWithScripts([]);
135 const failure = yield* Effect.flip(
136 decideOrchestrationCommand({
137 readModel,
138 command: {
139 type: "project.meta.update",
140 commandId: CommandId.make("cmd-invalid-script"),
141 projectId: asProjectId("project-scripts"),
142 scripts: [script("lint"), script(id)],
143 },
144 }),
145 );
146 expect(failure).toMatchObject({ _tag: "OrchestrationCommandInvariantError" });
147 expect(failure.message).toContain("Script ID");
148 expect(failure.message).toContain("24");
149 expect(readModel.projects[0]?.scripts).toEqual([]);
150 }),
151 );
152 }
153​
154 it.effect("accepts a script ID at the shortcut length limit", () =>
155 Effect.gen(function* () {
156 const readModel = yield* projectWithScripts([]);
157 const scripts = [script("a".repeat(24))];
158 const result = yield* decideOrchestrationCommand({
159 readModel,
160 command: {
161 type: "project.meta.update",
162 commandId: CommandId.make("cmd-valid-script"),
163 projectId: asProjectId("project-scripts"),
164 scripts,
165 },
166 });
167 const event = Array.isArray(result) ? result[0] : result;
168 expect(event.payload).toMatchObject({ scripts });
169 }),
170 );
171​
172 it.effect(
173 "keeps legacy scripts readable, editable and removable while allowing valid additions",
174 () =>
175 Effect.gen(function* () {
176 const legacy = script("install-javascript-dependencies");
177 const readModel = yield* projectWithScripts([legacy]);
178 expect(readModel.projects[0]?.scripts).toEqual([legacy]);
179 for (const scripts of [[{ ...legacy, command: "vp install" }, script("lint")], []]) {
180 const result = yield* decideOrchestrationCommand({
181 readModel,
182 command: {
183 type: "project.meta.update",
184 commandId: CommandId.make("cmd-repair-script"),
185 projectId: asProjectId("project-scripts"),
186 scripts,
187 },
188 });
189 const event = Array.isArray(result) ? result[0] : result;
190 expect(event.payload).toMatchObject({ scripts });
191 }
192 const failure = yield* Effect.flip(
193 decideOrchestrationCommand({
194 readModel,
195 command: {
196 type: "project.meta.update",
197 commandId: CommandId.make("cmd-new-invalid-script"),
198 projectId: asProjectId("project-scripts"),
199 scripts: [legacy, script("another.invalid.id")],
200 },
201 }),
202 );
203 expect(failure).toMatchObject({ _tag: "OrchestrationCommandInvariantError" });
204 }),
205 );
206​
207 it.effect("propagates project icon metadata in project.meta.update", () =>
208 Effect.gen(function* () {
209 const now = "2026-01-01T00:00:00.000Z";
210 const readModel = yield* projectEvent(createEmptyReadModel(now), {
211 sequence: 1,
212 eventId: asEventId("evt-project-create-favicon"),
213 aggregateKind: "project",
214 aggregateId: asProjectId("project-favicon"),
215 type: "project.created",
216 occurredAt: now,
217 commandId: CommandId.make("cmd-project-create-favicon"),
218 causationEventId: null,
219 correlationId: CommandId.make("cmd-project-create-favicon"),
220 metadata: {},
221 payload: {
222 projectId: asProjectId("project-favicon"),
223 title: "Favicon",
224 workspaceRoot: "/tmp/favicon",
225 defaultModelSelection: null,
226 scripts: [],
227 createdAt: now,
228 updatedAt: now,
229 },
230 });
231​
232 const result = yield* decideOrchestrationCommand({
233 command: {
234 type: "project.meta.update",
235 commandId: CommandId.make("cmd-project-update-favicon"),
236 projectId: asProjectId("project-favicon"),
237 faviconPath: "brand/icon.svg",
238 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
239 },
240 readModel,
241 });
242​
243 const event = Array.isArray(result) ? result[0] : result;
244 expect(event.type).toBe("project.meta-updated");
245 expect((event.payload as { faviconPath?: string }).faviconPath).toBe("brand/icon.svg");
246 expect((event.payload as { projectIcon?: unknown }).projectIcon).toEqual({
247 kind: "lucide",
248 name: "alarm-clock",
249 color: "violet",
250 });
251 }),
252 );
253​
254 it.effect("rejects project.create for an active workspace root that already exists", () =>
255 Effect.gen(function* () {
256 const now = "2026-01-01T00:00:00.000Z";
257 const initial = createEmptyReadModel(now);
258 const readModel = yield* projectEvent(initial, {
259 sequence: 1,
260 eventId: asEventId("evt-project-create"),
261 aggregateKind: "project",
262 aggregateId: asProjectId("project-existing"),
263 type: "project.created",
264 occurredAt: now,
265 commandId: CommandId.make("cmd-project-create"),
266 causationEventId: null,
267 correlationId: CommandId.make("cmd-project-create"),
268 metadata: {},
269 payload: {
270 projectId: asProjectId("project-existing"),
271 title: "Project",
272 workspaceRoot: "/tmp/project",
273 defaultModelSelection: null,
274 scripts: [],
275 createdAt: now,
276 updatedAt: now,
277 },
278 });
279​
280 const failure = yield* Effect.flip(
281 decideOrchestrationCommand({
282 command: {
283 type: "project.create",
284 commandId: CommandId.make("cmd-project-create-duplicate-root"),
285 projectId: asProjectId("project-duplicate-root"),
286 title: "Duplicate Project",
287 workspaceRoot: "/tmp/project/",
288 createdAt: now,
289 },
290 readModel,
291 }),
292 );
293​
294 expect(failure.message).toContain(
295 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
296 );
297 }),
298 );
299​
300 it.effect("rejects project.meta.update when moving onto another active workspace root", () =>
301 Effect.gen(function* () {
302 const now = "2026-01-01T00:00:00.000Z";
303 const initial = createEmptyReadModel(now);
304 const withFirstProject = yield* projectEvent(initial, {
305 sequence: 1,
306 eventId: asEventId("evt-project-create-first"),
307 aggregateKind: "project",
308 aggregateId: asProjectId("project-first"),
309 type: "project.created",
310 occurredAt: now,
311 commandId: CommandId.make("cmd-project-create-first"),
312 causationEventId: null,
313 correlationId: CommandId.make("cmd-project-create-first"),
314 metadata: {},
315 payload: {
316 projectId: asProjectId("project-first"),
317 title: "First",
318 workspaceRoot: "/tmp/project-first",
319 defaultModelSelection: null,
320 scripts: [],
321 createdAt: now,
322 updatedAt: now,
323 },
324 });
325 const readModel = yield* projectEvent(withFirstProject, {
326 sequence: 2,
327 eventId: asEventId("evt-project-create-second"),
328 aggregateKind: "project",
329 aggregateId: asProjectId("project-second"),
330 type: "project.created",
331 occurredAt: now,
332 commandId: CommandId.make("cmd-project-create-second"),
333 causationEventId: null,
334 correlationId: CommandId.make("cmd-project-create-second"),
335 metadata: {},
336 payload: {
337 projectId: asProjectId("project-second"),
338 title: "Second",
339 workspaceRoot: "/tmp/project-second",
340 defaultModelSelection: null,
341 scripts: [],
342 createdAt: now,
343 updatedAt: now,
344 },
345 });
346​
347 const failure = yield* Effect.flip(
348 decideOrchestrationCommand({
349 command: {
350 type: "project.meta.update",
351 commandId: CommandId.make("cmd-project-update-duplicate-root"),
352 projectId: asProjectId("project-second"),
353 workspaceRoot: "/tmp/project-first",
354 },
355 readModel,
356 }),
357 );
358​
359 expect(failure.message).toContain(
360 "Active project 'project-first' already exists for workspace root '/tmp/project-first'.",
361 );
362 }),
363 );
364​
365 it.effect("emits user message and turn-start-requested events for thread.turn.start", () =>
366 Effect.gen(function* () {
367 const now = "2026-01-01T00:00:00.000Z";
368 const initial = createEmptyReadModel(now);
369 const withProject = yield* projectEvent(initial, {
370 sequence: 1,
371 eventId: asEventId("evt-project-create"),
372 aggregateKind: "project",
373 aggregateId: asProjectId("project-1"),
374 type: "project.created",
375 occurredAt: now,
376 commandId: CommandId.make("cmd-project-create"),
377 causationEventId: null,
378 correlationId: CommandId.make("cmd-project-create"),
379 metadata: {},
380 payload: {
381 projectId: asProjectId("project-1"),
382 title: "Project",
383 workspaceRoot: "/tmp/project",
384 defaultModelSelection: null,
385 scripts: [],
386 createdAt: now,
387 updatedAt: now,
388 },
389 });
390 const readModel = yield* projectEvent(withProject, {
391 sequence: 2,
392 eventId: asEventId("evt-thread-create"),
393 aggregateKind: "thread",
394 aggregateId: ThreadId.make("thread-1"),
395 type: "thread.created",
396 occurredAt: now,
397 commandId: CommandId.make("cmd-thread-create"),
398 causationEventId: null,
399 correlationId: CommandId.make("cmd-thread-create"),
400 metadata: {},
401 payload: {
402 threadId: ThreadId.make("thread-1"),
403 projectId: asProjectId("project-1"),
404 title: "Thread",
405 modelSelection: {
406 instanceId: ProviderInstanceId.make("codex"),
407 model: "gpt-5-codex",
408 },
409 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
410 runtimeMode: "approval-required",
411 branch: null,
412 worktreePath: null,
413 createdAt: now,
414 updatedAt: now,
415 },
416 });
417​
418 const result = yield* decideOrchestrationCommand({
419 command: {
420 type: "thread.turn.start",
421 commandId: CommandId.make("cmd-turn-start"),
422 threadId: ThreadId.make("thread-1"),
423 message: {
424 messageId: asMessageId("message-user-1"),
425 role: "user",
426 text: "hello",
427 attachments: [],
428 },
429 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
430 { id: "reasoningEffort", value: "high" },
431 { id: "fastMode", value: true },
432 ]),
433 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
434 runtimeMode: "approval-required",
435 createdAt: now,
436 },
437 readModel,
438 });
439​
440 expect(Array.isArray(result)).toBe(true);
441 const events = Array.isArray(result) ? result : [result];
442 expect(events).toHaveLength(2);
443 expect(events[0]?.type).toBe("thread.message-sent");
444 const turnStartEvent = events[1];
445 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
446 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
447 if (turnStartEvent?.type !== "thread.turn-start-requested") {
448 return;
449 }
450 expect(turnStartEvent.payload).toMatchObject({
451 threadId: ThreadId.make("thread-1"),
452 messageId: asMessageId("message-user-1"),
453 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
454 { id: "reasoningEffort", value: "high" },
455 { id: "fastMode", value: true },
456 ]),
457 runtimeMode: "approval-required",
458 });
459 }),
460 );
461​
462 it.effect("emits thread.runtime-mode-set from thread.runtime-mode.set", () =>
463 Effect.gen(function* () {
464 const now = "2026-01-01T00:00:00.000Z";
465 const initial = createEmptyReadModel(now);
466 const withProject = yield* projectEvent(initial, {
467 sequence: 1,
468 eventId: asEventId("evt-project-create"),
469 aggregateKind: "project",
470 aggregateId: asProjectId("project-1"),
471 type: "project.created",
472 occurredAt: now,
473 commandId: CommandId.make("cmd-project-create"),
474 causationEventId: null,
475 correlationId: CommandId.make("cmd-project-create"),
476 metadata: {},
477 payload: {
478 projectId: asProjectId("project-1"),
479 title: "Project",
480 workspaceRoot: "/tmp/project",
481 defaultModelSelection: null,
482 scripts: [],
483 createdAt: now,
484 updatedAt: now,
485 },
486 });
487 const readModel = yield* projectEvent(withProject, {
488 sequence: 2,
489 eventId: asEventId("evt-thread-create"),
490 aggregateKind: "thread",
491 aggregateId: ThreadId.make("thread-1"),
492 type: "thread.created",
493 occurredAt: now,
494 commandId: CommandId.make("cmd-thread-create"),
495 causationEventId: null,
496 correlationId: CommandId.make("cmd-thread-create"),
497 metadata: {},
498 payload: {
499 threadId: ThreadId.make("thread-1"),
500 projectId: asProjectId("project-1"),
501 title: "Thread",
502 modelSelection: {
503 instanceId: ProviderInstanceId.make("codex"),
504 model: "gpt-5-codex",
505 },
506 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
507 runtimeMode: "full-access",
508 branch: null,
509 worktreePath: null,
510 createdAt: now,
511 updatedAt: now,
512 },
513 });
514​
515 const result = yield* decideOrchestrationCommand({
516 command: {
517 type: "thread.runtime-mode.set",
518 commandId: CommandId.make("cmd-runtime-mode-set"),
519 threadId: ThreadId.make("thread-1"),
520 runtimeMode: "approval-required",
521 createdAt: now,
522 },
523 readModel,
524 });
525​
526 const singleResult = Array.isArray(result) ? null : result;
527 if (singleResult === null) {
528 throw new Error("Expected a single runtime-mode-set event.");
529 }
530 expect(singleResult).toMatchObject({
531 type: "thread.runtime-mode-set",
532 payload: {
533 threadId: ThreadId.make("thread-1"),
534 runtimeMode: "approval-required",
535 },
536 });
537 }),
538 );
539​
540 it.effect("emits thread.interaction-mode-set from thread.interaction-mode.set", () =>
541 Effect.gen(function* () {
542 const now = "2026-01-01T00:00:00.000Z";
543 const initial = createEmptyReadModel(now);
544 const withProject = yield* projectEvent(initial, {
545 sequence: 1,
546 eventId: asEventId("evt-project-create"),
547 aggregateKind: "project",
548 aggregateId: asProjectId("project-1"),
549 type: "project.created",
550 occurredAt: now,
551 commandId: CommandId.make("cmd-project-create"),
552 causationEventId: null,
553 correlationId: CommandId.make("cmd-project-create"),
554 metadata: {},
555 payload: {
556 projectId: asProjectId("project-1"),
557 title: "Project",
558 workspaceRoot: "/tmp/project",
559 defaultModelSelection: null,
560 scripts: [],
561 createdAt: now,
562 updatedAt: now,
563 },
564 });
565 const readModel = yield* projectEvent(withProject, {
566 sequence: 2,
567 eventId: asEventId("evt-thread-create"),
568 aggregateKind: "thread",
569 aggregateId: ThreadId.make("thread-1"),
570 type: "thread.created",
571 occurredAt: now,
572 commandId: CommandId.make("cmd-thread-create"),
573 causationEventId: null,
574 correlationId: CommandId.make("cmd-thread-create"),
575 metadata: {},
576 payload: {
577 threadId: ThreadId.make("thread-1"),
578 projectId: asProjectId("project-1"),
579 title: "Thread",
580 modelSelection: {
581 instanceId: ProviderInstanceId.make("codex"),
582 model: "gpt-5-codex",
583 },
584 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
585 runtimeMode: "approval-required",
586 branch: null,
587 worktreePath: null,
588 createdAt: now,
589 updatedAt: now,
590 },
591 });
592​
593 const result = yield* decideOrchestrationCommand({
594 command: {
595 type: "thread.interaction-mode.set",
596 commandId: CommandId.make("cmd-interaction-mode-set"),
597 threadId: ThreadId.make("thread-1"),
598 interactionMode: "plan",
599 createdAt: now,
600 },
601 readModel,
602 });
603​
604 const singleResult = Array.isArray(result) ? null : result;
605 if (singleResult === null) {
606 throw new Error("Expected a single interaction-mode-set event.");
607 }
608 expect(singleResult).toMatchObject({
609 type: "thread.interaction-mode-set",
610 payload: {
611 threadId: ThreadId.make("thread-1"),
612 interactionMode: "plan",
613 },
614 });
615 }),
616 );
617});