{"draft":null,"from":35,"to":318,"files":[{"path":"server/test/projected-queue.test.ts","added":189,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-queue.test.ts\n@@ -0,0 +1,189 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import type { Project } from \"../../worker/project/project.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk queue preserves a dropped base without restoring dropped changes\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue-drop\", \"queue-drop\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"temporary\\n\" } }).status, 200);\n+\t\t\tassert.equal(answer(c, \"publish\", \"alice\", { message: \"Temporary\" }).status, 200);\n+\t\t\tc.sync(\"old\");\n+\t\t\tassert.equal(c.drop(\"alice\", [1], \"Undo temporary\").ok, true);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\tfor (const [agent, file] of [\n+\t\t[\"ahead\", \"ahead.txt\"],\n+\t\t[\"old\", \"old.txt\"],\n+\t] as const) {\n+\t\truntime().write(\n+\t\t\t(c) => {\n+\t\t\t\tc.setCheckerReady(true);\n+\t\t\t\tassert.equal(answer(c, \"record\", agent, { files: { [file]: `${agent}\\n` } }).status, 200);\n+\t\t\t\tconst result = answer(c, \"publish\", agent, { message: agent });\n+\t\t\t\tassert.equal(result.status, 200, JSON.stringify(result));\n+\t\t\t\tassert.equal((result.body as { queued: number }).queued, agent === \"ahead\" ? 1 : 2);\n+\t\t\t},\n+\t\t\t() => {},\n+\t\t);\n+\t}\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"before\\n\");\n+\tassert.equal(candidate.files[\"ahead.txt\"], \"ahead\\n\");\n+\tassert.equal(candidate.files[\"old.txt\"], \"old\\n\");\n+});\n+\n+test(\"disk runtime builds and lands a queued candidate\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue\", \"queue\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"alice\", { message: \"Queued\" });\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 1);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\t// Queue-driver methods are internal to the Project adapter, not public HTTP commands.\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(1),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"after\\n\");\n+\tconst landed = runtime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(1, 10),\n+\t\t() => {},\n+\t);\n+\tassert.deepEqual(landed.versions, [{ id: 1, version: 1 }]);\n+\tconst empty = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(4),\n+\t\t() => {},\n+\t);\n+\tassert.deepEqual(empty.ids, []);\n+\tassert.deepEqual(empty.files, candidate.files);\n+\tassert.equal(empty.tree, candidate.tree);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files[\"a.txt\"],\n+\t\t\t() => {},\n+\t\t),\n+\t\t\"after\\n\",\n+\t);\n+});\n+\n+test(\"disk runtime builds and lands two queued candidates in order\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue\", \"queue\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"alice\", { message: \"Queued\" });\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 1);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"bob\", { files: { \"b.txt\": \"second\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"bob\", { message: \"Second\" });\n+\t\t\tassert.equal(result.status, 200, JSON.stringify(result));\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 2);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\t// Queue-driver methods are internal to the Project adapter, not public HTTP commands.\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"after\\n\");\n+\tassert.equal(candidate.files[\"b.txt\"], \"second\\n\");\n+\tconst landed = runtime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(2, 10),\n+\t\t() => {},\n+\t);\n+\tassert.deepEqual(landed.versions, [\n+\t\t{ id: 1, version: 1 },\n+\t\t{ id: 2, version: 2 },\n+\t]);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files[\"a.txt\"],\n+\t\t\t() => {},\n+\t\t),\n+\t\t\"after\\n\",\n+\t);\n+});\n+\n+test(\"disk queue refuses overlapping edits and accepts an older nonconflicting base\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue-edge\", \"queue-edge\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst call = (op: string, agent: string, body: unknown) =>\n+\t\truntime().write(\n+\t\t\t(c) => {\n+\t\t\t\tc.setCheckerReady(true);\n+\t\t\t\treturn answer(c, op, agent, body);\n+\t\t\t},\n+\t\t\t() => {},\n+\t\t);\n+\tfor (const agent of [\"alice\", \"bob\", \"old\"]) assert.equal(call(\"sync\", agent, {}).status, 200);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"a.txt\": \"alice\\n\" } }).status, 200);\n+\tassert.equal((call(\"publish\", \"alice\", { message: \"Alice\" }).body as { queued: number }).queued, 1);\n+\tassert.equal(call(\"record\", \"bob\", { files: { \"a.txt\": \"bob\\n\" } }).status, 200);\n+\tconst refused = call(\"publish\", \"bob\", { message: \"Bob conflict\" });\n+\tassert.equal(refused.status, 200, JSON.stringify(refused));\n+\tassert.equal((refused.body as { ok: boolean }).ok, false);\n+\tassert.ok((refused.body as { collisions: unknown[] }).collisions.length > 0);\n+\truntime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(1, 20),\n+\t\t() => {},\n+\t);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"next.txt\": \"next\\n\" } }).status, 200);\n+\tassert.equal((call(\"publish\", \"alice\", { message: \"Next\" }).body as { queued: number }).queued, 2);\n+\tassert.equal(call(\"record\", \"old\", { files: { \"old.txt\": \"old base\\n\" } }).status, 200);\n+\tconst queued = call(\"publish\", \"old\", { message: \"Old base\" });\n+\tassert.equal(queued.status, 200, JSON.stringify(queued));\n+\tassert.equal((queued.body as { queued: number }).queued, 3);\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"alice\\n\");\n+\tassert.equal(candidate.files[\"next.txt\"], \"next\\n\");\n+\tassert.equal(candidate.files[\"old.txt\"], \"old base\\n\");\n+});"}],"unmatched":[]}