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