The best way to control Claude Code, Codex and more from any computer or phone

main
ts 89 lines 3.68 KB
Raw
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("102_ThreadItemCounts", (it) => {
11 it.effect("matches a recount of items without a run or with an existing run", () =>
12 Effect.gen(function* () {
13 const sql = yield* SqlClient.SqlClient;
14 yield* runMigrations({ toMigrationInclusive: 101 });
15 const upsertItem = (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-11-19T00:00:00.000Z', '{}'
22 )
23 ON CONFLICT(turn_item_id) DO UPDATE SET thread_id = excluded.thread_id, run_id = excluded.run_id
24 `;
25 const insertRun = (runId: string, threadId: string) => sql`
26 INSERT INTO orchestration_v2_projection_runs (
27 run_id, thread_id, ordinal, provider, status, requested_at, payload_json
28 )
29 VALUES (
30 ${runId}, ${threadId},
31 (SELECT COUNT(*) FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId}),
32 'codex', 'completed', '2026-11-19T00:00:00.000Z', '{}'
33 )
34 `;
35 const totals = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
36 SELECT thread_id, item_count FROM orchestration_v2_projection_thread_item_counts
37 ORDER BY thread_id
38 `;
39 const recount = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
40 SELECT item.thread_id, COUNT(*) AS item_count
41 FROM orchestration_v2_projection_turn_items item
42 WHERE item.run_id IS NULL
43 OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs run WHERE run.run_id = item.run_id)
44 GROUP BY item.thread_id
45 ORDER BY item.thread_id
46 `;
47 const assertTotals = (expected: ReadonlyArray<readonly [string, number]>) =>
48 Effect.gen(function* () {
49 const actual = yield* totals();
50 assert.deepEqual(actual, yield* recount());
51 assert.deepEqual(
52 actual.map((row) => [row.thread_id, row.item_count]),
53 expected.map(([threadId, count]) => [threadId, count]),
54 );
55 });
56
57 yield* insertRun("run-1", "thread-1");
58 yield* upsertItem("runless", "thread-1", null);
59 yield* upsertItem("run-1:a", "thread-1", "run-1");
60 yield* upsertItem("late:a", "thread-1", "run-late");
61 yield* runMigrations();
62 yield* assertTotals([["thread-1", 2]]);
63
64 yield* upsertItem("run-1:b", "thread-1", "run-1");
65 yield* upsertItem("late:b", "thread-2", "run-late");
66 yield* assertTotals([["thread-1", 3]]);
67
68 yield* insertRun("run-late", "thread-1");
69 yield* assertTotals([
70 ["thread-1", 4],
71 ["thread-2", 1],
72 ]);
73
74 yield* upsertItem("run-1:b", "thread-2", null);
75 yield* upsertItem("late:a", "thread-1", "run-missing");
76 yield* assertTotals([
77 ["thread-1", 2],
78 ["thread-2", 2],
79 ]);
80
81 yield* sql`DELETE FROM orchestration_v2_projection_runs WHERE run_id = 'run-late'`;
82 yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = 'run-1:b'`;
83 yield* assertTotals([["thread-1", 2]]);
84
85 yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`;
86 yield* assertTotals([]);
87 }),
88 );
89 });