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

commandInvariants.test.ts

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