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

decider.projectScripts.test.ts

183 lines6.0 KB

1import { CommandId, EventId, MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3import { Effect } from "effect";
4​
5import { decideOrchestrationCommand } from "./decider.ts";
6import { createEmptyReadModel, projectEvent } from "./projector.ts";
7​
8const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
9const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
10const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
11​
12describe("decider project scripts", () => {
13 it("emits empty scripts on project.create", async () => {
14 const now = new Date().toISOString();
15 const readModel = createEmptyReadModel(now);
16​
17 const result = await Effect.runPromise(
18 decideOrchestrationCommand({
19 command: {
20 type: "project.create",
21 commandId: CommandId.makeUnsafe("cmd-project-create-scripts"),
22 projectId: asProjectId("project-scripts"),
23 title: "Scripts",
24 workspaceRoot: "/tmp/scripts",
25 createdAt: now,
26 },
27 readModel,
28 }),
29 );
30​
31 const event = Array.isArray(result) ? result[0] : result;
32 expect(event.type).toBe("project.created");
33 expect((event.payload as { scripts: unknown[] }).scripts).toEqual([]);
34 });
35​
36 it("propagates scripts in project.meta.update payload", async () => {
37 const now = new Date().toISOString();
38 const initial = createEmptyReadModel(now);
39 const readModel = await Effect.runPromise(
40 projectEvent(initial, {
41 sequence: 1,
42 eventId: asEventId("evt-project-create-scripts"),
43 aggregateKind: "project",
44 aggregateId: asProjectId("project-scripts"),
45 type: "project.created",
46 occurredAt: now,
47 commandId: CommandId.makeUnsafe("cmd-project-create-scripts"),
48 causationEventId: null,
49 correlationId: CommandId.makeUnsafe("cmd-project-create-scripts"),
50 metadata: {},
51 payload: {
52 projectId: asProjectId("project-scripts"),
53 title: "Scripts",
54 workspaceRoot: "/tmp/scripts",
55 defaultModel: null,
56 scripts: [],
57 createdAt: now,
58 updatedAt: now,
59 },
60 }),
61 );
62​
63 const scripts = [
64 {
65 id: "lint",
66 name: "Lint",
67 command: "bun run lint",
68 icon: "lint",
69 runOnWorktreeCreate: false,
70 },
71 ] as const;
72​
73 const result = await Effect.runPromise(
74 decideOrchestrationCommand({
75 command: {
76 type: "project.meta.update",
77 commandId: CommandId.makeUnsafe("cmd-project-update-scripts"),
78 projectId: asProjectId("project-scripts"),
79 scripts: Array.from(scripts),
80 },
81 readModel,
82 }),
83 );
84​
85 const event = Array.isArray(result) ? result[0] : result;
86 expect(event.type).toBe("project.meta-updated");
87 expect((event.payload as { scripts?: unknown[] }).scripts).toEqual(scripts);
88 });
89​
90 it("emits user message and turn-start-requested events for thread.turn.start", async () => {
91 const now = new Date().toISOString();
92 const initial = createEmptyReadModel(now);
93 const withProject = await Effect.runPromise(
94 projectEvent(initial, {
95 sequence: 1,
96 eventId: asEventId("evt-project-create"),
97 aggregateKind: "project",
98 aggregateId: asProjectId("project-1"),
99 type: "project.created",
100 occurredAt: now,
101 commandId: CommandId.makeUnsafe("cmd-project-create"),
102 causationEventId: null,
103 correlationId: CommandId.makeUnsafe("cmd-project-create"),
104 metadata: {},
105 payload: {
106 projectId: asProjectId("project-1"),
107 title: "Project",
108 workspaceRoot: "/tmp/project",
109 defaultModel: null,
110 scripts: [],
111 createdAt: now,
112 updatedAt: now,
113 },
114 }),
115 );
116 const readModel = await Effect.runPromise(
117 projectEvent(withProject, {
118 sequence: 2,
119 eventId: asEventId("evt-thread-create"),
120 aggregateKind: "thread",
121 aggregateId: ThreadId.makeUnsafe("thread-1"),
122 type: "thread.created",
123 occurredAt: now,
124 commandId: CommandId.makeUnsafe("cmd-thread-create"),
125 causationEventId: null,
126 correlationId: CommandId.makeUnsafe("cmd-thread-create"),
127 metadata: {},
128 payload: {
129 threadId: ThreadId.makeUnsafe("thread-1"),
130 projectId: asProjectId("project-1"),
131 title: "Thread",
132 model: "gpt-5-codex",
133 branch: null,
134 worktreePath: null,
135 createdAt: now,
136 updatedAt: now,
137 },
138 }),
139 );
140​
141 const result = await Effect.runPromise(
142 decideOrchestrationCommand({
143 command: {
144 type: "thread.turn.start",
145 commandId: CommandId.makeUnsafe("cmd-turn-start"),
146 threadId: ThreadId.makeUnsafe("thread-1"),
147 message: {
148 messageId: asMessageId("message-user-1"),
149 role: "user",
150 text: "hello",
151 attachments: [],
152 },
153 model: "gpt-5",
154 effort: "high",
155 approvalPolicy: "on-request",
156 sandboxMode: "workspace-write",
157 createdAt: now,
158 },
159 readModel,
160 }),
161 );
162​
163 expect(Array.isArray(result)).toBe(true);
164 const events = Array.isArray(result) ? result : [result];
165 expect(events).toHaveLength(2);
166 expect(events[0]?.type).toBe("thread.message-sent");
167 const turnStartEvent = events[1];
168 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
169 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
170 if (turnStartEvent?.type !== "thread.turn-start-requested") {
171 return;
172 }
173 expect(turnStartEvent.payload.assistantDeliveryMode).toBe("buffered");
174 expect(turnStartEvent.payload).toMatchObject({
175 threadId: ThreadId.makeUnsafe("thread-1"),
176 messageId: asMessageId("message-user-1"),
177 model: "gpt-5",
178 effort: "high",
179 approvalPolicy: "on-request",
180 sandboxMode: "workspace-write",
181 });
182 });
183});