The best way to control Claude Code, Codex and more from any computer or phone
| 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 ordinalsOf = (threadId: string) => ` |
| 9 | SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal |
| 10 | FROM orchestration_v2_projection_turn_items |
| 11 | WHERE thread_id = '${threadId}' AND type IN ('user_message', 'assistant_message') |
| 12 | AND json_valid(payload_json) |
| 13 | GROUP BY json_extract(payload_json, '$.messageId') |
| 14 | ORDER BY message_id |
| 15 | `; |
| 16 | |
| 17 | const insertTurnItem = ( |
| 18 | id: string, |
| 19 | threadId: string, |
| 20 | ordinal: number, |
| 21 | type: string, |
| 22 | payloadJson: string, |
| 23 | ) => |
| 24 | Effect.gen(function* () { |
| 25 | const sql = yield* SqlClient.SqlClient; |
| 26 | yield* sql` |
| 27 | INSERT INTO orchestration_v2_projection_turn_items ( |
| 28 | turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json |
| 29 | ) |
| 30 | VALUES ( |
| 31 | ${id}, ${threadId}, NULL, ${ordinal}, ${type}, 'completed', |
| 32 | '2026-11-01T00:00:00.000Z', ${payloadJson} |
| 33 | ) |
| 34 | `; |
| 35 | }); |
| 36 | |
| 37 | // Without the index a payload that is not JSON fails no write; with it, it must fail none either. |
| 38 | it.effect("migrates over a message whose payload is not JSON, and takes one after it", () => |
| 39 | Effect.gen(function* () { |
| 40 | const sql = yield* SqlClient.SqlClient; |
| 41 | yield* runMigrations({ toMigrationInclusive: 93 }); |
| 42 | yield* insertTurnItem("bad-1", "thread-a", 1, "assistant_message", "{not json"); |
| 43 | yield* insertTurnItem("ok-1", "thread-a", 2, "user_message", '{"messageId":"m1"}'); |
| 44 | |
| 45 | assert.deepEqual( |
| 46 | (yield* runMigrations({ toMigrationInclusive: 94 })).map(([id]) => id), |
| 47 | [94], |
| 48 | ); |
| 49 | yield* insertTurnItem("bad-2", "thread-a", 3, "user_message", "{not json"); |
| 50 | yield* insertTurnItem("ok-2", "thread-a", 4, "assistant_message", '{"messageId":"m1"}'); |
| 51 | yield* sql` |
| 52 | UPDATE orchestration_v2_projection_turn_items SET payload_json = '{still not json' |
| 53 | WHERE turn_item_id = 'ok-2' |
| 54 | `; |
| 55 | |
| 56 | assert.deepEqual(yield* sql.unsafe(ordinalsOf("thread-a")), [{ message_id: "m1", ordinal: 2 }]); |
| 57 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), |
| 58 | ); |