{"draft":null,"from":4,"to":318,"files":[{"path":"server/test/issues/issues.test.ts","added":630,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/issues/issues.test.ts\n@@ -0,0 +1,630 @@\n+// Issues: the store and its routes, on node:sqlite as a Durable Object's SQLite.\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, WRITES_PER_MINUTE } from \"../../src/issues/store.ts\";\n+\n+function sql(): Sql {\n+\tconst db = new DatabaseSync(\":memory:\");\n+\treturn {\n+\t\texec: (q, ...b) => {\n+\t\t\t// Like ctx.storage.sql.exec: the statement runs at once; rows come from toArray.\n+\t\t\tif (/^\\s*CREATE\\b/i.test(q)) {\n+\t\t\t\tdb.exec(q);\n+\t\t\t\treturn { toArray: () => [] };\n+\t\t\t}\n+\t\t\tconst rows = db.prepare(q).all(...(b as (string | number | null)[]));\n+\t\t\treturn { toArray: () => rows };\n+\t\t},\n+\t};\n+}\n+\n+function store(): { s: IssuesStore; tick: (ms?: number) => void } {\n+\tlet t = Date.parse(\"2026-10-07T20:00:00Z\");\n+\tconst db = sql();\n+\tIssuesStore.createTables(db);\n+\treturn { s: new IssuesStore(db, () => t), tick: (ms = 1000) => void (t += ms) };\n+}\n+\n+const builder = { agent: \"builder3\" };\n+const christopher = { person: \"u1\" };\n+\n+describe(\"issues: statuses come from the work\", () => {\n+\tit(\"open, in progress, in review, blocked, done and dropped, and a dropped issue can be reopened\", () => {\n+\t\tconst { s, tick } = store();\n+\t\tconst a = s.open(builder, { title: \"Sign-in page\", description: \"Pages for sign-in\" }).id;\n+\t\tconst b = s.open(builder, { title: \"Logo\", parent: a }).id;\n+\t\tassert.equal(s.page(a).issue.status, \"open\");\n+\t\tassert.equal(s.page(a).issue.children, 1);\n+\n+\t\ttick();\n+\t\ts.claim(\"builder3\", a, 4400);\n+\t\tassert.equal(s.page(a).issue.status, \"in progress\");\n+\t\tassert.deepEqual(s.page(a).issue.agents, [\"builder3\"]);\n+\n+\t\ttick();\n+\t\tconst review = s.ask(builder, { issue: a, of: \"security\", question: \"Is the cookie set right?\", review: true });\n+\t\tassert.equal(s.page(a).issue.status, \"in review\");\n+\t\tassert.deepEqual(s.page(a).issue.waitingOn, [\"security\"]);\n+\t\tassert.deepEqual(review.news.to, [\"security\"]);\n+\t\tassert.ok(review.news.text.endsWith(`: \"Is the cookie set right?\" Answer: abso answer ${review.id} \"...\"`));\n+\n+\t\t// A question that isn't a review blocks, and outranks the review; it can hold up other issues too.\n+\t\tconst q = s.ask(builder, {\n+\t\t\tissue: a,\n+\t\t\tof: \"person:u1\",\n+\t\t\tquestion: \"Take abso.dev?\",\n+\t\t\toptions: [\"Yes\", \"No\"],\n+\t\t\tblocks: [b],\n+\t\t\tdue: \"2026-10-09\",\n+\t\t});\n+\t\tassert.equal(s.page(a).issue.status, \"blocked\");\n+\t\tassert.equal(s.page(b).issue.status, \"blocked\", \"an ask holds up the issues it names in blocks\");\n+\t\tassert.deepEqual(s.page(a).issue.waitingOn, [\"security\", \"person:u1\"]);\n+\t\tassert.ok(q.news.text.endsWith('\"Take abso.dev?\"'), \"a person answers elsewhere, so no command\");\n+\n+\t\ttick();\n+\t\tconst answered = s.answer(christopher, q.id, { option: \"Yes\", note: \"Renew it for two years.\" });\n+\t\tassert.equal(answered.ask.answer?.words, \"Yes\\n\\nRenew it for two years.\", \"kept in the person's own words\");\n+\t\tassert.equal(s.page(b).issue.status, \"open\");\n+\t\tassert.equal(s.page(a).issue.status, \"in review\");\n+\t\ts.answer({ agent: \"security\" }, review.id, { words: \"Looks right.\" });\n+\t\tassert.equal(s.page(a).issue.status, \"in progress\");\n+\n+\t\ttick();\n+\t\tconst done = s.done(\"builder3\", a, 13);\n+\t\tassert.match(done.news!.text, /^builder3 finished issue 1 \\(\"Sign-in page\"\\) in version 13$/);\n+\t\tconst page = s.page(a);\n+\t\tassert.equal(page.issue.status, \"done\");\n+\t\tassert.deepEqual(page.issue.closed, { how: \"done\", by: \"builder3\", version: 13, at: page.issue.closed!.at });\n+\t\tassert.deepEqual(page.issue.versions, [13]);\n+\t\tassert.deepEqual(page.claims, []);\n+\n+\t\ts.drop(builder, b);\n+\t\tassert.equal(s.page(b).issue.status, \"dropped\");\n+\t\tassert.throws(() => s.drop(builder, b), /dropped already/);\n+\t\tassert.throws(() => s.markDone(builder, b, null), /reopen it/);\n+\t\tconst reopened = s.reopen(builder, b);\n+\t\tassert.match(reopened.news.text, /reopened issue/);\n+\t\tassert.equal(s.page(b).issue.status, \"open\");\n+\t\tassert.throws(() => s.reopen(builder, b), /is open/);\n+\t\tconst marked = s.markDone(builder, b, 381);\n+\t\tassert.match(marked.news.text, /done in version 381/);\n+\t\tassert.equal(s.page(b).issue.status, \"done\");\n+\t\tassert.deepEqual(s.page(b).issue.versions, [381]);\n+\t\tassert.throws(() => s.reopen(builder, b), /stays done/);\n+\t\tassert.throws(() => s.markDone(builder, b, null), /done already/);\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ open: true }).map((i) => i.id),\n+\t\t\t[],\n+\t\t);\n+\t});\n+\n+\tit(\"a start note that ends without --done takes the claim back and leaves the issue open\", () => {\n+\t\tconst { s } = store();\n+\t\tconst id = s.open(builder, { title: \"x\" }).id;\n+\t\ts.claim(\"builder3\", id, 10);\n+\t\ts.unclaim(\"builder3\", id);\n+\t\tassert.equal(s.page(id).issue.status, \"open\");\n+\t\t// Hooks for an issue that doesn't exist are ignored: the start note stands either way.\n+\t\ts.claim(\"builder3\", 99, 10);\n+\t\ts.unclaim(\"builder3\", 99);\n+\t\tassert.deepEqual(s.done(\"builder3\", 99, 3), {});\n+\t});\n+\n+\tit(\"lists by status and agent, most recently updated first\", () => {\n+\t\tconst { s, tick } = store();\n+\t\tconst one = s.open(builder, { title: \"one\" }).id;\n+\t\ttick();\n+\t\tconst two = s.open(builder, { title: \"two\" }).id;\n+\t\ttick();\n+\t\ts.comment({ agent: \"engine\" }, one, { text: \"on it soon\" });\n+\t\tassert.deepEqual(\n+\t\t\ts.list().map((i) => i.id),\n+\t\t\t[one, two],\n+\t\t);\n+\t\ts.claim(\"engine\", two, 5);\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ agent: \"engine\" }).map((i) => i.id),\n+\t\t\t[two],\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ status: \"open\" }).map((i) => i.id),\n+\t\t\t[one],\n+\t\t);\n+\t});\n+});\n+\n+describe(\"issues: the asker withdraws an ask\", () => {\n+\tit(\"closes like an answer: off the waiting list, no longer blocking, kept with the note, and the one asked hears it\", () => {\n+\t\tconst { s } = store();\n+\t\tconst id = s.open(builder, { title: \"How judges see abso\" }).id;\n+\t\tconst ask = s.ask(builder, { of: \"person:u1\", question: \"A, B or C?\", issue: id, options: [\"A\", \"B\", \"C\"] });\n+\t\tassert.equal(s.page(id).issue.status, \"blocked\");\n+\t\tconst r = s.withdraw(builder, ask.id, { note: \"Overtaken by tonight's rulings\" });\n+\t\tassert.equal(r.ask.outcome, \"withdrawn\");\n+\t\tassert.deepEqual(r.ask.answer && { by: r.ask.answer.by, words: r.ask.answer.words }, {\n+\t\t\tby: \"builder3\",\n+\t\t\twords: \"Overtaken by tonight's rulings\",\n+\t\t});\n+\t\tassert.deepEqual(r.news.to, [\"person:u1\"]);\n+\t\tassert.match(r.news.text, /^builder3 withdrew their question of person:u1 on issue \\d+: \"Overtaken/);\n+\t\tassert.deepEqual(s.waiting(\"person:u1\"), []);\n+\t\tassert.equal(s.page(id).issue.status, \"open\");\n+\t\tassert.equal(s.page(id).asks[0]!.outcome, \"withdrawn\");\n+\t\tassert.equal(s.answered(\"person:u1\")[0]!.outcome, \"withdrawn\");\n+\t});\n+\n+\tit(\"only the asker withdraws, only while unanswered, and once; then it can't be answered\", () => {\n+\t\tconst { s } = store();\n+\t\tconst ask = s.ask(builder, { of: \"person:u1\", question: \"Pick one\" });\n+\t\tassert.throws(() => s.withdraw(christopher, ask.id, {}), /asked by builder3; only they can withdraw it/);\n+\t\tassert.throws(() => s.withdraw(builder, 99, {}), /there's no ask 99/);\n+\t\tconst r = s.withdraw(builder, ask.id, {});\n+\t\tassert.equal(r.ask.answer!.words, \"\");\n+\t\tassert.equal(r.news.text, \"builder3 withdrew their question of person:u1\", \"no note, so nothing after it\");\n+\t\tassert.throws(() => s.withdraw(builder, ask.id, {}), /withdrawn already/);\n+\t\tassert.throws(\n+\t\t\t() => s.answer(christopher, ask.id, { words: \"A\" }),\n+\t\t\t(e: { status?: number; message: string }) => e.status === 409 && /withdrawn by builder3/.test(e.message),\n+\t\t);\n+\t\tconst answered = s.ask(builder, { of: \"person:u1\", question: \"Another\" });\n+\t\ts.answer(christopher, answered.id, { words: \"Yes\" });\n+\t\tassert.throws(() => s.withdraw(builder, answered.id, {}), /answered already, so there's nothing to withdraw/);\n+\t\tassert.equal(s.answered(\"person:u1\").find((a) => a.id === answered.id)!.outcome, undefined);\n+\t});\n+});\n+\n+describe(\"issues: the one asked acknowledges an ask\", () => {\n+\tit(\"stays open and blocking, shows the ack and its note, and the asker hears it; an answer closes it\", () => {\n+\t\tconst { s } = store();\n+\t\tconst id = s.open(builder, { title: \"Which deploy first\" }).id;\n+\t\tconst ask = s.ask(builder, { of: \"person:u1\", question: \"A or B?\", issue: id });\n+\t\tconst r = s.ack(christopher, ask.id, { note: \"after the review\" });\n+\t\tassert.equal(r.ask.ack!.note, \"after the review\");\n+\t\tassert.equal(r.ask.answer, null);\n+\t\tassert.deepEqual(r.news.to, [\"builder3\"]);\n+\t\tassert.equal(\n+\t\t\tr.news.text,\n+\t\t\t`person:u1 has your question (ask ${ask.id}) on issue ${id} and will answer: \"after the review\"`,\n+\t\t);\n+\t\tassert.equal(s.page(id).issue.status, \"blocked\");\n+\t\tassert.equal(s.page(id).asks[0]!.ack!.note, \"after the review\");\n+\t\tassert.deepEqual(\n+\t\t\ts.waiting(\"person:u1\").map((a) => a.id),\n+\t\t\t[ask.id],\n+\t\t);\n+\t\t// Again, with no note: the note and time update.\n+\t\tconst again = s.ack(christopher, ask.id, {});\n+\t\tassert.equal(again.ask.ack!.note, \"\");\n+\t\tassert.equal(again.news.text, `person:u1 has your question (ask ${ask.id}) on issue ${id} and will answer.`);\n+\t\ts.answer(christopher, ask.id, { words: \"A\" });\n+\t\tassert.equal(s.page(id).issue.status, \"open\");\n+\t\tassert.deepEqual(s.waiting(\"person:u1\"), []);\n+\t});\n+\n+\tit(\"only the one asked acknowledges, and only while it's open\", () => {\n+\t\tconst { s } = store();\n+\t\tconst ask = s.ask(builder, { of: \"person:u1\", question: \"Pick one\" });\n+\t\tassert.throws(\n+\t\t\t() => s.ack(builder, ask.id, {}),\n+\t\t\t(e: { status?: number; message: string }) => e.status === 403 && /is for person:u1, not builder3/.test(e.message),\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() => s.ack(christopher, 99, {}),\n+\t\t\t(e: { status?: number }) => e.status === 404,\n+\t\t);\n+\t\ts.answer(christopher, ask.id, { words: \"A\" });\n+\t\tassert.throws(\n+\t\t\t() => s.ack(christopher, ask.id, {}),\n+\t\t\t(e: { status?: number; message: string }) => e.status === 409 && /answered already/.test(e.message),\n+\t\t);\n+\t\tconst gone = s.ask(builder, { of: \"person:u1\", question: \"Another\" });\n+\t\ts.withdraw(builder, gone.id, {});\n+\t\tassert.throws(\n+\t\t\t() => s.ack(christopher, gone.id, {}),\n+\t\t\t(e: { status?: number; message: string }) => e.status === 409 && /withdrawn by builder3/.test(e.message),\n+\t\t);\n+\t});\n+});\n+\n+describe(\"issues: asks and answers\", () => {\n+\tit(\"an ask on no issue, a task answered done, and waiting sorted by due date\", () => {\n+\t\tconst { s } = store();\n+\t\tconst id = s.open(builder, { title: \"Sign-in\" }).id;\n+\t\tconst later = s.ask(builder, {\n+\t\t\tof: \"person:u1\",\n+\t\t\tquestion: \"Does the avcs command become abso?\",\n+\t\t\tdue: \"2026-10-20\",\n+\t\t\trecommendation: { text: \"Yes, with avcs kept as an alias\" },\n+\t\t});\n+\t\tconst sooner = s.ask(builder, {\n+\t\t\tof: \"person:u1\",\n+\t\t\tkind: \"task\",\n+\t\t\tquestion: \"Upload the sign-in logo\",\n+\t\t\tdue: \"2026-10-08\",\n+\t\t\tblocks: [id],\n+\t\t});\n+\t\tconst none = s.ask(builder, { of: \"person:u1\", question: \"Anything else?\" });\n+\t\tconst waiting = s.waiting(\"person:u1\");\n+\t\tassert.deepEqual(\n+\t\t\twaiting.map((a) => a.id),\n+\t\t\t[sooner.id, later.id, none.id],\n+\t\t);\n+\t\tassert.equal(waiting[1]!.issue, null);\n+\t\tassert.equal(waiting[1]!.title, null);\n+\t\tassert.deepEqual(waiting[1]!.recommendation, { text: \"Yes, with avcs kept as an alias\", by: \"builder3\" });\n+\t\tassert.equal(s.page(id).issue.status, \"blocked\");\n+\t\ts.answer(christopher, sooner.id, { done: true });\n+\t\tassert.equal(s.answered(\"person:u1\")[0]!.answer!.words, \"done\");\n+\t\tassert.equal(s.page(id).issue.status, \"open\");\n+\t\tassert.deepEqual(\n+\t\t\ts.waiting(\"person:u1\").map((a) => a.id),\n+\t\t\t[later.id, none.id],\n+\t\t);\n+\t});\n+\n+\tit(\"only the one asked answers, once, from its options\", () => {\n+\t\tconst { s } = store();\n+\t\tconst ask = s.ask(builder, { of: \"person:u1\", question: \"Pick one\", options: [\"A\", \"B\"] });\n+\t\tassert.throws(() => s.answer({ person: \"u2\" }, ask.id, { words: \"A\" }), /is for person:u1, not person:u2/);\n+\t\tassert.throws(() => s.answer(christopher, ask.id, { option: \"C\" }), /options are: \"A\", \"B\"/);\n+\t\t// The page sends the option's text as it showed it: an index that names another is refused.\n+\t\tassert.throws(\n+\t\t\t() => s.answer(christopher, ask.id, { option: 1, expect: \"A\" }),\n+\t\t\t(e: { status?: number; message: string }) =>\n+\t\t\t\te.status === 409 && /option 2 is \"B\", not the one you picked/.test(e.message),\n+\t\t);\n+\t\ts.answer(christopher, ask.id, { option: 1, expect: \"B\" });\n+\t\tassert.throws(() => s.answer(christopher, ask.id, { words: \"A\" }), /answered already/);\n+\t\tassert.equal(s.answered(\"person:u1\")[0]!.answer!.words, \"B\");\n+\t});\n+\n+\tit(\"checks its input\", () => {\n+\t\tconst { s } = store();\n+\t\tassert.throws(() => s.open(builder, { title: \"\" }), /the title is missing/);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\".repeat(201) }), /201 characters; the most is 200/);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\", parent: 7 }), /there's no issue 7/);\n+\t\tassert.throws(\n+\t\t\t() => s.open(builder, { title: \"x\", lines: { file: \"a.ts\", from: 3, to: 1, version: 2 } }),\n+\t\t\t/lines are/,\n+\t\t);\n+\t\tassert.throws(() => s.ask(builder, { of: \"x\", question: \"q\", review: true }), /a review ask is on an issue/);\n+\t\tassert.throws(() => s.ask(builder, { of: \"x\", question: \"q\", due: \"soon\" }), /due is a date/);\n+\t\tassert.throws(() => s.ask(builder, { of: \"x\", question: \"q\", kind: \"chore\" }), /kind is \"question\", or \"task\"/);\n+\t});\n+});\n+\n+describe(\"issues: routes\", () => {\n+\tconst call = (s: IssuesStore, method: string, path: string, body?: unknown, headers: Record<string, string> = {}) =>\n+\t\tissuesRoute(\n+\t\t\ts,\n+\t\t\tnew Request(`https://issues.internal${path}`, {\n+\t\t\t\tmethod,\n+\t\t\t\theaders,\n+\t\t\t\t...(body !== undefined ? { body: JSON.stringify(body) } : {}),\n+\t\t\t}),\n+\t\t).then(async (r) => ({ status: r.status, json: (await r.json()) as Record<string, any> }));\n+\n+\tit(\"agents write under /a/:agent/, a write naming nobody is refused, and everyone reads\", async () => {\n+\t\tconst { s } = store();\n+\t\tconst opened = await call(s, \"POST\", \"/p/web/a/builder3/issue\", { title: \"Sign-in\", description: \"d\" });\n+\t\tassert.equal(opened.status, 200);\n+\t\tassert.equal(opened.json.id, 1);\n+\t\tassert.equal((await call(s, \"POST\", \"/p/web/a/fe/issue/1/comment\", { text: \"nice\" })).status, 200);\n+\t\tassert.equal((await call(s, \"POST\", \"/p/web/issue/1/comment\", { text: \"who am I\" })).status, 403);\n+\t\tassert.equal(\n+\t\t\t(await call(s, \"POST\", \"/p/web/issue/1/comment\", { text: \"a person?\" }, { \"X-Avcs-User\": \"u1\" })).status,\n+\t\t\t403,\n+\t\t\t\"no header makes a person here\",\n+\t\t);\n+\t\tconst asked = await call(s, \"POST\", \"/p/web/a/builder3/ask\", { issue: 1, of: \"fe\", question: \"OK?\" });\n+\t\tassert.equal(asked.json.news.to[0], \"fe\");\n+\t\tassert.equal((await call(s, \"POST\", `/p/web/a/fe/answer/${asked.json.id}`, { words: \"OK\" })).status, 200);\n+\n+\t\tconst list = await call(s, \"GET\", \"/p/web/issues?open=1\");\n+\t\tassert.deepEqual(\n+\t\t\tlist.json.issues.map((i: { id: number; status: string }) => [i.id, i.status]),\n+\t\t\t[[1, \"open\"]],\n+\t\t);\n+\t\tconst page = await call(s, \"GET\", \"/p/web/issue/1\");\n+\t\tassert.deepEqual(\n+\t\t\tpage.json.comments.map((c: { by: string }) => c.by),\n+\t\t\t[\"fe\"],\n+\t\t);\n+\t\tassert.equal(page.json.asks[0].answer.words, \"OK\");\n+\t\tassert.equal((await call(s, \"GET\", \"/p/web/issue/9\")).status, 404);\n+\t\tassert.equal((await call(s, \"GET\", \"/p/web/answered\")).status, 400);\n+\t\tassert.equal((await call(s, \"POST\", \"/p/web/a/builder3/issue\", \"not an object\")).status, 400);\n+\t});\n+});\n+\n+describe(\"issues: edits, watchers, Blocked by and drafts\", () => {\n+\tit(\"an edit tells the watchers by default, adds people, or tells nobody\", () => {\n+\t\tconst { s, tick } = store();\n+\t\tconst a = s.open(christopher, { title: \"Accounts\" }).id;\n+\t\tconst b = s.open(christopher, { title: \"Sign-in\", agent: \"fe\", notify: [\"design\"] });\n+\t\tassert.deepEqual(b.news, {\n+\t\t\tissue: null,\n+\t\t\ttext: 'person:u1 opened issue 2: \"Sign-in\" (Agent fe)',\n+\t\t\tto: [\"fe\", \"design\"],\n+\t\t});\n+\t\ts.comment(builder, b.id, { text: \"on it soon\" });\n+\t\ts.claim(\"engine\", b.id, 40);\n+\t\ttick();\n+\t\t// Issue 1 opened before issue 2, then put under it.\n+\t\tconst moved = s.edit(christopher, b.id, { parent: a, title: \"Sign-in flow\" });\n+\t\tassert.deepEqual(moved.notified, [\"builder3\", \"design\", \"engine\", \"fe\"]);\n+\t\tassert.equal(moved.news!.text, 'person:u1 edited issue 2 (\"Sign-in flow\"): title was \"Sign-in\"; parent is issue 1');\n+\t\tconst page = s.page(b.id);\n+\t\tassert.equal(page.issue.parent, a);\n+\t\tassert.deepEqual(page.issue.watchers, [\"builder3\", \"design\", \"engine\", \"fe\", \"person:u1\"]);\n+\t\tassert.deepEqual(page.edits[0]!.changes, {\n+\t\t\ttitle: { from: \"Sign-in\", to: \"Sign-in flow\" },\n+\t\t\tparent: { from: null, to: 1 },\n+\t\t});\n+\t\tconst quiet = s.edit(builder, b.id, { description: \"typo\", notify: false });\n+\t\tassert.deepEqual(quiet, { edit: 2, notified: [] });\n+\t\tconst more = s.edit(builder, b.id, { agent: \"web\", also: [\"person:u2\"] });\n+\t\tassert.ok(\n+\t\t\tmore.notified.includes(\"person:u2\") && more.notified.includes(\"web\") && !more.notified.includes(\"builder3\"),\n+\t\t);\n+\t\tassert.equal(s.page(b.id).issue.agent, \"web\");\n+\t\tassert.throws(() => s.edit(builder, b.id, { agent: \"web\" }), /that changes nothing on issue 2/);\n+\t\tassert.throws(() => s.edit(builder, a, { parent: b.id }), /issue 2 is issue 1 or under it/);\n+\t\tassert.throws(() => s.edit(builder, a, { notify: \"no\", title: \"A\" }), /notify is true/);\n+\t});\n+\n+\tit(\"Blocked by holds an issue Blocked until each is Done, and refuses a loop\", () => {\n+\t\tconst { s } = store();\n+\t\tconst a = s.open(builder, { title: \"Schema\" }).id;\n+\t\tconst b = s.open(builder, { title: \"Pages\", blockedBy: [a] }).id;\n+\t\tassert.equal(s.page(b).issue.status, \"blocked\");\n+\t\tassert.deepEqual(s.page(b).issue.blockedBy, [a]);\n+\t\tassert.throws(() => s.edit(builder, a, { blockedBy: [b] }), /issue 2 already waits on issue 1/);\n+\t\tassert.throws(() => s.edit(builder, a, { blockedBy: [a] }), /can't be blocked by itself/);\n+\t\ts.done(\"engine\", a, 12);\n+\t\tassert.equal(s.page(b).issue.status, \"open\");\n+\t\tconst c = s.open(builder, { title: \"Docs\" }).id;\n+\t\ts.edit(builder, c, { blockedBy: [b] });\n+\t\tassert.equal(s.page(c).issue.status, \"blocked\");\n+\t\ts.edit(builder, c, { blockedBy: [] });\n+\t\tassert.equal(s.page(c).issue.status, \"open\");\n+\t});\n+\n+\tit(\"the first start note sets the Agent when none was set, and the claimer watches\", () => {\n+\t\tconst { s } = store();\n+\t\tconst id = s.open(builder, { title: \"x\" }).id;\n+\t\ts.claim(\"engine\", id, 3);\n+\t\ts.claim(\"fe\", id, 4);\n+\t\tassert.equal(s.page(id).issue.agent, \"engine\");\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ agent: \"engine\" }).map((i) => i.id),\n+\t\t\t[id],\n+\t\t);\n+\t\tassert.ok(s.page(id).issue.watchers.includes(\"fe\"));\n+\t});\n+\n+\tit(\"drafts are their writer's alone, have no number, make no news, and open as issues\", () => {\n+\t\tconst { s } = store();\n+\t\tconst d = s.saveDraft(christopher, { title: \"Half a thought\" }).id;\n+\t\ts.saveDraft(christopher, { id: d, title: \"A whole thought\", agent: \"fe\" });\n+\t\tassert.deepEqual(\n+\t\t\ts.drafts(christopher).map((x) => x.body),\n+\t\t\t[{ title: \"A whole thought\", agent: \"fe\" }],\n+\t\t);\n+\t\tassert.deepEqual(s.drafts({ person: \"u2\" }), []);\n+\t\tassert.throws(() => s.deleteDraft({ person: \"u2\" }, d), /you have no draft/);\n+\t\tassert.deepEqual(s.list(), []);\n+\t\tassert.deepEqual(s.drafts(christopher)[0]!.id, d);\n+\t\ts.saveDraft(christopher, {});\n+\t\tconst opened = s.openDraft(christopher, d, { description: \"added when opening\" });\n+\t\tassert.equal(opened.id, 1);\n+\t\tassert.deepEqual(opened.news!.to, [\"fe\"]);\n+\t\tassert.equal(s.page(1).issue.description, \"added when opening\");\n+\t\tassert.equal(s.drafts(christopher).length, 1);\n+\t\tassert.throws(() => s.openDraft(christopher, s.drafts(christopher)[0]!.id), /the title is missing/);\n+\t});\n+\n+\tit(\"refuses words a reader can't see, and titles over more than one line\", () => {\n+\t\tconst { s } = store();\n+\t\tassert.throws(\n+\t\t\t() => s.open(builder, { title: \"fine\\u{E0041}\" }),\n+\t\t\t/a tag character \\(U\\+E0000-E007F\\) at character 5/,\n+\t\t);\n+\t\tassert.throws(() => s.open(builder, { title: \"one\\ntwo\" }), /the title is one line/);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\", description: \"a\\u202Eb\" }), /bidirectional/);\n+\t\tconst id = s.open(builder, { title: \"x\", description: \"two\\nlines\\tand a tab\" }).id;\n+\t\tassert.throws(() => s.comment(builder, id, { text: \"hi\\u200B\" }), /zero-width/);\n+\t\tassert.throws(() => s.edit(builder, id, { title: \"a\\nb\" }), /one line/);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\", notify: [\"fe\\nengine\"] }), /one line/);\n+\t});\n+\n+\tit(\"routes: edit, and drafts read and written by their writer\", async () => {\n+\t\tconst { s } = store();\n+\t\tconst call = (method: string, path: string, body?: unknown, headers: Record<string, string> = {}) =>\n+\t\t\tissuesRoute(\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\theaders,\n+\t\t\t\t\t...(body !== undefined ? { body: JSON.stringify(body) } : {}),\n+\t\t\t\t}),\n+\t\t\t).then(async (r) => ({ status: r.status, json: (await r.json()) as Record<string, any> }));\n+\t\tconst title400 = await call(\"POST\", \"/p/web/a/fe/issue\", { title: \"x\\u{E0001}\" });\n+\t\tassert.equal(title400.status, 400);\n+\t\tassert.match(title400.json.error, /tag character/);\n+\t\tassert.equal((await call(\"POST\", \"/p/web/a/fe/issue\", { title: \"a\\nb\" })).status, 400);\n+\t\tawait call(\"POST\", \"/p/web/a/fe/issue\", { title: \"Sign-in\" });\n+\t\tconst edited = await call(\"POST\", \"/p/web/a/lead/issue/1/edit\", { title: \"Sign-in flow\" });\n+\t\tassert.equal(edited.status, 200);\n+\t\tassert.deepEqual(edited.json.notified, [\"fe\"]);\n+\t\tconst saved = await call(\"POST\", \"/p/web/a/lead/draft\", { title: \"Later\" });\n+\t\tassert.equal((await call(\"GET\", \"/p/web/a/lead/drafts\", undefined)).json.drafts[0].body.title, \"Later\");\n+\t\tassert.deepEqual((await call(\"GET\", \"/p/web/a/fe/drafts\")).json.drafts, []);\n+\t\t// A read naming nobody sees no one's drafts.\n+\t\tassert.equal((await call(\"GET\", \"/p/web/drafts\")).status, 403);\n+\t\tassert.equal((await call(\"POST\", `/p/web/a/fe/draft/${saved.json.id}/delete`)).status, 404);\n+\t\tassert.equal((await call(\"POST\", `/p/web/a/lead/draft/${saved.json.id}/open`, {})).json.id, 2);\n+\t\tassert.deepEqual((await call(\"GET\", \"/p/web/a/lead/drafts\", undefined)).json.drafts, []);\n+\t});\n+});\n+\n+describe(\"issues: each writer's limit\", () => {\n+\tit(\"one write past the per-minute limit gets 429 with Retry-After; another writer isn't affected\", async () => {\n+\t\tconst { s, tick } = store();\n+\t\tconst call = (path: string, body: unknown, agent: string) =>\n+\t\t\tissuesRoute(\n+\t\t\t\ts,\n+\t\t\t\tnew Request(`https://issues.internal${path.replace(\"/p/web/\", `/p/web/a/${agent}/`)}`, {\n+\t\t\t\t\tmethod: \"POST\",\n+\t\t\t\t\tbody: JSON.stringify(body),\n+\t\t\t\t}),\n+\t\t\t);\n+\t\tassert.equal((await call(\"/p/web/issue\", { title: \"One\" }, \"u1\")).status, 200);\n+\t\tfor (let i = 0; i < WRITES_PER_MINUTE - 1; i++)\n+\t\t\tassert.equal((await call(\"/p/web/issue/1/comment\", { text: `c${i}` }, \"u1\")).status, 200);\n+\t\t// A refused write doesn't count, and drafts make no news, so they don't either.\n+\t\tassert.equal((await call(\"/p/web/draft\", { title: \"later\" }, \"u1\")).status, 200);\n+\t\tconst over = await call(\"/p/web/issue/1/comment\", { text: \"over limit\" }, \"u1\");\n+\t\tassert.equal(over.status, 429);\n+\t\tassert.match(\n+\t\t\t((await over.json()) as { error: string }).error,\n+\t\t\tnew RegExp(`at most ${WRITES_PER_MINUTE} issue writes a minute`),\n+\t\t);\n+\t\tassert.equal(over.headers.get(\"Retry-After\"), \"60\");\n+\t\tassert.equal((await call(\"/p/web/issue/1/comment\", { text: \"mine\" }, \"u2\")).status, 200);\n+\t\ttick(60_000);\n+\t\tassert.equal((await call(\"/p/web/issue/1/comment\", { text: \"a minute on\" }, \"u1\")).status, 200);\n+\t});\n+});\n+\n+describe(\"issues: labels\", () => {\n+\tit(\"set on open, changed by edit, filtered by, and listed with counts\", () => {\n+\t\tconst { s } = store();\n+\t\tconst a = s.open(builder, { title: \"Sync is slow\", labels: [\"friction\", \"client\", \"friction\"] }).id;\n+\t\tconst b = s.open(builder, { title: \"Blame past a reformat\", labels: [\"friction\", \"engine\"] }).id;\n+\t\ts.open(builder, { title: \"Logo\" });\n+\t\tassert.deepEqual(s.page(a).issue.labels, [\"client\", \"friction\"]);\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ labels: [\"friction\"] }).map((i) => i.id),\n+\t\t\t[b, a],\n+\t\t);\n+\t\tassert.deepEqual(\n+\t\t\ts.list({ labels: [\"friction\", \"client\"] }).map((i) => i.id),\n+\t\t\t[a],\n+\t\t);\n+\t\t// A label-only edit tells nobody unless asked; it's in the activity.\n+\t\tconst quiet = s.edit(christopher, a, { addLabels: [\"good-first-issue\"], removeLabels: [\"client\"] });\n+\t\tassert.deepEqual(quiet.notified, []);\n+\t\tassert.deepEqual(s.page(a).issue.labels, [\"friction\", \"good-first-issue\"]);\n+\t\tassert.deepEqual(s.page(a).edits.at(-1)!.changes.labels, {\n+\t\t\tfrom: [\"client\", \"friction\"],\n+\t\t\tto: [\"friction\", \"good-first-issue\"],\n+\t\t});\n+\t\tconst told = s.edit(christopher, a, { labels: [\"friction\"], notify: true });\n+\t\tassert.match(told.news!.text, /labels friction$/);\n+\t\ts.drop(builder, b);\n+\t\tassert.deepEqual(s.labelsInUse(), [\n+\t\t\t{ label: \"engine\", open: 0, total: 1 },\n+\t\t\t{ label: \"friction\", open: 1, total: 2 },\n+\t\t]);\n+\t\tassert.throws(() => s.edit(builder, a, { addLabels: [\"friction\"] }), /changes nothing/);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\", labels: [\"two words\"] }), /isn't a label/);\n+\t\t// Sent straight to the Worker, upper case and a zero-width are refused, not changed.\n+\t\tassert.throws(\n+\t\t\t() => s.open(builder, { title: \"x\", labels: [\"Friction\"] }),\n+\t\t\t(e: { status?: number }) => e.status === 400,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() => s.open(builder, { title: \"x\", labels: [\"fric​tion\"] }),\n+\t\t\t(e: { status?: number }) => e.status === 400,\n+\t\t);\n+\t\tassert.throws(() => s.open(builder, { title: \"x\", labels: [\"x\".repeat(41)] }), /isn't a label/);\n+\t\tassert.throws(() => s.edit(builder, a, { labels: [], addLabels: [\"x\"] }), /one or the other/);\n+\t\tassert.throws(() => s.list({ labels: [\"Bad Label\"] }), /isn't a label/);\n+\t});\n+\n+\tit(\"routes: ?label= filters, GET labels lists, and drafts keep labels\", async () => {\n+\t\tconst { s } = store();\n+\t\tconst call = (method: string, path: string, body?: unknown, headers: Record<string, string> = {}) =>\n+\t\t\tissuesRoute(\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\theaders,\n+\t\t\t\t\t...(body !== undefined ? { body: JSON.stringify(body) } : {}),\n+\t\t\t\t}),\n+\t\t\t).then(async (r) => ({ status: r.status, json: (await r.json()) as Record<string, any> }));\n+\t\tawait call(\"POST\", \"/p/web/a/fe/issue\", { title: \"One\", labels: [\"friction\"] });\n+\t\tawait call(\"POST\", \"/p/web/a/fe/issue\", { title: \"Two\", labels: [\"friction\", \"client\"] });\n+\t\tconst both = await call(\"GET\", \"/p/web/issues?label=friction,client\");\n+\t\tassert.deepEqual(\n+\t\t\tboth.json.issues.map((i: { title: string }) => i.title),\n+\t\t\t[\"Two\"],\n+\t\t);\n+\t\tassert.equal((await call(\"GET\", \"/p/web/issues?label=friction&label=client\")).json.issues.length, 1);\n+\t\tassert.equal((await call(\"GET\", \"/p/web/issues?label=Not%20one\")).status, 400);\n+\t\tassert.deepEqual((await call(\"GET\", \"/p/web/labels\")).json.labels, [\n+\t\t\t{ label: \"client\", open: 1, total: 1 },\n+\t\t\t{ label: \"friction\", open: 2, total: 2 },\n+\t\t]);\n+\t\tconst edited = await call(\"POST\", \"/p/web/a/fe/issue/1/edit\", { addLabels: [\"client\"] });\n+\t\tassert.equal(edited.status, 200);\n+\t\tconst d = await call(\"POST\", \"/p/web/a/fe/draft\", { title: \"Later\", labels: [\"friction\"] });\n+\t\tconst opened = await call(\"POST\", `/p/web/a/fe/draft/${d.json.id}/open`, {});\n+\t\tassert.deepEqual((await call(\"GET\", `/p/web/issue/${opened.json.id}`)).json.issue.labels, [\"friction\"]);\n+\t});\n+});\n+\n+describe(\"issues: a project's labels are capped\", () => {\n+\tit(\"a new label past 200 distinct ones is refused; one in use still goes on\", () => {\n+\t\tconst { s } = store();\n+\t\tconst ids: number[] = [];\n+\t\tfor (let i = 0; i < 10; i++)\n+\t\t\tids.push(s.open(builder, { title: `t${i}`, labels: Array.from({ length: 20 }, (_, j) => `l${i * 20 + j}`) }).id);\n+\t\tassert.equal(s.labelsInUse().length, 200);\n+\t\tassert.throws(\n+\t\t\t() => s.open(builder, { title: \"one more\", labels: [\"l-new\"] }),\n+\t\t\t/this project has 200 labels, and 200 is the most/,\n+\t\t);\n+\t\tassert.throws(\n+\t\t\t() => s.edit(builder, s.open(builder, { title: \"room\" }).id, { addLabels: [\"l-new\"] }),\n+\t\t\t/200 is the most/,\n+\t\t);\n+\t\tassert.equal(s.edit(builder, ids[1]!, { addLabels: [\"l0\"], removeLabels: [\"l20\"] }).notified.length, 0);\n+\t});\n+});\n+\n+describe(\"issues: when an ask was first listed to its reader\", () => {\n+\tconst get = (s: IssuesStore, path: string, headers: Record<string, string> = {}) =>\n+\t\tissuesRoute(s, new Request(`https://issues.internal${path}`, { headers }));\n+\n+\tit(\"an agent's own waiting marks what it lists as seen, and says null for what it saw first just now\", async () => {\n+\t\tconst { s, tick } = store();\n+\t\tconst id = s.ask(builder, { of: \"security\", question: \"Is the cookie set right?\" }).id;\n+\t\tconst read = async (path: string) =>\n+\t\t\t((await (await get(s, path)).json()) as { asks: { id: number; seen: number | null }[] }).asks;\n+\t\tassert.deepEqual(await read(\"/p/abso/waiting?of=security\"), [{ ...s.waiting(\"security\")[0]!, seen: null }]);\n+\t\tassert.equal(s.waiting(\"security\")[0]!.seen, null, \"a read of someone else's list marks nothing\");\n+\t\ttick();\n+\t\tconst first = await read(\"/p/abso/a/security/waiting\");\n+\t\tassert.equal(first[0]!.seen, null);\n+\t\tconst at = s.waiting(\"security\")[0]!.seen;\n+\t\tassert.equal(at, Date.parse(\"2026-10-07T20:00:01Z\"));\n+\t\ttick();\n+\t\tassert.equal((await read(\"/p/abso/a/security/waiting\"))[0]!.seen, at, \"the first time stays\");\n+\t\tassert.equal(first[0]!.id, id);\n+\t});\n+\n+\tit(\"another agent's read of the list marks nothing\", async () => {\n+\t\tconst { s } = store();\n+\t\ts.ask(builder, { of: \"security\", question: \"Take abso.dev?\" });\n+\t\tawait get(s, \"/p/abso/a/fe/waiting\");\n+\t\tassert.equal(s.waiting(\"security\")[0]!.seen, null);\n+\t\tawait get(s, \"/p/abso/a/security/waiting\");\n+\t\tassert.notEqual(s.waiting(\"security\")[0]!.seen, null);\n+\t});\n+});"}],"unmatched":[]}