|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 5 | + |
| 6 | +import { runMigrations } from "../Migrations.ts"; |
| 7 | + |
| 8 | +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 9 | + |
| 10 | +layer("076_TurnItemCounts", (it) => { |
| 11 | + it.effect("backfills item counts, then follows upserts, moves, and deletes", () => |
| 12 | + Effect.gen(function* () { |
| 13 | + const sql = yield* SqlClient.SqlClient; |
| 14 | + yield* runMigrations({ toMigrationInclusive: 75 }); |
| 15 | + const upsert = (id: string, threadId: string, runId: string | null) => sql` |
| 16 | + INSERT INTO orchestration_v2_projection_turn_items ( |
| 17 | + turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json |
| 18 | + ) |
| 19 | + VALUES ( |
| 20 | + ${id}, ${threadId}, ${runId}, 1, 'assistant_message', 'completed', |
| 21 | + '2026-10-29T00:00:00.000Z', '{}' |
| 22 | + ) |
| 23 | + ON CONFLICT(turn_item_id) DO UPDATE SET |
| 24 | + thread_id = excluded.thread_id, |
| 25 | + run_id = excluded.run_id, |
| 26 | + status = excluded.status |
| 27 | + `; |
| 28 | + yield* upsert("runless-1", "thread-1", null); |
| 29 | + yield* upsert("runless-2", "thread-1", null); |
| 30 | + yield* upsert("run-1", "thread-1", "run-1"); |
| 31 | + |
| 32 | + yield* runMigrations(); |
| 33 | + |
| 34 | + const counts = () => |
| 35 | + sql<{ readonly thread_id: string; readonly run_key: string; readonly item_count: number }>` |
| 36 | + SELECT thread_id, run_key, item_count |
| 37 | + FROM orchestration_v2_projection_turn_item_counts |
| 38 | + ORDER BY thread_id, run_key |
| 39 | + `; |
| 40 | + assert.deepEqual(yield* counts(), [ |
| 41 | + { thread_id: "thread-1", run_key: "", item_count: 2 }, |
| 42 | + { thread_id: "thread-1", run_key: "run-1", item_count: 1 }, |
| 43 | + ]); |
| 44 | + |
| 45 | + yield* upsert("run-1", "thread-1", "run-1"); |
| 46 | + yield* upsert("run-2", "thread-2", "run-2"); |
| 47 | + yield* upsert("runless-2", "thread-1", "run-1"); |
| 48 | + assert.deepEqual(yield* counts(), [ |
| 49 | + { thread_id: "thread-1", run_key: "", item_count: 1 }, |
| 50 | + { thread_id: "thread-1", run_key: "run-1", item_count: 2 }, |
| 51 | + { thread_id: "thread-2", run_key: "run-2", item_count: 1 }, |
| 52 | + ]); |
| 53 | + |
| 54 | + yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`; |
| 55 | + assert.deepEqual(yield* counts(), [ |
| 56 | + { thread_id: "thread-2", run_key: "run-2", item_count: 1 }, |
| 57 | + ]); |
| 58 | + }), |
| 59 | + ); |
| 60 | +}); |
0 commit comments