Versions 157 to 318
new file, 206 lines
1+// A version without others: the files of one version of shared with some earlier versions left out. A read: 2+// shared and its log never change. Refused, naming the chain, when a version kept builds on one left out. 4+import assert from "node:assert/strict"; 5+import { test } from "node:test"; 6+import { MemoryStore, Project, ProjectError } from "../project.ts"; 7+import { treeHash } from "../tree.ts"; 9+const START = { "a.ts": "one\ntwo\nthree\n", "b.ts": "alpha\nbeta\n" }; 12+ const store = new MemoryStore(); 13+ const project = new Project(store, { hasBlob: () => true }); 14+ project.init(START, 0); 15+ for (const a of ["ann", "bob", "cat"]) project.sync(a); 16+ return { project, store }; 19+/** ann publishes v1 (a.ts), bob v2 (b.ts), cat v3 (a new c.ts): three versions that don't build on each other. */ 20+function threeVersions(project: Project): void { 21+ project.edit("ann", { file: "a.ts", old: "two", new: "TWO" }, 1); 22+ project.publish("ann", "Shout two", 2); 23+ project.edit("bob", { file: "b.ts", old: "beta", new: "BETA" }, 3); 24+ project.publish("bob", "Shout beta", 4); 25+ project.record("cat", { "c.ts": "gamma\n" }, 5); 26+ project.publish("cat", "Add c", 6); 29+const badRequest = (f: () => unknown): string => { 33+ if (e instanceof ProjectError && e.code === "bad-request") return e.message; 39+test("leaving out an independent version takes only its change out; the others stay as they are", () => { 40+ const { project, store } = setup(); 41+ threeVersions(project); 42+ const entries = store.entries.length; 43+ const answer = project.without(3, [1]); 44+ assert.equal(answer.ok, true); 45+ assert.ok(answer.ok); 46+ assert.deepEqual(answer.without, [{ version: 1, agent: "ann", message: "Shout two" }]); 47+ assert.deepEqual(answer.files, [{ path: "a.ts", before: "one\nTWO\nthree\n", after: "one\ntwo\nthree\n" }]); 50+ treeHash({ files: { "a.ts": "one\ntwo\nthree\n", "b.ts": "alpha\nBETA\n", "c.ts": "gamma\n" } }), 52+ // Shared is unchanged: same version, same files, nothing written to the log. 53+ assert.equal(project.version, 3); 54+ assert.deepEqual(project.files(), { "a.ts": "one\nTWO\nthree\n", "b.ts": "alpha\nBETA\n", "c.ts": "gamma\n" }); 55+ assert.equal(store.entries.length, entries); 56+ // Two left out at once. 57+ const two = project.without(3, [3, 1]); 60+ two.without.map((w) => w.version), 64+ two.files.map((f) => [f.path, f.after]), 66+ ["a.ts", "one\ntwo\nthree\n"], 72+test("the same ask gives the same answer, and shared moving on doesn't change it", () => { 73+ const { project } = setup(); 74+ threeVersions(project); 75+ const first = project.without(3, [2]); 76+ assert.deepEqual(project.without(3, [2]), first); 78+ project.edit("ann", { file: "c.ts", old: "gamma", new: "GAMMA" }, 7); 79+ project.publish("ann", "Shout gamma", 8); 80+ assert.deepEqual(project.without(3, [2]), first); 81+ // Asked of the newer version, the newer change is in it. 82+ const later = project.without(4, [2]); 83+ assert.ok(later.ok && first.ok); 84+ assert.notEqual(later.tree, first.tree); 87+test("leaving out a version a kept one builds on is refused, with the chain, the files and the wider set", () => { 88+ const { project, store } = setup(); 89+ threeVersions(project); 91+ project.edit("cat", { file: "a.ts", old: "TWO", new: "TWO!" }, 7); 92+ project.publish("cat", "Louder", 8); // v4 builds on v1 94+ project.edit("bob", { file: "a.ts", old: "TWO!", new: "TWO!!" }, 9); 95+ project.publish("bob", "Louder still", 10); // v5 builds on v4 96+ const entries = store.entries.length; 97+ const refused = project.without(5, [1]); 98+ assert.deepEqual(refused, { 101+ without: [{ version: 1, agent: "ann", message: "Shout two" }], 103+ { version: 4, agent: "cat", message: "Louder", needs: [{ version: 1, files: ["a.ts"] }] }, 104+ { version: 5, agent: "bob", message: "Louder still", needs: [{ version: 4, files: ["a.ts"] }] }, 106+ suggestion: [1, 4, 5], 108+ assert.equal(store.entries.length, entries); 109+ assert.equal(project.version, 5); 110+ // The suggestion, asked for, works; it's never applied without being asked. 111+ const wider = project.without(5, refused.ok ? [] : refused.suggestion); 112+ assert.ok(wider.ok); 113+ assert.deepEqual(wider.files, [{ path: "a.ts", before: "one\nTWO!!\nthree\n", after: "one\ntwo\nthree\n" }]); 114+ // Before the dependent version, leaving it out is fine. 115+ assert.equal(project.without(3, [1]).ok, true); 118+test("a deletion left out brings the lines back; a rename left out keeps the old name; a move left out puts lines back", () => { 119+ const { project } = setup(); 120+ project.edit("ann", { file: "a.ts", old: "two\n", new: "" }, 1); 121+ project.publish("ann", "No two", 2); 122+ project.record("bob", { "b.ts": null, "c.ts": START["b.ts"] }, 3); 123+ project.publish("bob", "Rename b to c", 4); 124+ project.sync("cat"); 125+ project.move("cat", "a.ts", "src/a.ts", 5); 126+ project.publish("cat", "Move a into src", 6); 127+ assert.deepEqual(project.files(), { "src/a.ts": "one\nthree\n", "c.ts": "alpha\nbeta\n" }); 129+ const noDelete = project.without(3, [1]); 130+ assert.ok(noDelete.ok); 132+ noDelete.files.map((f) => [f.path, f.after]), 133+ [["src/a.ts", "one\ntwo\nthree\n"]], 135+ const noRename = project.without(3, [2]); 136+ assert.ok(noRename.ok); 138+ noRename.files.map((f) => [f.path, f.before, f.after]), 140+ ["b.ts", null, "alpha\nbeta\n"], 141+ ["c.ts", "alpha\nbeta\n", null], 144+ const noMove = project.without(3, [3]); 145+ assert.ok(noMove.ok); 147+ noMove.files.map((f) => [f.path, f.after]), 149+ ["a.ts", "one\nthree\n"], 155+test("a binary file's change and an executable bit left out go back to what they were", () => { 156+ const A = "a".repeat(64); 157+ const B = "b".repeat(64); 158+ const store = new MemoryStore(); 159+ const project = new Project(store, { hasBlob: () => true }); 160+ project.init({ "run.sh": "echo hi\n" }, 0, {}, { "logo.png": A }); 161+ project.sync("ann"); 162+ project.record("ann", {}, 1, {}, { "logo.png": B }); 163+ project.publish("ann", "New logo", 2); 164+ project.record("ann", {}, 3, { "run.sh": { exec: true } }); 165+ project.publish("ann", "Make run.sh runnable", 4); 166+ const noLogo = project.without(2, [1]); 167+ assert.ok(noLogo.ok); 169+ noLogo.files.map((f) => [f.path, f.blob]), 170+ [["logo.png", { before: B, after: A }]], 172+ const noExec = project.without(2, [2]); 173+ assert.ok(noExec.ok); 175+ noExec.files.map((f) => [f.path, f.mode?.after.exec ?? false]), 176+ [["run.sh", false]], 180+test("versions it can't leave out are refused plainly", () => { 181+ const { project } = setup(); 182+ threeVersions(project); 183+ assert.equal(project.drop("cat", [2], "Undo beta", 7).ok, true); // v4 drops v2 185+ badRequest(() => project.without(3, [4])), 186+ /comes after version 3/, 189+ badRequest(() => project.without(4, [0])), 190+ /version 0 is the start/, 193+ badRequest(() => project.without(4, [4])), 197+ badRequest(() => project.without(4, [2])), 198+ /isn't in version 4: version 4 dropped it/, 201+ badRequest(() => project.without(4, [])), 202+ /versions to leave out/, 204+ // Before the drop, it's there to leave out. 205+ assert.equal(project.without(3, [2]).ok, true);