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.makeUnsafe("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.makeUnsafe("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.makeUnsafe("thread-1"),
57 projectId: ProjectId.makeUnsafe("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.makeUnsafe("thread-2"),
80 projectId: ProjectId.makeUnsafe("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.makeUnsafe("cmd-1"),
107 threadId: ThreadId.makeUnsafe("thread-1"),
108 message: {
109 messageId: MessageId.makeUnsafe("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.makeUnsafe("thread-1"))?.projectId).toBe("project-a");
122 expect(findThreadById(readModel, ThreadId.makeUnsafe("missing"))).toBeUndefined();
123 expect(
124 listThreadsByProjectId(readModel, ProjectId.makeUnsafe("project-b")).map(
125 (thread) => thread.id,
126 ),
127 ).toEqual([ThreadId.makeUnsafe("thread-2")]);
128 });
129
130 it("requires existing thread", async () => {
131 const thread = await Effect.runPromise(
132 requireThread({
133 readModel,
134 command: messageSendCommand,
135 threadId: ThreadId.makeUnsafe("thread-1"),
136 }),
137 );
138 expect(thread.id).toBe(ThreadId.makeUnsafe("thread-1"));
139
140 await expect(
141 Effect.runPromise(
142 requireThread({
143 readModel,
144 command: messageSendCommand,
145 threadId: ThreadId.makeUnsafe("missing"),
146 }),
147 ),
148 ).rejects.toThrow("does not exist");
149 });
150
151 it("requires missing thread for create flows", async () => {
152 await Effect.runPromise(
153 requireThreadAbsent({
154 readModel,
155 command: {
156 type: "thread.create",
157 commandId: CommandId.makeUnsafe("cmd-2"),
158 threadId: ThreadId.makeUnsafe("thread-3"),
159 projectId: ProjectId.makeUnsafe("project-a"),
160 title: "new",
161 modelSelection: {
162 provider: "codex",
163 model: "gpt-5-codex",
164 },
165 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
166 runtimeMode: "full-access",
167 branch: null,
168 worktreePath: null,
169 createdAt: now,
170 },
171 threadId: ThreadId.makeUnsafe("thread-3"),
172 }),
173 );
174
175 await expect(
176 Effect.runPromise(
177 requireThreadAbsent({
178 readModel,
179 command: {
180 type: "thread.create",
181 commandId: CommandId.makeUnsafe("cmd-3"),
182 threadId: ThreadId.makeUnsafe("thread-1"),
183 projectId: ProjectId.makeUnsafe("project-a"),
184 title: "dup",
185 modelSelection: {
186 provider: "codex",
187 model: "gpt-5-codex",
188 },
189 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
190 runtimeMode: "full-access",
191 branch: null,
192 worktreePath: null,
193 createdAt: now,
194 },
195 threadId: ThreadId.makeUnsafe("thread-1"),
196 }),
197 ),
198 ).rejects.toThrow("already exists");
199 });
200
201 it("requires non-negative integers", async () => {
202 await Effect.runPromise(
203 requireNonNegativeInteger({
204 commandType: "thread.checkpoint.revert",
205 field: "turnCount",
206 value: 0,
207 }),
208 );
209
210 await expect(
211 Effect.runPromise(
212 requireNonNegativeInteger({
213 commandType: "thread.checkpoint.revert",
214 field: "turnCount",
215 value: -1,
216 }),
217 ),
218 ).rejects.toThrow("greater than or equal to 0");
219 });
220});