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

commandInvariants.test.ts

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