Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import {
3 MessageId,
4 CommandId,
5 DEFAULT_PROVIDER_INTERACTION_MODE,
6 ProjectId,
7 ThreadId,
8 type OrchestrationCommand,
9 type OrchestrationReadModel,
10} from "@t3tools/contracts";
11import { Effect } from "effect";
12
13import {
14 findThreadById,
15 listThreadsByProjectId,
16 requireNonNegativeInteger,
17 requireThread,
18 requireThreadAbsent,
19} from "./commandInvariants.ts";
20
21const now = new Date().toISOString();
22
23const readModel: OrchestrationReadModel = {
24 snapshotSequence: 2,
25 updatedAt: now,
26 projects: [
27 {
28 id: ProjectId.make("project-a"),
29 title: "Project A",
30 workspaceRoot: "/tmp/project-a",
31 defaultModelSelection: {
32 provider: "codex",
33 model: "gpt-5-codex",
34 },
35 scripts: [],
36 createdAt: now,
37 updatedAt: now,
38 deletedAt: null,
39 },
40 {
41 id: ProjectId.make("project-b"),
42 title: "Project B",
43 workspaceRoot: "/tmp/project-b",
44 defaultModelSelection: {
45 provider: "codex",
46 model: "gpt-5-codex",
47 },
48 scripts: [],
49 createdAt: now,
50 updatedAt: now,
51 deletedAt: null,
52 },
53 ],
54 threads: [
55 {
56 id: ThreadId.make("thread-1"),
57 projectId: ProjectId.make("project-a"),
58 title: "Thread A",
59 modelSelection: {
60 provider: "codex",
61 model: "gpt-5-codex",
62 },
63 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
64 runtimeMode: "full-access",
65 branch: null,
66 worktreePath: null,
67 createdAt: now,
68 updatedAt: now,
69 archivedAt: null,
70 latestTurn: null,
71 messages: [],
72 session: null,
73 activities: [],
74 proposedPlans: [],
75 checkpoints: [],
76 deletedAt: null,
77 },
78 {
79 id: ThreadId.make("thread-2"),
80 projectId: ProjectId.make("project-b"),
81 title: "Thread B",
82 modelSelection: {
83 provider: "codex",
84 model: "gpt-5-codex",
85 },
86 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
87 runtimeMode: "full-access",
88 branch: null,
89 worktreePath: null,
90 createdAt: now,
91 updatedAt: now,
92 archivedAt: null,
93 latestTurn: null,
94 messages: [],
95 session: null,
96 activities: [],
97 proposedPlans: [],
98 checkpoints: [],
99 deletedAt: null,
100 },
101 ],
102};
103
104const messageSendCommand: OrchestrationCommand = {
105 type: "thread.turn.start",
106 commandId: CommandId.make("cmd-1"),
107 threadId: ThreadId.make("thread-1"),
108 message: {
109 messageId: MessageId.make("msg-1"),
110 role: "user",
111 text: "hello",
112 attachments: [],
113 },
114 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
115 runtimeMode: "approval-required",
116 createdAt: now,
117};
118
119describe("commandInvariants", () => {
120 it("finds threads by id and project", () => {
121 expect(findThreadById(readModel, ThreadId.make("thread-1"))?.projectId).toBe("project-a");
122 expect(findThreadById(readModel, ThreadId.make("missing"))).toBeUndefined();
123 expect(
124 listThreadsByProjectId(readModel, ProjectId.make("project-b")).map((thread) => thread.id),
125 ).toEqual([ThreadId.make("thread-2")]);
126 });
127
128 it("requires existing thread", async () => {
129 const thread = await Effect.runPromise(
130 requireThread({
131 readModel,
132 command: messageSendCommand,
133 threadId: ThreadId.make("thread-1"),
134 }),
135 );
136 expect(thread.id).toBe(ThreadId.make("thread-1"));
137
138 await expect(
139 Effect.runPromise(
140 requireThread({
141 readModel,
142 command: messageSendCommand,
143 threadId: ThreadId.make("missing"),
144 }),
145 ),
146 ).rejects.toThrow("does not exist");
147 });
148
149 it("requires missing thread for create flows", async () => {
150 await Effect.runPromise(
151 requireThreadAbsent({
152 readModel,
153 command: {
154 type: "thread.create",
155 commandId: CommandId.make("cmd-2"),
156 threadId: ThreadId.make("thread-3"),
157 projectId: ProjectId.make("project-a"),
158 title: "new",
159 modelSelection: {
160 provider: "codex",
161 model: "gpt-5-codex",
162 },
163 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
164 runtimeMode: "full-access",
165 branch: null,
166 worktreePath: null,
167 createdAt: now,
168 },
169 threadId: ThreadId.make("thread-3"),
170 }),
171 );
172
173 await expect(
174 Effect.runPromise(
175 requireThreadAbsent({
176 readModel,
177 command: {
178 type: "thread.create",
179 commandId: CommandId.make("cmd-3"),
180 threadId: ThreadId.make("thread-1"),
181 projectId: ProjectId.make("project-a"),
182 title: "dup",
183 modelSelection: {
184 provider: "codex",
185 model: "gpt-5-codex",
186 },
187 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
188 runtimeMode: "full-access",
189 branch: null,
190 worktreePath: null,
191 createdAt: now,
192 },
193 threadId: ThreadId.make("thread-1"),
194 }),
195 ),
196 ).rejects.toThrow("already exists");
197 });
198
199 it("requires non-negative integers", async () => {
200 await Effect.runPromise(
201 requireNonNegativeInteger({
202 commandType: "thread.checkpoint.revert",
203 field: "turnCount",
204 value: 0,
205 }),
206 );
207
208 await expect(
209 Effect.runPromise(
210 requireNonNegativeInteger({
211 commandType: "thread.checkpoint.revert",
212 field: "turnCount",
213 value: -1,
214 }),
215 ),
216 ).rejects.toThrow("greater than or equal to 0");
217 });
218});