|
| 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 | +it.effect.each([null, "root"])("seeks ordered root errors when node is %s", (nodeId) => |
| 9 | + Effect.gen(function* () { |
| 10 | + const sql = yield* SqlClient.SqlClient; |
| 11 | + yield* runMigrations({ toMigrationInclusive: 61 }); |
| 12 | + yield* sql` |
| 13 | + WITH RECURSIVE items(ordinal) AS ( |
| 14 | + SELECT 1 UNION ALL SELECT ordinal + 1 FROM items WHERE ordinal < 512 |
| 15 | + ) |
| 16 | + INSERT INTO orchestration_v2_projection_turn_items |
| 17 | + (turn_item_id, thread_id, run_id, node_id, ordinal, type, status, updated_at, payload_json) |
| 18 | + SELECT 'command-' || ordinal, 'thread', 'run', ${nodeId}, ordinal, |
| 19 | + 'command_execution', 'completed', '2026-10-01', '{}' |
| 20 | + FROM items |
| 21 | + `; |
| 22 | + yield* sql` |
| 23 | + INSERT INTO orchestration_v2_projection_turn_items |
| 24 | + (turn_item_id, thread_id, run_id, node_id, ordinal, type, status, updated_at, payload_json) |
| 25 | + VALUES |
| 26 | + ('older', 'thread', 'run', ${nodeId}, 513, 'error', 'failed', '2026-10-02', '{}'), |
| 27 | + ('tie-a', 'thread', 'run', ${nodeId}, 514, 'error', 'failed', '2026-10-02', '{}'), |
| 28 | + ('tie-b', 'thread', 'run', ${nodeId}, 514, 'error', 'failed', '2026-10-02', '{}'), |
| 29 | + ('child', 'thread', 'run', 'child', 515, 'error', 'failed', '2026-10-03', '{}'), |
| 30 | + ('other-run', 'thread', 'other', ${nodeId}, 516, 'error', 'failed', '2026-10-03', '{}') |
| 31 | + `; |
| 32 | + yield* runMigrations({ toMigrationInclusive: 62 }); |
| 33 | + const lookup = sql<{ readonly turn_item_id: string }>` |
| 34 | + SELECT turn_item_id FROM orchestration_v2_projection_turn_items |
| 35 | + WHERE thread_id = 'thread' AND run_id = 'run' |
| 36 | + AND node_id IS ${nodeId} AND type = 'error' AND status = 'failed' |
| 37 | + ORDER BY updated_at DESC, ordinal DESC, turn_item_id DESC LIMIT 1 |
| 38 | + `; |
| 39 | + assert.deepEqual(yield* lookup, [{ turn_item_id: "tie-b" }]); |
| 40 | + const [query, parameters] = lookup.compile(); |
| 41 | + const plan = yield* sql.unsafe<{ readonly detail: string }>( |
| 42 | + `EXPLAIN QUERY PLAN ${query}`, |
| 43 | + parameters, |
| 44 | + ); |
| 45 | + assert.isTrue(plan.some((row) => row.detail.includes("turn_items_failed_error_idx"))); |
| 46 | + assert.isFalse(plan.some((row) => row.detail.includes("TEMP B-TREE"))); |
| 47 | + |
| 48 | + yield* sql` |
| 49 | + UPDATE orchestration_v2_projection_turn_items SET status = 'completed' |
| 50 | + WHERE turn_item_id = 'tie-b' |
| 51 | + `; |
| 52 | + assert.deepEqual(yield* lookup, [{ turn_item_id: "tie-a" }]); |
| 53 | + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), |
| 54 | +); |
0 commit comments