{"draft":null,"from":157,"to":158,"files":[{"path":"server/test/project-object-disk-first.test.ts","added":168,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/project-object-disk-first.test.ts\n@@ -0,0 +1,168 @@\n+import assert from \"node:assert/strict\";\n+import { registerHooks } from \"node:module\";\n+import { test } from \"node:test\";\n+import { Project } from \"../../worker/project/project.ts\";\n+import { EngineProject } from \"../src/engine-project.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+const hooks = registerHooks({\n+\tresolve(specifier, context, next) {\n+\t\tif (specifier === \"cloudflare:workers\")\n+\t\t\treturn {\n+\t\t\t\turl: `data:text/javascript,${encodeURIComponent(\"export class DurableObject { constructor(ctx, env) { this.ctx = ctx; this.env = env; } }\")}`,\n+\t\t\t\tshortCircuit: true,\n+\t\t\t};\n+\t\treturn next(specifier, context);\n+\t},\n+});\n+const { ProjectObject } = await import(\"../src/project-object.ts\");\n+hooks.deregister();\n+\n+test(\"empty disk projects and unloadable legacy projects preserve lookup fallbacks\", async (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst context = { id: { toString: () => \"empty\" }, storage: { ...storage, get: async () => undefined } };\n+\tconst disk = new ProjectObject(context as never, { ABSO_DISK_FIRST: \"on\" } as never);\n+\tassert.deepEqual(await disk.call(\"empty\", \"status\", undefined, {}), {\n+\t\tstatus: 404,\n+\t\tbody: { error: \"no such project; run init first\" },\n+\t});\n+\tassert.deepEqual(await disk.agents(\"empty\"), []);\n+\tassert.deepEqual(await disk.news(\"empty\", [{ from: \"alice\", issue: null, text: \"Hello\" }]), [{ to: [] }]);\n+\tawait disk.alarm();\n+\tt.mock.method(EngineProject, \"open\", () => {\n+\t\tthrow Error(\"unloadable legacy log\");\n+\t});\n+\tconst legacy = new ProjectObject(context as never, {} as never);\n+\tassert.deepEqual(await legacy.agents(\"empty\"), []);\n+\tassert.deepEqual(await legacy.news(\"empty\", [{ from: \"alice\", issue: null, text: \"Hello\" }]), [{ to: [] }]);\n+\tawait legacy.alarm();\n+});\n+\n+test(\"ProjectObject disk-first calls survive cold objects without replay\", async (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst values = new Map<string, unknown>();\n+\tlet alarm: number | null = null;\n+\tlet onReaders: (() => Promise<void>) | undefined;\n+\tconst context = {\n+\t\tid: { toString: () => \"project-1\" },\n+\t\tstorage: {\n+\t\t\t...storage,\n+\t\t\tget: async (key: string) => {\n+\t\t\t\tif (key === \"readers\" && onReaders) {\n+\t\t\t\t\tconst action = onReaders;\n+\t\t\t\t\tonReaders = undefined;\n+\t\t\t\t\tawait action();\n+\t\t\t\t}\n+\t\t\t\treturn values.get(key);\n+\t\t\t},\n+\t\t\tput: async (key: string | Record<string, unknown>, value?: unknown) => {\n+\t\t\t\tif (typeof key === \"string\") values.set(key, value);\n+\t\t\t\telse for (const [name, v] of Object.entries(key)) values.set(name, v);\n+\t\t\t},\n+\t\t\tdelete: async (key: string) => values.delete(key),\n+\t\t\tgetAlarm: async () => alarm,\n+\t\t\tsetAlarm: async (value: number) => {\n+\t\t\t\talarm = value;\n+\t\t\t},\n+\t\t},\n+\t};\n+\tconst object = () =>\n+\t\tnew ProjectObject(\n+\t\t\tcontext as never,\n+\t\t\t{ ABSO_DISK_FIRST: \"on\", ABSO_ARTIFACTS: \"off\", KEY_HMAC_SECRET: \"invented-test-secret\" } as never,\n+\t\t);\n+\tt.mock.method(Project, \"load\", () => {\n+\t\tthrow Error(\"full history replay forbidden\");\n+\t});\n+\tconst call = async (op: string, agent: string | undefined, body: unknown) => {\n+\t\tfor (let page = 0; page < 100; page++) {\n+\t\t\tconst result = await object().call(\"project\", op, agent, body);\n+\t\t\tif (result.status !== 503) {\n+\t\t\t\tassert.notEqual(result.status, 500, JSON.stringify({ op, result }));\n+\t\t\t\treturn result;\n+\t\t\t}\n+\t\t\tassert.notEqual(alarm, null);\n+\t\t\tawait object().alarm();\n+\t\t}\n+\t\tthrow Error(\"adoption did not finish\");\n+\t};\n+\tassert.equal((await call(\"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\n\" } })).status, 200);\n+\tassert.equal((await call(\"sync\", \"alice\", {})).status, 200);\n+\tassert.equal((await call(\"record\", \"alice\", { files: { \"a.txt\": \"changed\\n\" } })).status, 200);\n+\tconst published = await call(\"publish\", \"alice\", { message: \"Disk first\" });\n+\tassert.equal(published.status, 200);\n+\tassert.equal((published.body as { ok: boolean }).ok, true);\n+\tconst file = await call(\"file\", undefined, { path: \"a.txt\", version: \"1\" });\n+\tassert.equal(file.status, 200);\n+\tassert.match(JSON.stringify(file.body), /changed/);\n+\tconst feed = await call(\"feed\", undefined, { from: \"0\", limit: \"2\" });\n+\tassert.equal(feed.status, 200);\n+\tassert.ok((feed.body as { next: number }).next > 0);\n+\tassert.deepEqual(await object().agents(\"project\"), [\"alice\"]);\n+\tassert.equal((await object().out()).version, 1);\n+\tassert.deepEqual(await object().news(\"project\", [{ from: \"bob\", issue: null, text: \"Hello\", to: [\"alice\"] }]), [\n+\t\t{ to: [\"alice\"] },\n+\t]);\n+\tassert.equal((await call(\"status\", undefined, {})).status, 200);\n+\tfor (const agent of [\"alice\", \"bob\"]) assert.equal((await call(\"sync\", agent, {})).status, 200);\n+\tassert.equal((await call(\"record\", \"alice\", { files: { \"a.txt\": \"Alice's change\\n\" } })).status, 200);\n+\tassert.equal((await call(\"record\", \"bob\", { files: { \"a.txt\": \"Bob's change\\n\" } })).status, 200);\n+\tassert.equal((await call(\"publish\", \"alice\", { message: \"Alice wins first\" })).status, 200);\n+\tconst collision = await call(\"publish\", \"bob\", { message: \"Bob conflicts\" });\n+\tassert.equal((collision.body as { ok: boolean }).ok, false);\n+\tassert.equal((collision.body as { collisions: unknown[] }).collisions.length, 1);\n+\tconst historical = await call(\"file\", undefined, { path: \"a.txt\", version: \"1\" });\n+\tassert.equal(historical.status, 200);\n+\tassert.match(JSON.stringify(historical.body), /changed/);\n+\tconst rows = () => ({\n+\t\tlog: storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(),\n+\t\tstate: storage.db.prepare(\"SELECT * FROM writer_state ORDER BY namespace,collection,key,part\").all(),\n+\t});\n+\tconst stable = rows();\n+\tconst disabled = new ProjectObject(context as never, { ABSO_DISK_FIRST: \"off\" } as never);\n+\tconst held = await disabled.call(\"project\", \"record\", \"alice\", { files: { \"a.txt\": \"legacy write\\n\" } });\n+\tassert.equal(held.status, 409);\n+\tassert.match(JSON.stringify(held.body), /enable ABSO_DISK_FIRST/);\n+\tassert.equal(await disabled.gitSync(), null);\n+\tassert.deepEqual(rows(), stable);\n+\tassert.equal((await call(\"status\", undefined, {})).status, 200, \"re-enabling disk mode preserves the valid root\");\n+\tconst originalAnnounce = EngineProject.prototype.announce;\n+\tconst failingAnnounce = t.mock.method(\n+\t\tEngineProject.prototype,\n+\t\t\"announce\",\n+\t\tfunction (this: EngineProject, ...args: Parameters<EngineProject[\"announce\"]>) {\n+\t\t\toriginalAnnounce.apply(this, args);\n+\t\t\tthrow Error(\"injected after announce\");\n+\t\t},\n+\t);\n+\tawait assert.rejects(\n+\t\t() => object().news(\"project\", [{ from: \"bob\", issue: null, text: \"Must roll back\", to: [\"alice\"] }]),\n+\t\t/injected after announce/,\n+\t);\n+\tassert.deepEqual(rows(), stable);\n+\tfailingAnnounce.mock.restore();\n+\tconst current = object();\n+\tonReaders = async () => {\n+\t\tawait current.revokeAgentKey(\"missing-test-key\", \"127.0.0.1\");\n+\t};\n+\tconst revoked = await current.call(\"project\", \"record\", \"alice\", { files: { \"a.txt\": \"unauthorized\\n\" } });\n+\tassert.equal(revoked.status, 409);\n+\tassert.equal((revoked.body as { retryable: boolean }).retryable, true);\n+\tassert.deepEqual(rows(), stable);\n+\tconst original = EngineProject.prototype.record;\n+\tt.mock.method(\n+\t\tEngineProject.prototype,\n+\t\t\"record\",\n+\t\tfunction (this: EngineProject, ...args: Parameters<EngineProject[\"record\"]>) {\n+\t\t\toriginal.apply(this, args);\n+\t\t\tthrow Error(\"injected after record\");\n+\t\t},\n+\t);\n+\tawait assert.rejects(\n+\t\t() => object().call(\"project\", \"record\", \"alice\", { files: { \"a.txt\": \"failed\\n\" } }),\n+\t\t/injected after record/,\n+\t);\n+\tassert.deepEqual(rows(), stable);\n+});"}],"unmatched":[]}