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

ProjectionSnapshotQuery.test.ts

292 lines8.3 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_turns`;
30​
31 yield* sql`
32 INSERT INTO projection_projects (
33 project_id,
34 title,
35 workspace_root,
36 default_model,
37 scripts_json,
38 created_at,
39 updated_at,
40 deleted_at
41 )
42 VALUES (
43 'project-1',
44 'Project 1',
45 '/tmp/project-1',
46 'gpt-5-codex',
47 '[{"id":"script-1","name":"Build","command":"bun run build","icon":"build","runOnWorktreeCreate":false}]',
48 '2026-02-24T00:00:00.000Z',
49 '2026-02-24T00:00:01.000Z',
50 NULL
51 )
52 `;
53​
54 yield* sql`
55 INSERT INTO projection_threads (
56 thread_id,
57 project_id,
58 title,
59 model,
60 branch,
61 worktree_path,
62 latest_turn_id,
63 created_at,
64 updated_at,
65 deleted_at
66 )
67 VALUES (
68 'thread-1',
69 'project-1',
70 'Thread 1',
71 'gpt-5-codex',
72 NULL,
73 NULL,
74 'turn-1',
75 '2026-02-24T00:00:02.000Z',
76 '2026-02-24T00:00:03.000Z',
77 NULL
78 )
79 `;
80​
81 yield* sql`
82 INSERT INTO projection_thread_messages (
83 message_id,
84 thread_id,
85 turn_id,
86 role,
87 text,
88 is_streaming,
89 created_at,
90 updated_at
91 )
92 VALUES (
93 'message-1',
94 'thread-1',
95 'turn-1',
96 'assistant',
97 'hello from projection',
98 0,
99 '2026-02-24T00:00:04.000Z',
100 '2026-02-24T00:00:05.000Z'
101 )
102 `;
103​
104 yield* sql`
105 INSERT INTO projection_thread_activities (
106 activity_id,
107 thread_id,
108 turn_id,
109 tone,
110 kind,
111 summary,
112 payload_json,
113 created_at
114 )
115 VALUES (
116 'activity-1',
117 'thread-1',
118 'turn-1',
119 'info',
120 'runtime.note',
121 'provider started',
122 '{"stage":"start"}',
123 '2026-02-24T00:00:06.000Z'
124 )
125 `;
126​
127 yield* sql`
128 INSERT INTO projection_thread_sessions (
129 thread_id,
130 status,
131 provider_name,
132 provider_session_id,
133 provider_thread_id,
134 runtime_mode,
135 active_turn_id,
136 last_error,
137 updated_at
138 )
139 VALUES (
140 'thread-1',
141 'running',
142 'codex',
143 'provider-session-1',
144 'provider-thread-1',
145 'approval-required',
146 'turn-1',
147 NULL,
148 '2026-02-24T00:00:07.000Z'
149 )
150 `;
151​
152 yield* sql`
153 INSERT INTO projection_turns (
154 thread_id,
155 turn_id,
156 pending_message_id,
157 assistant_message_id,
158 state,
159 requested_at,
160 started_at,
161 completed_at,
162 checkpoint_turn_count,
163 checkpoint_ref,
164 checkpoint_status,
165 checkpoint_files_json
166 )
167 VALUES (
168 'thread-1',
169 'turn-1',
170 NULL,
171 'message-1',
172 'completed',
173 '2026-02-24T00:00:08.000Z',
174 '2026-02-24T00:00:08.000Z',
175 '2026-02-24T00:00:08.000Z',
176 1,
177 'checkpoint-1',
178 'ready',
179 '[{"path":"README.md","kind":"modified","additions":2,"deletions":1}]'
180 )
181 `;
182​
183 let sequence = 5;
184 for (const projector of Object.values(ORCHESTRATION_PROJECTOR_NAMES)) {
185 yield* sql`
186 INSERT INTO projection_state (
187 projector,
188 last_applied_sequence,
189 updated_at
190 )
191 VALUES (
192 ${projector},
193 ${sequence},
194 '2026-02-24T00:00:09.000Z'
195 )
196 `;
197 sequence += 1;
198 }
199​
200 const snapshot = yield* snapshotQuery.getSnapshot();
201​
202 assert.equal(snapshot.snapshotSequence, 5);
203 assert.equal(snapshot.updatedAt, "2026-02-24T00:00:09.000Z");
204 assert.deepEqual(snapshot.projects, [
205 {
206 id: asProjectId("project-1"),
207 title: "Project 1",
208 workspaceRoot: "/tmp/project-1",
209 defaultModel: "gpt-5-codex",
210 scripts: [
211 {
212 id: "script-1",
213 name: "Build",
214 command: "bun run build",
215 icon: "build",
216 runOnWorktreeCreate: false,
217 },
218 ],
219 createdAt: "2026-02-24T00:00:00.000Z",
220 updatedAt: "2026-02-24T00:00:01.000Z",
221 deletedAt: null,
222 },
223 ]);
224 assert.deepEqual(snapshot.threads, [
225 {
226 id: ThreadId.makeUnsafe("thread-1"),
227 projectId: asProjectId("project-1"),
228 title: "Thread 1",
229 model: "gpt-5-codex",
230 interactionMode: "default",
231 runtimeMode: "full-access",
232 branch: null,
233 worktreePath: null,
234 latestTurn: {
235 turnId: asTurnId("turn-1"),
236 state: "completed",
237 requestedAt: "2026-02-24T00:00:08.000Z",
238 startedAt: "2026-02-24T00:00:08.000Z",
239 completedAt: "2026-02-24T00:00:08.000Z",
240 assistantMessageId: asMessageId("message-1"),
241 },
242 createdAt: "2026-02-24T00:00:02.000Z",
243 updatedAt: "2026-02-24T00:00:03.000Z",
244 deletedAt: null,
245 messages: [
246 {
247 id: asMessageId("message-1"),
248 role: "assistant",
249 text: "hello from projection",
250 turnId: asTurnId("turn-1"),
251 streaming: false,
252 createdAt: "2026-02-24T00:00:04.000Z",
253 updatedAt: "2026-02-24T00:00:05.000Z",
254 },
255 ],
256 proposedPlans: [],
257 activities: [
258 {
259 id: asEventId("activity-1"),
260 tone: "info",
261 kind: "runtime.note",
262 summary: "provider started",
263 payload: { stage: "start" },
264 turnId: asTurnId("turn-1"),
265 createdAt: "2026-02-24T00:00:06.000Z",
266 },
267 ],
268 checkpoints: [
269 {
270 turnId: asTurnId("turn-1"),
271 checkpointTurnCount: 1,
272 checkpointRef: asCheckpointRef("checkpoint-1"),
273 status: "ready",
274 files: [{ path: "README.md", kind: "modified", additions: 2, deletions: 1 }],
275 assistantMessageId: asMessageId("message-1"),
276 completedAt: "2026-02-24T00:00:08.000Z",
277 },
278 ],
279 session: {
280 threadId: ThreadId.makeUnsafe("thread-1"),
281 status: "running",
282 providerName: "codex",
283 runtimeMode: "approval-required",
284 activeTurnId: asTurnId("turn-1"),
285 lastError: null,
286 updatedAt: "2026-02-24T00:00:07.000Z",
287 },
288 },
289 ]);
290 }),
291 );
292});