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 createdAt: now,
63 updatedAt: now,
64 archivedAt: null,
65 settledOverride: null,
66 settledAt: null,
67 latestTurn: null,
68 messages: [],
69 session: null,
70 activities: [],
71 proposedPlans: [],
72 checkpoints: [],
73 deletedAt: null,
74 },
75 {
76 id: ThreadId.make("thread-2"),
77 projectId: ProjectId.make("project-b"),
78 title: "Thread B",
79 modelSelection: {
80 instanceId: ProviderInstanceId.make("codex"),
81 model: "gpt-5-codex",
82 },
83 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
84 runtimeMode: "full-access",
85 branch: null,
86 worktreePath: null,
87 createdAt: now,
88 updatedAt: now,
89 archivedAt: null,
90 settledOverride: null,
91 settledAt: null,
92 latestTurn: null,
93 messages: [],
94 session: null,
95 activities: [],
96 proposedPlans: [],
97 checkpoints: [],
98 deletedAt: null,
99 },
100 ],
101};
102
103const messageSendCommand: OrchestrationCommand = {
104 type: "thread.turn.start",
105 commandId: CommandId.make("cmd-1"),
106 threadId: ThreadId.make("thread-1"),
107 message: {
108 messageId: MessageId.make("msg-1"),
109 role: "user",
110 text: "hello",
111 attachments: [],
112 },
113 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
114 runtimeMode: "approval-required",
115 createdAt: now,
116};
117
118describe("commandInvariants", () => {
119 it("lists threads by project", () => {
120 expect(
121 listThreadsByProjectId(readModel, ProjectId.make("project-b")).map((thread) => thread.id),
122 ).toEqual([ThreadId.make("thread-2")]);
123 });
124
125 it("requires existing thread", async () => {
126 const thread = await Effect.runPromise(
127 requireThread({
128 readModel,
129 command: messageSendCommand,
130 threadId: ThreadId.make("thread-1"),
131 }),
132 );
133 expect(thread.id).toBe(ThreadId.make("thread-1"));
134
135 await expect(
136 Effect.runPromise(
137 requireThread({
138 readModel,
139 command: messageSendCommand,
140 threadId: ThreadId.make("missing"),
141 }),
142 ),
143 ).rejects.toThrow("does not exist");
144 });
145
146 it("requires missing thread for create flows", async () => {
147 await Effect.runPromise(
148 requireThreadAbsent({
149 readModel,
150 command: {
151 type: "thread.create",
152 commandId: CommandId.make("cmd-2"),
153 threadId: ThreadId.make("thread-3"),
154 projectId: ProjectId.make("project-a"),
155 title: "new",
156 modelSelection: {
157 instanceId: ProviderInstanceId.make("codex"),
158 model: "gpt-5-codex",
159 },
160 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
161 runtimeMode: "full-access",
162 branch: null,
163 worktreePath: null,
164 createdAt: now,
165 },
166 threadId: ThreadId.make("thread-3"),
167 }),
168 );
169
170 await expect(
171 Effect.runPromise(
172 requireThreadAbsent({
173 readModel,
174 command: {
175 type: "thread.create",
176 commandId: CommandId.make("cmd-3"),
177 threadId: ThreadId.make("thread-1"),
178 projectId: ProjectId.make("project-a"),
179 title: "dup",
180 modelSelection: {
181 instanceId: ProviderInstanceId.make("codex"),
182 model: "gpt-5-codex",
183 },
184 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
185 runtimeMode: "full-access",
186 branch: null,
187 worktreePath: null,
188 createdAt: now,
189 },
190 threadId: ThreadId.make("thread-1"),
191 }),
192 ),
193 ).rejects.toThrow("already exists");
194 });
195
196 it("lets a draft retry re-create a thread id after its first attempt was deleted", async () => {
197 const threadId = ThreadId.make("thread-1");
198 const firstAttempt = readModel.threads.find((thread) => thread.id === threadId)!;
199 const afterRollback: OrchestrationReadModel = {
200 ...readModel,
201 threads: readModel.threads.map((thread) =>
202 thread.id === threadId ? { ...thread, deletedAt: now, updatedAt: now } : thread,
203 ),
204 };
205 const retry: OrchestrationCommand = {
206 type: "thread.create",
207 commandId: CommandId.make("cmd-retry"),
208 threadId,
209 projectId: firstAttempt.projectId,
210 title: firstAttempt.title,
211 modelSelection: firstAttempt.modelSelection,
212 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
213 runtimeMode: "approval-required",
214 branch: null,
215 worktreePath: null,
216 createdAt: now,
217 };
218
219 await expect(
220 Effect.runPromise(
221 requireThreadAbsent({ readModel: afterRollback, command: retry, threadId }),
222 ),
223 ).resolves.toBeUndefined();
224 });
225});