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