{"draft":null,"from":175,"to":318,"files":[{"path":"worker/project/test/without.test.ts","added":206,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/without.test.ts\n@@ -0,0 +1,206 @@\n+// A version without others: the files of one version of shared with some earlier versions left out. A read:\n+// shared and its log never change. Refused, naming the chain, when a version kept builds on one left out.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { MemoryStore, Project, ProjectError } from \"../project.ts\";\n+import { treeHash } from \"../tree.ts\";\n+\n+const START = { \"a.ts\": \"one\\ntwo\\nthree\\n\", \"b.ts\": \"alpha\\nbeta\\n\" };\n+\n+function setup() {\n+\tconst store = new MemoryStore();\n+\tconst project = new Project(store, { hasBlob: () => true });\n+\tproject.init(START, 0);\n+\tfor (const a of [\"ann\", \"bob\", \"cat\"]) project.sync(a);\n+\treturn { project, store };\n+}\n+\n+/** ann publishes v1 (a.ts), bob v2 (b.ts), cat v3 (a new c.ts): three versions that don't build on each other. */\n+function threeVersions(project: Project): void {\n+\tproject.edit(\"ann\", { file: \"a.ts\", old: \"two\", new: \"TWO\" }, 1);\n+\tproject.publish(\"ann\", \"Shout two\", 2);\n+\tproject.edit(\"bob\", { file: \"b.ts\", old: \"beta\", new: \"BETA\" }, 3);\n+\tproject.publish(\"bob\", \"Shout beta\", 4);\n+\tproject.record(\"cat\", { \"c.ts\": \"gamma\\n\" }, 5);\n+\tproject.publish(\"cat\", \"Add c\", 6);\n+}\n+\n+const badRequest = (f: () => unknown): string => {\n+\ttry {\n+\t\tf();\n+\t} catch (e) {\n+\t\tif (e instanceof ProjectError && e.code === \"bad-request\") return e.message;\n+\t\tthrow e;\n+\t}\n+\treturn \"no error\";\n+};\n+\n+test(\"leaving out an independent version takes only its change out; the others stay as they are\", () => {\n+\tconst { project, store } = setup();\n+\tthreeVersions(project);\n+\tconst entries = store.entries.length;\n+\tconst answer = project.without(3, [1]);\n+\tassert.equal(answer.ok, true);\n+\tassert.ok(answer.ok);\n+\tassert.deepEqual(answer.without, [{ version: 1, agent: \"ann\", message: \"Shout two\" }]);\n+\tassert.deepEqual(answer.files, [{ path: \"a.ts\", before: \"one\\nTWO\\nthree\\n\", after: \"one\\ntwo\\nthree\\n\" }]);\n+\tassert.equal(\n+\t\tanswer.tree,\n+\t\ttreeHash({ files: { \"a.ts\": \"one\\ntwo\\nthree\\n\", \"b.ts\": \"alpha\\nBETA\\n\", \"c.ts\": \"gamma\\n\" } }),\n+\t);\n+\t// Shared is unchanged: same version, same files, nothing written to the log.\n+\tassert.equal(project.version, 3);\n+\tassert.deepEqual(project.files(), { \"a.ts\": \"one\\nTWO\\nthree\\n\", \"b.ts\": \"alpha\\nBETA\\n\", \"c.ts\": \"gamma\\n\" });\n+\tassert.equal(store.entries.length, entries);\n+\t// Two left out at once.\n+\tconst two = project.without(3, [3, 1]);\n+\tassert.ok(two.ok);\n+\tassert.deepEqual(\n+\t\ttwo.without.map((w) => w.version),\n+\t\t[1, 3],\n+\t);\n+\tassert.deepEqual(\n+\t\ttwo.files.map((f) => [f.path, f.after]),\n+\t\t[\n+\t\t\t[\"a.ts\", \"one\\ntwo\\nthree\\n\"],\n+\t\t\t[\"c.ts\", null],\n+\t\t],\n+\t);\n+});\n+\n+test(\"the same ask gives the same answer, and shared moving on doesn't change it\", () => {\n+\tconst { project } = setup();\n+\tthreeVersions(project);\n+\tconst first = project.without(3, [2]);\n+\tassert.deepEqual(project.without(3, [2]), first);\n+\tproject.sync(\"ann\");\n+\tproject.edit(\"ann\", { file: \"c.ts\", old: \"gamma\", new: \"GAMMA\" }, 7);\n+\tproject.publish(\"ann\", \"Shout gamma\", 8);\n+\tassert.deepEqual(project.without(3, [2]), first);\n+\t// Asked of the newer version, the newer change is in it.\n+\tconst later = project.without(4, [2]);\n+\tassert.ok(later.ok && first.ok);\n+\tassert.notEqual(later.tree, first.tree);\n+});\n+\n+test(\"leaving out a version a kept one builds on is refused, with the chain, the files and the wider set\", () => {\n+\tconst { project, store } = setup();\n+\tthreeVersions(project);\n+\tproject.sync(\"cat\");\n+\tproject.edit(\"cat\", { file: \"a.ts\", old: \"TWO\", new: \"TWO!\" }, 7);\n+\tproject.publish(\"cat\", \"Louder\", 8); // v4 builds on v1\n+\tproject.sync(\"bob\");\n+\tproject.edit(\"bob\", { file: \"a.ts\", old: \"TWO!\", new: \"TWO!!\" }, 9);\n+\tproject.publish(\"bob\", \"Louder still\", 10); // v5 builds on v4\n+\tconst entries = store.entries.length;\n+\tconst refused = project.without(5, [1]);\n+\tassert.deepEqual(refused, {\n+\t\tok: false,\n+\t\tversion: 5,\n+\t\twithout: [{ version: 1, agent: \"ann\", message: \"Shout two\" }],\n+\t\tdependents: [\n+\t\t\t{ version: 4, agent: \"cat\", message: \"Louder\", needs: [{ version: 1, files: [\"a.ts\"] }] },\n+\t\t\t{ version: 5, agent: \"bob\", message: \"Louder still\", needs: [{ version: 4, files: [\"a.ts\"] }] },\n+\t\t],\n+\t\tsuggestion: [1, 4, 5],\n+\t});\n+\tassert.equal(store.entries.length, entries);\n+\tassert.equal(project.version, 5);\n+\t// The suggestion, asked for, works; it's never applied without being asked.\n+\tconst wider = project.without(5, refused.ok ? [] : refused.suggestion);\n+\tassert.ok(wider.ok);\n+\tassert.deepEqual(wider.files, [{ path: \"a.ts\", before: \"one\\nTWO!!\\nthree\\n\", after: \"one\\ntwo\\nthree\\n\" }]);\n+\t// Before the dependent version, leaving it out is fine.\n+\tassert.equal(project.without(3, [1]).ok, true);\n+});\n+\n+test(\"a deletion left out brings the lines back; a rename left out keeps the old name; a move left out puts lines back\", () => {\n+\tconst { project } = setup();\n+\tproject.edit(\"ann\", { file: \"a.ts\", old: \"two\\n\", new: \"\" }, 1);\n+\tproject.publish(\"ann\", \"No two\", 2);\n+\tproject.record(\"bob\", { \"b.ts\": null, \"c.ts\": START[\"b.ts\"] }, 3);\n+\tproject.publish(\"bob\", \"Rename b to c\", 4);\n+\tproject.sync(\"cat\");\n+\tproject.move(\"cat\", \"a.ts\", \"src/a.ts\", 5);\n+\tproject.publish(\"cat\", \"Move a into src\", 6);\n+\tassert.deepEqual(project.files(), { \"src/a.ts\": \"one\\nthree\\n\", \"c.ts\": \"alpha\\nbeta\\n\" });\n+\n+\tconst noDelete = project.without(3, [1]);\n+\tassert.ok(noDelete.ok);\n+\tassert.deepEqual(\n+\t\tnoDelete.files.map((f) => [f.path, f.after]),\n+\t\t[[\"src/a.ts\", \"one\\ntwo\\nthree\\n\"]],\n+\t);\n+\tconst noRename = project.without(3, [2]);\n+\tassert.ok(noRename.ok);\n+\tassert.deepEqual(\n+\t\tnoRename.files.map((f) => [f.path, f.before, f.after]),\n+\t\t[\n+\t\t\t[\"b.ts\", null, \"alpha\\nbeta\\n\"],\n+\t\t\t[\"c.ts\", \"alpha\\nbeta\\n\", null],\n+\t\t],\n+\t);\n+\tconst noMove = project.without(3, [3]);\n+\tassert.ok(noMove.ok);\n+\tassert.deepEqual(\n+\t\tnoMove.files.map((f) => [f.path, f.after]),\n+\t\t[\n+\t\t\t[\"a.ts\", \"one\\nthree\\n\"],\n+\t\t\t[\"src/a.ts\", null],\n+\t\t],\n+\t);\n+});\n+\n+test(\"a binary file's change and an executable bit left out go back to what they were\", () => {\n+\tconst A = \"a\".repeat(64);\n+\tconst B = \"b\".repeat(64);\n+\tconst store = new MemoryStore();\n+\tconst project = new Project(store, { hasBlob: () => true });\n+\tproject.init({ \"run.sh\": \"echo hi\\n\" }, 0, {}, { \"logo.png\": A });\n+\tproject.sync(\"ann\");\n+\tproject.record(\"ann\", {}, 1, {}, { \"logo.png\": B });\n+\tproject.publish(\"ann\", \"New logo\", 2);\n+\tproject.record(\"ann\", {}, 3, { \"run.sh\": { exec: true } });\n+\tproject.publish(\"ann\", \"Make run.sh runnable\", 4);\n+\tconst noLogo = project.without(2, [1]);\n+\tassert.ok(noLogo.ok);\n+\tassert.deepEqual(\n+\t\tnoLogo.files.map((f) => [f.path, f.blob]),\n+\t\t[[\"logo.png\", { before: B, after: A }]],\n+\t);\n+\tconst noExec = project.without(2, [2]);\n+\tassert.ok(noExec.ok);\n+\tassert.deepEqual(\n+\t\tnoExec.files.map((f) => [f.path, f.mode?.after.exec ?? false]),\n+\t\t[[\"run.sh\", false]],\n+\t);\n+});\n+\n+test(\"versions it can't leave out are refused plainly\", () => {\n+\tconst { project } = setup();\n+\tthreeVersions(project);\n+\tassert.equal(project.drop(\"cat\", [2], \"Undo beta\", 7).ok, true); // v4 drops v2\n+\tassert.match(\n+\t\tbadRequest(() => project.without(3, [4])),\n+\t\t/comes after version 3/,\n+\t);\n+\tassert.match(\n+\t\tbadRequest(() => project.without(4, [0])),\n+\t\t/version 0 is the start/,\n+\t);\n+\tassert.match(\n+\t\tbadRequest(() => project.without(4, [4])),\n+\t\t/is a drop/,\n+\t);\n+\tassert.match(\n+\t\tbadRequest(() => project.without(4, [2])),\n+\t\t/isn't in version 4: version 4 dropped it/,\n+\t);\n+\tassert.match(\n+\t\tbadRequest(() => project.without(4, [])),\n+\t\t/versions to leave out/,\n+\t);\n+\t// Before the drop, it's there to leave out.\n+\tassert.equal(project.without(3, [2]).ok, true);\n+});"}],"unmatched":[]}