{"draft":null,"from":116,"to":318,"files":[{"path":"server/test/engine-project.test.ts","added":57,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/engine-project.test.ts\n@@ -0,0 +1,57 @@\n+import assert from \"node:assert/strict\";\n+import { describe, it } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { EngineProject, SqliteLog } from \"../src/engine-project.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+describe(\"the engine over the object's SQLite log\", () => {\n+\tit(\"extracts feed events without retaining a page of canonical payloads\", (t) => {\n+\t\tconst storage = memoryStorage();\n+\t\tt.after(() => storage.db.close());\n+\t\tconst project = EngineProject.open(storage);\n+\t\tanswer(project, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } });\n+\t\tanswer(project, \"sync\", \"alice\", {});\n+\t\tanswer(project, \"record\", \"alice\", { files: { \"a.txt\": \"two\\n\" } });\n+\t\tanswer(project, \"publish\", \"alice\", { message: \"Changed\" });\n+\t\tconst expected = project.feed(0, 500);\n+\t\tconst original = SqliteLog.prototype.since;\n+\t\tlet reads = 0;\n+\t\tt.mock.method(SqliteLog.prototype, \"since\", function (this: SqliteLog, from: number, limit: number) {\n+\t\t\tassert.equal(limit, 1, \"feed must release each canonical payload before reading another\");\n+\t\t\treads++;\n+\t\t\treturn original.call(this, from, limit);\n+\t\t});\n+\t\tassert.deepEqual(project.feed(0, 500), expected);\n+\t\tassert.equal(reads, expected.next);\n+\t\tassert.deepEqual(project.feed(1, 2).events, expected.events.slice(1, 3));\n+\t});\n+\tit(\"serves init, records and pushes through answer(), and holds a collision\", () => {\n+\t\tconst storage = memoryStorage();\n+\t\tconst p = EngineProject.open(storage);\n+\t\tassert.equal(answer(p, \"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\nthree\\n\" } }).status, 200);\n+\t\tfor (const agent of [\"alice\", \"bob\"]) assert.equal(answer(p, \"sync\", agent, {}).status, 200);\n+\t\tanswer(p, \"record\", \"alice\", { files: { \"a.txt\": \"one\\nTWO by alice\\nthree\\n\" } });\n+\t\tanswer(p, \"record\", \"bob\", { files: { \"a.txt\": \"one\\ntwo by bob\\nthree\\n\" } });\n+\t\tconst first = answer(p, \"publish\", \"alice\", { message: \"Alice's two\" });\n+\t\tassert.deepEqual(first, { status: 200, body: { ok: true, version: 1, changes: 1 } });\n+\t\tconst second = answer(p, \"publish\", \"bob\", { message: \"Bob's two\" }).body as { ok: boolean; collisions: unknown[] };\n+\t\tassert.equal(second.ok, false);\n+\t\tassert.equal(second.collisions.length, 1);\n+\t});\n+\n+\tit(\"loads the same project back from the log\", () => {\n+\t\tconst storage = memoryStorage();\n+\t\tconst p = EngineProject.open(storage);\n+\t\tanswer(p, \"init\", undefined, { files: { \"a.txt\": \"one\\n\" } });\n+\t\tanswer(p, \"sync\", \"alice\", {});\n+\t\tanswer(p, \"record\", \"alice\", { files: { \"a.txt\": \"one\\ntwo\\n\" } });\n+\t\tanswer(p, \"publish\", \"alice\", { message: \"Add two\" });\n+\t\tconst again = EngineProject.open(storage);\n+\t\tassert.equal(again.shared().version, 1);\n+\t\tassert.equal(again.shared().files[\"a.txt\"], \"one\\ntwo\\n\");\n+\t\tconst log = new SqliteLog(storage);\n+\t\tassert.equal(log.sharedVersion(), 1);\n+\t\tassert.deepEqual(log.lastPublish()?.agent, \"alice\");\n+\t\tassert.equal(log.logSize().entries, log.seq());\n+\t});\n+});"}],"unmatched":[]}