Skip to content
refs
History
51 lines · 2.5 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import type { Actor } from "../contract/actor.ts";
4import { hinting, pullStream, recordEvent, repoStream, taskStream, type Hint } from "../events.ts";
5import { d1, migrated } from "./d1.ts";
6
7const human: Actor = { kind: "human", person: { id: "u1", name: "Aman", email: "a@example.com", role: "owner" }, via: "session" };
8
9function setup() {
10 const sqlite = migrated();
11 sqlite.exec("INSERT INTO tasks (id, repo_id, number, title, body, author_id, created_at, updated_at) VALUES (5, 'r1', 1, 't', '', 'u1', 0, 0)");
12 sqlite.exec("INSERT INTO pulls (id, repo_id, number, title, author_id, base_ref, head_ref, created_at, updated_at) VALUES (9, 'r2', 1, 't', 'u1', 'main', 'tasks/1', 0, 0)");
13 const hints: Hint[] = [];
14 return { db: hinting(d1(sqlite), (hint) => hints.push(hint)), hints, sqlite };
15}
16
17test("a committed batch hints each stream's repository once, with its newest event", async () => {
18 const { db, hints } = setup();
19 const [first] = await db.batch([
20 recordEvent(db, human, taskStream(5), "task.renamed", { from: "a", to: "b" }, 0),
21 recordEvent(db, human, taskStream(5), "task.closed", {}, 0),
22 db.prepare("UPDATE tasks SET title = 'b' WHERE id = 5"),
23 recordEvent(db, human, pullStream(9), "pull.closed", {}, 0),
24 recordEvent(db, human, "org", "invitation.revoked", { email: "x@example.com" }, 0),
25 ]);
26 assert.equal(first.meta.last_row_id, 1);
27 assert.deepEqual(hints, [
28 { latestId: 2, stream: "task:5", repoId: "r1" },
29 { latestId: 3, stream: "pull:9", repoId: "r2" },
30 ]);
31});
32
33test("a single recorded event hints after it runs", async () => {
34 const { db, hints } = setup();
35 const statement = recordEvent(db, human, repoStream("r3"), "repo.updated", { description: "x" }, 0);
36 assert.deepEqual(hints, []);
37 await statement.run();
38 assert.deepEqual(hints, [{ latestId: 1, stream: "repo:r3", repoId: "r3" }]);
39});
40
41test("a failed batch hints nothing", async () => {
42 const { db, hints } = setup();
43 await assert.rejects(db.batch([recordEvent(db, human, taskStream(5), "task.closed", {}, 0), db.prepare("INSERT INTO nowhere VALUES (1)")]));
44 assert.deepEqual(hints, []);
45});
46
47test("reading an event statement with first() fails instead of skipping its hint", async () => {
48 const { db, hints } = setup();
49 await assert.rejects(recordEvent(db, human, repoStream("r4"), "repo.updated", { description: "x" }, 0).first(), /run\(\) or db\.batch\(\)/);
50 assert.deepEqual(hints, []);
51});