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

commandInvariants.test.ts

223 lines5.8 KB

1import { describe, expect, it } from "vite-plus/test";
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 * as Effect from "effect/Effect";
13​
14import {
15 findThreadById,
16 listThreadsByProjectId,
17 requireNonNegativeInteger,
18 requireThread,
19 requireThreadAbsent,
20} from "./commandInvariants.ts";
21​
22const now = "2026-01-01T00:00:00.000Z";
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 settledOverride: null,
72 settledAt: null,
73 latestTurn: null,
74 messages: [],
75 session: null,
76 activities: [],
77 proposedPlans: [],
78 checkpoints: [],
79 deletedAt: null,
80 },
81 {
82 id: ThreadId.make("thread-2"),
83 projectId: ProjectId.make("project-b"),
84 title: "Thread B",
85 modelSelection: {
86 instanceId: ProviderInstanceId.make("codex"),
87 model: "gpt-5-codex",
88 },
89 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
90 runtimeMode: "full-access",
91 branch: null,
92 worktreePath: null,
93 createdAt: now,
94 updatedAt: now,
95 archivedAt: null,
96 settledOverride: null,
97 settledAt: null,
98 latestTurn: null,
99 messages: [],
100 session: null,
101 activities: [],
102 proposedPlans: [],
103 checkpoints: [],
104 deletedAt: null,
105 },
106 ],
107};
108​
109const messageSendCommand: OrchestrationCommand = {
110 type: "thread.turn.start",
111 commandId: CommandId.make("cmd-1"),
112 threadId: ThreadId.make("thread-1"),
113 message: {
114 messageId: MessageId.make("msg-1"),
115 role: "user",
116 text: "hello",
117 attachments: [],
118 },
119 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
120 runtimeMode: "approval-required",
121 createdAt: now,
122};
123​
124describe("commandInvariants", () => {
125 it("finds threads by id and project", () => {
126 expect(findThreadById(readModel, ThreadId.make("thread-1"))?.projectId).toBe("project-a");
127 expect(findThreadById(readModel, ThreadId.make("missing"))).toBeUndefined();
128 expect(
129 listThreadsByProjectId(readModel, ProjectId.make("project-b")).map((thread) => thread.id),
130 ).toEqual([ThreadId.make("thread-2")]);
131 });
132​
133 it("requires existing thread", async () => {
134 const thread = await Effect.runPromise(
135 requireThread({
136 readModel,
137 command: messageSendCommand,
138 threadId: ThreadId.make("thread-1"),
139 }),
140 );
141 expect(thread.id).toBe(ThreadId.make("thread-1"));
142​
143 await expect(
144 Effect.runPromise(
145 requireThread({
146 readModel,
147 command: messageSendCommand,
148 threadId: ThreadId.make("missing"),
149 }),
150 ),
151 ).rejects.toThrow("does not exist");
152 });
153​
154 it("requires missing thread for create flows", async () => {
155 await Effect.runPromise(
156 requireThreadAbsent({
157 readModel,
158 command: {
159 type: "thread.create",
160 commandId: CommandId.make("cmd-2"),
161 threadId: ThreadId.make("thread-3"),
162 projectId: ProjectId.make("project-a"),
163 title: "new",
164 modelSelection: {
165 instanceId: ProviderInstanceId.make("codex"),
166 model: "gpt-5-codex",
167 },
168 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
169 runtimeMode: "full-access",
170 branch: null,
171 worktreePath: null,
172 createdAt: now,
173 },
174 threadId: ThreadId.make("thread-3"),
175 }),
176 );
177​
178 await expect(
179 Effect.runPromise(
180 requireThreadAbsent({
181 readModel,
182 command: {
183 type: "thread.create",
184 commandId: CommandId.make("cmd-3"),
185 threadId: ThreadId.make("thread-1"),
186 projectId: ProjectId.make("project-a"),
187 title: "dup",
188 modelSelection: {
189 instanceId: ProviderInstanceId.make("codex"),
190 model: "gpt-5-codex",
191 },
192 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
193 runtimeMode: "full-access",
194 branch: null,
195 worktreePath: null,
196 createdAt: now,
197 },
198 threadId: ThreadId.make("thread-1"),
199 }),
200 ),
201 ).rejects.toThrow("already exists");
202 });
203​
204 it("requires non-negative integers", async () => {
205 await Effect.runPromise(
206 requireNonNegativeInteger({
207 commandType: "thread.checkpoint.revert",
208 field: "turnCount",
209 value: 0,
210 }),
211 );
212​
213 await expect(
214 Effect.runPromise(
215 requireNonNegativeInteger({
216 commandType: "thread.checkpoint.revert",
217 field: "turnCount",
218 value: -1,
219 }),
220 ),
221 ).rejects.toThrow("greater than or equal to 0");
222 });
223});