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