{"draft":null,"from":18,"to":318,"files":[{"path":"server/test/issues/changes.test.ts","added":121,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/issues/changes.test.ts\n@@ -0,0 +1,121 @@\n+// The change count: one number per project's issues that every write an issue read can\n+// show moves forward, kept by SQLite triggers, so a reader can cache issue reads under it.\n+\n+import assert from \"node:assert/strict\";\n+import { DatabaseSync } from \"node:sqlite\";\n+import { describe, it } from \"node:test\";\n+import { issuesRoute } from \"../../src/issues/routes.ts\";\n+import { IssuesStore, type Sql } from \"../../src/issues/store.ts\";\n+\n+function sql(): { sql: Sql; db: DatabaseSync } {\n+\tconst db = new DatabaseSync(\":memory:\");\n+\treturn {\n+\t\tdb,\n+\t\tsql: {\n+\t\t\texec: (q, ...b) => {\n+\t\t\t\tif (/^\\s*CREATE\\b/i.test(q)) {\n+\t\t\t\t\tdb.exec(q);\n+\t\t\t\t\treturn { toArray: () => [] };\n+\t\t\t\t}\n+\t\t\t\tconst rows = db.prepare(q).all(...(b as (string | number | null)[]));\n+\t\t\t\treturn { toArray: () => rows };\n+\t\t\t},\n+\t\t},\n+\t};\n+}\n+\n+function store(): { s: IssuesStore; db: DatabaseSync; tick: () => void } {\n+\tlet t = Date.parse(\"2026-10-09T10:00:00Z\");\n+\tconst { sql: q, db } = sql();\n+\tIssuesStore.createTables(q);\n+\treturn { s: new IssuesStore(q, () => t), db, tick: () => void (t += 1000) };\n+}\n+\n+const builder = { agent: \"builder3\" };\n+const christopher = { person: \"u1\" };\n+\n+describe(\"issues: the change count\", () => {\n+\tit(\"is kept by a trigger on each counted table, for insert, update and delete\", () => {\n+\t\tconst { db, s } = store();\n+\t\tconst triggers = db\n+\t\t\t.prepare(\"SELECT name FROM sqlite_master WHERE type = 'trigger' AND name LIKE 'changed_%'\")\n+\t\t\t.all();\n+\t\tassert.equal(triggers.length, 30, \"three for each of the ten tables an issue read shows\");\n+\t\tassert.equal(s.changes(), 0);\n+\t});\n+\n+\tit(\"moves forward on every write an issue read shows, and not on reads, drafts or a repeated waiting\", () => {\n+\t\tconst { s, tick } = store();\n+\t\tlet last = s.changes();\n+\t\tconst moved = (what: string) => {\n+\t\t\tconst n = s.changes();\n+\t\t\tassert.ok(n > last, `${what} moved the count (${last} to ${n})`);\n+\t\t\tlast = n;\n+\t\t};\n+\t\tconst still = (what: string) => assert.equal(s.changes(), last, `${what} left the count alone`);\n+\n+\t\tconst a = s.open(builder, { title: \"Sign-in\" }).id;\n+\t\tmoved(\"opening an issue\");\n+\t\tconst b = s.open(builder, { title: \"Logo\", parent: a }).id;\n+\t\tmoved(\"opening a child\");\n+\t\ttick();\n+\t\ts.comment(christopher, a, { text: \"Looks right.\" });\n+\t\tmoved(\"a comment\");\n+\t\tconst q = s.ask(builder, { issue: a, of: \"security\", question: \"Is the cookie set right?\", review: true });\n+\t\tmoved(\"an ask\");\n+\t\ts.waiting(\"security\", \"security\");\n+\t\tmoved(\"waiting marking an ask seen\");\n+\t\ts.waiting(\"security\", \"security\");\n+\t\tstill(\"a second waiting with nothing new\");\n+\t\ts.answer({ agent: \"security\" }, q.id, { words: \"Yes.\" });\n+\t\tmoved(\"an answer\");\n+\t\ts.claim(\"builder3\", b, 12);\n+\t\tmoved(\"a start note's claim\");\n+\t\ts.edit(christopher, b, { labels: [\"design\"] });\n+\t\tmoved(\"a label\");\n+\t\ts.edit(christopher, a, { blockedBy: [b] });\n+\t\tmoved(\"Blocked by\");\n+\t\ts.markDone(builder, b, 381);\n+\t\tmoved(\"done in a version\");\n+\t\tconst c = s.open(builder, { title: \"Footer\" }).id;\n+\t\ts.claim(\"builder3\", c, 13);\n+\t\tlast = s.changes();\n+\t\ts.unclaim(\"builder3\", c);\n+\t\tmoved(\"an unclaim\");\n+\t\ts.drop(builder, c);\n+\t\tmoved(\"a drop\");\n+\t\ts.reopen(builder, c);\n+\t\tmoved(\"a reopen\");\n+\n+\t\ts.page(a);\n+\t\ts.list({});\n+\t\ts.search(\"sign\", 10);\n+\t\ts.waiting();\n+\t\tstill(\"reads\");\n+\t\ts.saveDraft(builder, { title: \"Mine for now\" });\n+\t\ts.wrote(builder);\n+\t\tstill(\"a draft and the rate log\");\n+\t});\n+\n+\tit(\"is a route, and every write answers the count it made\", async () => {\n+\t\tconst { s } = store();\n+\t\tconst call = async (method: string, path: string, body?: unknown) => {\n+\t\t\tconst r = await issuesRoute(\n+\t\t\t\ts,\n+\t\t\t\tnew Request(`https://issues.internal${path}`, {\n+\t\t\t\t\tmethod,\n+\t\t\t\t\t...(body !== undefined ? { body: JSON.stringify(body) } : {}),\n+\t\t\t\t}),\n+\t\t\t);\n+\t\t\treturn { status: r.status, json: (await r.json()) as Record<string, unknown> };\n+\t\t};\n+\t\tassert.deepEqual((await call(\"GET\", \"/p/web/changes\")).json, { changes: 0 });\n+\t\tconst opened = await call(\"POST\", \"/p/web/a/builder3/issue\", { title: \"Sign-in\" });\n+\t\tassert.equal(opened.status, 200);\n+\t\tconst after = (await call(\"GET\", \"/p/web/changes\")).json.changes as number;\n+\t\tassert.ok(after > 0);\n+\t\tassert.equal(opened.json.changes, after, \"the write's answer is the count a read after it sees\");\n+\t\tconst commented = await call(\"POST\", \"/p/web/a/builder3/issue/1/comment\", { text: \"More.\" });\n+\t\tassert.ok((commented.json.changes as number) > after);\n+\t});\n+});"}],"unmatched":[]}