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

commandInvariants.test.ts

219 lines5.7 KB

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