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

069_ThreadMessageSearch.test.ts

55 lines2.4 KB

1import { assert, it } from "@effect/vitest";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Effect from "effect/Effect";
4import * as SqlClient from "effect/sql/SqlClient";
5​
6import { runMigrations } from "../Migrations.ts";
7​
8const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
9​
10layer("069_ThreadMessageSearch", (it) => {
11 it.effect("backfills what users wrote and agents answered, then follows deletes", () =>
12 Effect.gen(function* () {
13 const sql = yield* SqlClient.SqlClient;
14 yield* runMigrations({ toMigrationInclusive: 68 });
15 const insert = (
16 id: string,
17 role: string,
18 streaming: number,
19 text: string,
20 createdBy: string | undefined = role === "user" ? "user" : "agent",
21 ) => sql`
22 INSERT INTO orchestration_v2_projection_messages (
23 message_id, thread_id, run_id, node_id, role, streaming, created_at, updated_at, payload_json
24 )
25 VALUES (
26 ${id}, 'thread-1', NULL, NULL, ${role}, ${streaming},
27 '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', ${JSON.stringify({ text, createdBy })}
28 )
29 `;
30 yield* insert("user", "user", 0, "needle from the user");
31 yield* insert("assistant", "assistant", 0, "needle from the agent");
32 yield* insert("streaming", "assistant", 1, "needle still streaming");
33 yield* insert("system", "system", 0, "needle in a system prompt");
34 yield* insert("notice", "user", 0, "needle in a pull request update", "agent");
35 // Older rows carry no author; they were the user's.
36 yield* insert("legacy", "user", 0, "needle from before authors", undefined);
37​
38 yield* runMigrations();
39​
40 const indexed = () =>
41 sql<{ readonly messageId: string }>`
42 SELECT messages.message_id AS "messageId"
43 FROM orchestration_v2_message_search AS search
44 INNER JOIN orchestration_v2_projection_messages AS messages
45 ON messages.rowid = search.rowid
46 WHERE orchestration_v2_message_search MATCH '"needle"'
47 ORDER BY messages.message_id
48 `.pipe(Effect.map((rows) => rows.map((row) => row.messageId)));
49 assert.deepEqual(yield* indexed(), ["assistant", "legacy", "user"]);
50​
51 yield* sql`DELETE FROM orchestration_v2_projection_messages WHERE message_id = 'user'`;
52 assert.deepEqual(yield* indexed(), ["assistant", "legacy"]);
53 }),
54 );
55});