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

ProjectionSnapshotQuery.test.ts

341 lines9.8 KB

1import { CheckpointRef, EventId, MessageId, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import { Effect, Layer } from "effect";
4import * as SqlClient from "effect/unstable/sql/SqlClient";
5​
6import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
7import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
8import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
9import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
10​
11const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
12const asTurnId = (value: string): TurnId => TurnId.makeUnsafe(value);
13const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
14const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
15const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.makeUnsafe(value);
16​
17const projectionSnapshotLayer = it.layer(
18 OrchestrationProjectionSnapshotQueryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
19);
20​
21projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
22 it.effect("hydrates read model from projection tables and computes snapshot sequence", () =>
23 Effect.gen(function* () {
24 const snapshotQuery = yield* ProjectionSnapshotQuery;
25 const sql = yield* SqlClient.SqlClient;
26​
27 yield* sql`DELETE FROM projection_projects`;
28 yield* sql`DELETE FROM projection_state`;
29 yield* sql`DELETE FROM projection_thread_proposed_plans`;
30 yield* sql`DELETE FROM projection_turns`;
31​
32 yield* sql`
33 INSERT INTO projection_projects (
34 project_id,
35 title,
36 workspace_root,
37 default_model_selection_json,
38 scripts_json,
39 created_at,
40 updated_at,
41 deleted_at
42 )
43 VALUES (
44 'project-1',
45 'Project 1',
46 '/tmp/project-1',
47 '{"provider":"codex","model":"gpt-5-codex"}',
48 '[{"id":"script-1","name":"Build","command":"bun run build","icon":"build","runOnWorktreeCreate":false}]',
49 '2026-02-24T00:00:00.000Z',
50 '2026-02-24T00:00:01.000Z',
51 NULL
52 )
53 `;
54​
55 yield* sql`
56 INSERT INTO projection_threads (
57 thread_id,
58 project_id,
59 title,
60 model_selection_json,
61 branch,
62 worktree_path,
63 latest_turn_id,
64 created_at,
65 updated_at,
66 deleted_at
67 )
68 VALUES (
69 'thread-1',
70 'project-1',
71 'Thread 1',
72 '{"provider":"codex","model":"gpt-5-codex"}',
73 NULL,
74 NULL,
75 'turn-1',
76 '2026-02-24T00:00:02.000Z',
77 '2026-02-24T00:00:03.000Z',
78 NULL
79 )
80 `;
81​
82 yield* sql`
83 INSERT INTO projection_thread_messages (
84 message_id,
85 thread_id,
86 turn_id,
87 role,
88 text,
89 is_streaming,
90 created_at,
91 updated_at
92 )
93 VALUES (
94 'message-1',
95 'thread-1',
96 'turn-1',
97 'assistant',
98 'hello from projection',
99 0,
100 '2026-02-24T00:00:04.000Z',
101 '2026-02-24T00:00:05.000Z'
102 )
103 `;
104​
105 yield* sql`
106 INSERT INTO projection_thread_proposed_plans (
107 plan_id,
108 thread_id,
109 turn_id,
110 plan_markdown,
111 implemented_at,
112 implementation_thread_id,
113 created_at,
114 updated_at
115 )
116 VALUES (
117 'plan-1',
118 'thread-1',
119 'turn-1',
120 '# Ship it',
121 '2026-02-24T00:00:05.500Z',
122 'thread-2',
123 '2026-02-24T00:00:05.000Z',
124 '2026-02-24T00:00:05.500Z'
125 )
126 `;
127​
128 yield* sql`
129 INSERT INTO projection_thread_activities (
130 activity_id,
131 thread_id,
132 turn_id,
133 tone,
134 kind,
135 summary,
136 payload_json,
137 created_at
138 )
139 VALUES (
140 'activity-1',
141 'thread-1',
142 'turn-1',
143 'info',
144 'runtime.note',
145 'provider started',
146 '{"stage":"start"}',
147 '2026-02-24T00:00:06.000Z'
148 )
149 `;
150​
151 yield* sql`
152 INSERT INTO projection_thread_sessions (
153 thread_id,
154 status,
155 provider_name,
156 provider_session_id,
157 provider_thread_id,
158 runtime_mode,
159 active_turn_id,
160 last_error,
161 updated_at
162 )
163 VALUES (
164 'thread-1',
165 'running',
166 'codex',
167 'provider-session-1',
168 'provider-thread-1',
169 'approval-required',
170 'turn-1',
171 NULL,
172 '2026-02-24T00:00:07.000Z'
173 )
174 `;
175​
176 yield* sql`
177 INSERT INTO projection_turns (
178 thread_id,
179 turn_id,
180 pending_message_id,
181 source_proposed_plan_thread_id,
182 source_proposed_plan_id,
183 assistant_message_id,
184 state,
185 requested_at,
186 started_at,
187 completed_at,
188 checkpoint_turn_count,
189 checkpoint_ref,
190 checkpoint_status,
191 checkpoint_files_json
192 )
193 VALUES (
194 'thread-1',
195 'turn-1',
196 NULL,
197 'thread-1',
198 'plan-1',
199 'message-1',
200 'completed',
201 '2026-02-24T00:00:08.000Z',
202 '2026-02-24T00:00:08.000Z',
203 '2026-02-24T00:00:08.000Z',
204 1,
205 'checkpoint-1',
206 'ready',
207 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
208 )
209 `;
210​
211 let sequence = 5;
212 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
213 yield* sql`
214 INSERT INTO projection_state (
215 projector,
216 last_applied_sequence,
217 updated_at
218 )
219 VALUES (
220 ${projector},
221 ${sequence},
222 '2026-02-24T00:00:09.000Z'
223 )
224 `;
225 sequence += 1;
226 }
227​
228 const snapshot = yield* snapshotQuery.getSnapshot();
229​
230 assert.equal(snapshot.snapshotSequence, 5);
231 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
232 assert.deepEqual(snapshot.projects, [
233 {
234 id: asProjectId("project-1"),
235 title: "Project 1",
236 workspaceRoot: "/tmp/project-1",
237 defaultModelSelection: {
238 provider: "codex",
239 model: "gpt-5-codex",
240 },
241 scripts: [
242 {
243 id: "script-1",
244 name: "Build",
245 command: "bun run build",
246 icon: "build",
247 runOnWorktreeCreate: false,
248 },
249 ],
250 createdAt: "2026-02-24T00:00:00.000Z",
251 updatedAt: "2026-02-24T00:00:01.000Z",
252 deletedAt: null,
253 },
254 ]);
255 assert.deepEqual(snapshot.threads, [
256 {
257 id: ThreadId.makeUnsafe("thread-1"),
258 projectId: asProjectId("project-1"),
259 title: "Thread 1",
260 modelSelection: {
261 provider: "codex",
262 model: "gpt-5-codex",
263 },
264 interactionMode: "default",
265 runtimeMode: "full-access",
266 branch: null,
267 worktreePath: null,
268 latestTurn: {
269 turnId: asTurnId("turn-1"),
270 state: "completed",
271 requestedAt: "2026-02-24T00:00:08.000Z",
272 startedAt: "2026-02-24T00:00:08.000Z",
273 completedAt: "2026-02-24T00:00:08.000Z",
274 assistantMessageId: asMessageId("message-1"),
275 sourceProposedPlan: {
276 threadId: ThreadId.makeUnsafe("thread-1"),
277 planId: "plan-1",
278 },
279 },
280 createdAt: "2026-02-24T00:00:02.000Z",
281 updatedAt: "2026-02-24T00:00:03.000Z",
282 archivedAt: null,
283 deletedAt: null,
284 messages: [
285 {
286 id: asMessageId("message-1"),
287 role: "assistant",
288 text: "hello from projection",
289 turnId: asTurnId("turn-1"),
290 streaming: false,
291 createdAt: "2026-02-24T00:00:04.000Z",
292 updatedAt: "2026-02-24T00:00:05.000Z",
293 },
294 ],
295 proposedPlans: [
296 {
297 id: "plan-1",
298 turnId: asTurnId("turn-1"),
299 planMarkdown: "# Ship it",
300 implementedAt: "2026-02-24T00:00:05.500Z",
301 implementationThreadId: ThreadId.makeUnsafe("thread-2"),
302 createdAt: "2026-02-24T00:00:05.000Z",
303 updatedAt: "2026-02-24T00:00:05.500Z",
304 },
305 ],
306 activities: [
307 {
308 id: asEventId("activity-1"),
309 tone: "info",
310 kind: "runtime.note",
311 summary: "provider started",
312 payload: { stage: "start" },
313 turnId: asTurnId("turn-1"),
314 createdAt: "2026-02-24T00:00:06.000Z",
315 },
316 ],
317 checkpoints: [
318 {
319 turnId: asTurnId("turn-1"),
320 checkpointTurnCount: 1,
321 checkpointRef: asCheckpointRef("checkpoint-1"),
322 status: "ready",
323 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
324 assistantMessageId: asMessageId("message-1"),
325 completedAt: "2026-02-24T00:00:08.000Z",
326 },
327 ],
328 session: {
329 threadId: ThreadId.makeUnsafe("thread-1"),
330 status: "running",
331 providerName: "codex",
332 runtimeMode: "approval-required",
333 activeTurnId: asTurnId("turn-1"),
334 lastError: null,
335 updatedAt: "2026-02-24T00:00:07.000Z",
336 },
337 },
338 ]);
339 }),
340 );
341});