@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 96 lines 4.15 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
3 import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
4 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
5 import * as Effect from "effect/Effect";
6 import * as SqlClient from "effect/sql/SqlClient";
7
8 import { runMigrations } from "../Migrations.ts";
9
10 const createdAt = "2026-10-01T00:00:00.000Z";
11 const quote = serializeAssistantCitation({
12 version: 1,
13 environmentId: EnvironmentId.make("environment:local"),
14 threadId: ThreadId.make("thread:quoted"),
15 messageId: MessageId.make("quoted-answer"),
16 text: "A lighthouse guides ships safely through the darkness.",
17 comment: "what are you talking about",
18 start: 0,
19 end: 54,
20 prefix: "beacon01",
21 suffix: "harbor",
22 });
23 const readable =
24 "A lighthouse guides ships safely through the darkness.\nComment: what are you talking about";
25 const indexed = (sql: SqlClient.SqlClient, phrase: string) =>
26 sql<{ readonly message_id: string }>`
27 SELECT messages.message_id
28 FROM orchestration_v2_message_search AS search
29 INNER JOIN orchestration_v2_projection_messages AS messages
30 ON messages.rowid = search.rowid
31 WHERE orchestration_v2_message_search MATCH ${phrase}
32 `.pipe(Effect.map((rows) => rows.map((row) => row.message_id)));
33
34 it.layer(NodeSqliteClient.layer({ filename: ":memory:" }))(
35 "081_ProjectionMessageSearchText",
36 (it) => {
37 it.effect("backfills readable text only for messages whose citations read differently", () =>
38 Effect.gen(function* () {
39 const sql = yield* SqlClient.SqlClient;
40 yield* runMigrations({ toMigrationInclusive: 80 });
41 const insertMessage = (messageId: string, text: unknown) => sql`
42 INSERT INTO orchestration_v2_projection_messages (
43 message_id, thread_id, role, streaming, created_at, updated_at, payload_json
44 ) VALUES (
45 ${messageId}, 'thread:quote', 'user', 0, ${createdAt}, ${createdAt},
46 ${JSON.stringify({ text })}
47 )
48 `;
49 yield* insertMessage("quoted", `${quote} quiet silver lantern`);
50 yield* insertMessage("plain", "quiet silver lantern");
51 yield* insertMessage("invalid", "[Assistant quote](t3-citation://v1/not-a-citation)");
52 yield* insertMessage("unreadable", 20261001);
53 yield* insertMessage(
54 "accented",
55 serializeAssistantCitation({
56 version: 1,
57 environmentId: EnvironmentId.make("environment:local"),
58 threadId: ThreadId.make("thread:quoted"),
59 messageId: MessageId.make("quoted-answer"),
60 text: "The kittiwake's résumé",
61 start: 0,
62 end: 22,
63 prefix: "",
64 suffix: "",
65 }),
66 );
67 yield* Effect.forEach(
68 Array.from({ length: 501 }, (_, index) => `page-${String(index).padStart(3, "0")}`),
69 (messageId) => insertMessage(messageId, quote),
70 { discard: true },
71 );
72
73 assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 81 }), [
74 [81, "ProjectionMessageSearchText"],
75 ]);
76 const rows = yield* sql<{
77 readonly message_id: string;
78 readonly search_text: string | null;
79 }>`SELECT message_id, search_text FROM orchestration_v2_projection_messages`;
80 const searchText = new Map(rows.map((row) => [row.message_id, row.search_text]));
81 assert.equal(searchText.get("quoted"), `${readable} quiet silver lantern`);
82 assert.isNull(searchText.get("plain"));
83 assert.isNull(searchText.get("invalid"));
84 assert.isNull(searchText.get("unreadable"));
85 assert.equal(searchText.get("page-000"), readable);
86 assert.equal(searchText.get("page-500"), readable);
87
88 // Messages indexed as stored are indexed again as they read.
89 assert.deepEqual(yield* indexed(sql, '"résumé"'), ["accented"]);
90 assert.deepEqual(yield* indexed(sql, `"kittiwake's"`), ["accented"]);
91 assert.deepEqual(yield* indexed(sql, '"beacon01"'), []);
92 assert.lengthOf(yield* indexed(sql, '"what are you talking about"'), 502);
93 }),
94 );
95 },
96 );