Open History to see the last commit
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 { listThreadsByProjectId, requireThread, requireThreadAbsent } from "./commandInvariants.ts";
15
16const now = "2026-01-01T00:00:00.000Z";
17
18const readModel: OrchestrationReadModel = {
19 snapshotSequence: 2,
20 updatedAt: now,
21 projects: [
22 {
23 id: ProjectId.make("project-a"),
24 title: "Project A",
25 workspaceRoot: "/tmp/project-a",
26 defaultModelSelection: {
27 instanceId: ProviderInstanceId.make("codex"),
28 model: "gpt-5-codex",
29 },
30 scripts: [],
31 createdAt: now,
32 updatedAt: now,
33 deletedAt: null,
34 },
35 {
36 id: ProjectId.make("project-b"),
37 title: "Project B",
38 workspaceRoot: "/tmp/project-b",
39 defaultModelSelection: {
40 instanceId: ProviderInstanceId.make("codex"),
41 model: "gpt-5-codex",
42 },
43 scripts: [],
44 createdAt: now,
45 updatedAt: now,
46 deletedAt: null,
47 },
48 ],
49 threads: [
50 {
51 id: ThreadId.make("thread-1"),
52 projectId: ProjectId.make("project-a"),
53 title: "Thread A",
54 modelSelection: {
55 instanceId: ProviderInstanceId.make("codex"),
56 model: "gpt-5-codex",
57 },
58 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
59 runtimeMode: "full-access",
60 branch: null,
61 worktreePath: null,
62 pullRequests: [],
63 createdAt: now,
64 updatedAt: now,
65 archivedAt: null,
66 settledOverride: null,
67 settledAt: null,
68 latestTurn: null,
69 messages: [],
70 session: null,
71 activities: [],
72 proposedPlans: [],
73 checkpoints: [],
74 deletedAt: null,
75 },
76 {
77 id: ThreadId.make("thread-2"),
78 projectId: ProjectId.make("project-b"),
79 title: "Thread B",
80 modelSelection: {
81 instanceId: ProviderInstanceId.make("codex"),
82 model: "gpt-5-codex",
83 },
84 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
85 runtimeMode: "full-access",
86 branch: null,
87 worktreePath: null,
88 pullRequests: [],
89 createdAt: now,
90 updatedAt: now,
91 archivedAt: null,
92 settledOverride: null,
93 settledAt: 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("lists threads by project", () => {
122 expect(
123 listThreadsByProjectId(readModel, ProjectId.make("project-b")).map((thread) => thread.id),
124 ).toEqual([ThreadId.make("thread-2")]);
125 });
126
127 it("requires existing thread", async () => {
128 const thread = await Effect.runPromise(
129 requireThread({
130 readModel,
131 command: messageSendCommand,
132 threadId: ThreadId.make("thread-1"),
133 }),
134 );
135 expect(thread.id).toBe(ThreadId.make("thread-1"));
136
137 await expect(
138 Effect.runPromise(
139 requireThread({
140 readModel,
141 command: messageSendCommand,
142 threadId: ThreadId.make("missing"),
143 }),
144 ),
145 ).rejects.toThrow("does not exist");
146 });
147
148 it("requires missing thread for create flows", async () => {
149 await Effect.runPromise(
150 requireThreadAbsent({
151 readModel,
152 command: {
153 type: "thread.create",
154 commandId: CommandId.make("cmd-2"),
155 threadId: ThreadId.make("thread-3"),
156 projectId: ProjectId.make("project-a"),
157 title: "new",
158 modelSelection: {
159 instanceId: ProviderInstanceId.make("codex"),
160 model: "gpt-5-codex",
161 },
162 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
163 runtimeMode: "full-access",
164 branch: null,
165 worktreePath: null,
166 createdAt: now,
167 },
168 threadId: ThreadId.make("thread-3"),
169 }),
170 );
171
172 await expect(
173 Effect.runPromise(
174 requireThreadAbsent({
175 readModel,
176 command: {
177 type: "thread.create",
178 commandId: CommandId.make("cmd-3"),
179 threadId: ThreadId.make("thread-1"),
180 projectId: ProjectId.make("project-a"),
181 title: "dup",
182 modelSelection: {
183 instanceId: ProviderInstanceId.make("codex"),
184 model: "gpt-5-codex",
185 },
186 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
187 runtimeMode: "full-access",
188 branch: null,
189 worktreePath: null,
190 createdAt: now,
191 },
192 threadId: ThreadId.make("thread-1"),
193 }),
194 ),
195 ).rejects.toThrow("already exists");
196 });
197
198 it("lets a draft retry re-create a thread id after its first attempt was deleted", async () => {
199 const threadId = ThreadId.make("thread-1");
200 const firstAttempt = readModel.threads.find((thread) => thread.id === threadId)!;
201 const afterRollback: OrchestrationReadModel = {
202 ...readModel,
203 threads: readModel.threads.map((thread) =>
204 thread.id === threadId ? { ...thread, deletedAt: now, updatedAt: now } : thread,
205 ),
206 };
207 const retry: OrchestrationCommand = {
208 type: "thread.create",
209 commandId: CommandId.make("cmd-retry"),
210 threadId,
211 projectId: firstAttempt.projectId,
212 title: firstAttempt.title,
213 modelSelection: firstAttempt.modelSelection,
214 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
215 runtimeMode: "approval-required",
216 branch: null,
217 worktreePath: null,
218 createdAt: now,
219 };
220
221 await expect(
222 Effect.runPromise(
223 requireThreadAbsent({ readModel: afterRollback, command: retry, threadId }),
224 ),
225 ).resolves.toBeUndefined();
226 });
227});