Versions 68 to 318
new file, 131 lines
1+// A held push from end to end, as two agents meet it: alice and bob change 2+// the same line, bob's push is held, he pulls, settles and pushes again. 3+// What each step prints has to be true, and the record has to keep his reason. 5+import assert from "node:assert/strict"; 6+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; 7+import { tmpdir } from "node:os"; 8+import { join } from "node:path"; 9+import { after, before, describe, it } from "node:test"; 10+import { main } from "../src/main.ts"; 11+import { type Fake, startFake } from "./fake-server.ts"; 13+const TOKEN = "test-token-not-secret"; 14+const ENV = { AVCS_TOKEN: TOKEN, AVCS_HOOK_FORCE: "1", AVCS_SERVERS_FILE: "no-servers.json" }; 15+const POLICY = "# Store policy\n\nFree shipping on orders over $50.\nReturns accepted within 30 days.\n"; 16+const HELD = "Lower free shipping to $35: half of abandoned carts were between $35 and $50"; 19+const dirs: string[] = []; 21+ fake = await startFake(0, TOKEN); 25+ for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 }); 28+function temp(): string { 29+ const d = mkdtempSync(join(tmpdir(), "abso-held-")); 34+async function run(cwd: string, ...argv: string[]): Promise<{ exit: number; out: string }> { 35+ const r = await main(argv, { cwd, env: ENV, write: () => {} }); 36+ return { exit: r.exit, out: r.stdout + r.stderr }; 40+/** alice pushes $75; bob's $35 push is held. Answers both folders and bob's held push. */ 41+async function held(): Promise<{ alice: string; bob: string; refused: string }> { 42+ const name = `held${++projects}`; 43+ const alice = temp(); 44+ writeFileSync(join(alice, "store-policy.md"), POLICY); 46+ (await run(alice, "init", "--server", fake.url, "--project", name, "--agent", "alice", "--as-is")).exit, 50+ assert.equal((await run(bob, "join", name, "--agent", "bob", "--server", fake.url)).exit, 0); 51+ writeFileSync(join(alice, "store-policy.md"), POLICY.replace("$50", "$75")); 52+ writeFileSync(join(bob, "store-policy.md"), POLICY.replace("$50", "$35")); 53+ const pushed = await run(alice, "push", "-m", "Raise free shipping to $75: shipping costs rose"); 54+ assert.equal(pushed.exit, 0, pushed.out); 55+ const refused = await run(bob, "push", "-m", HELD); 56+ assert.equal(refused.exit, 2, refused.out); 57+ return { alice, bob, refused: refused.out }; 60+const policy = (dir: string) => readFileSync(join(dir, "store-policy.md"), "utf8"); 62+describe("a held push", () => { 63+ it("says what the pull really writes: a marker block where both changed the same lines", async () => { 64+ const { bob, refused } = await held(); 65+ assert.doesNotMatch(refused, /no markers/); 66+ assert.match(refused, /Run abso pull: each of those files comes back holding both sides' lines/); 67+ assert.equal((await run(bob, "pull")).exit, 0); 68+ assert.match(policy(bob), /^<<<<<<< yours \(unpublished\)$/m, "the pull wrote a marker block"); 71+ for (const how of ["yours", "theirs", "both"] as const) { 72+ it(`why names the collision a line was kept from, settled --${how}`, async () => { 73+ const { alice, bob } = await held(); 74+ assert.equal((await run(bob, "pull")).exit, 0); 75+ const settled = await run(bob, "settle", "store-policy.md", `--${how}`); 76+ assert.equal(settled.exit, 0, settled.out); 77+ const pushed = await run(bob, "push", "-m", `Settle: keep ${how}`); 78+ assert.equal(pushed.exit, 0, pushed.out); 79+ assert.equal((await run(alice, "pull")).exit, 0); 80+ const why = await run(alice, "why", "store-policy.md:3"); 81+ assert.equal(why.exit, 0, why.out); 82+ assert.match(why.out, /It was kept when bob settled a collision between:\n {2}alice, version 1 by alice/); 83+ assert.match(why.out, new RegExp(`\\n {2}bob, a held push: "${HELD.replace(/\$/g, "\\$")}"`)); 87+ it("keeps its message: why on the settling line shows it, and so do the change page and the news", async () => { 88+ const { alice, bob } = await held(); 89+ assert.equal((await run(bob, "pull")).exit, 0); 90+ writeFileSync(join(bob, "store-policy.md"), POLICY.replace("$50", "$60")); 91+ const pushed = await run(bob, "push", "-m", "Settle with alice: meet at $60"); 92+ assert.equal(pushed.exit, 0, pushed.out); 93+ const why = await run(bob, "why", "store-policy.md:3"); 96+ /It settled a collision between:\n {2}alice, version 1 by alice[^\n]*\n {2}bob, a held push: "/, 98+ assert.ok(why.out.includes(HELD), why.out); 99+ const review = await run(alice, "review", "bob"); 100+ assert.match(review.out, /Settled: store-policy\.md \(rewrote both sides, against alice's version 1\)\./); 101+ assert.ok(review.out.includes(`Refused 1 time on the way: lines (store-policy.md): "${HELD}"`), review.out); 102+ const news = await run(alice, "news"); 103+ assert.ok(news.out.includes(`Its held push said: "${HELD}"`), news.out); 106+ it("a pull doesn't drop the news of what a settle kept", async () => { 107+ const { alice, bob } = await held(); 108+ assert.equal((await run(bob, "pull")).exit, 0); 109+ assert.equal((await run(bob, "settle", "store-policy.md", "--both")).exit, 0); 110+ assert.equal((await run(bob, "push", "-m", "Settle: keep both")).exit, 0); 111+ const pulled = await run(alice, "pull"); 112+ assert.equal(pulled.exit, 0, pulled.out); 113+ const news = await run(alice, "news"); 114+ assert.match(news.out, /bob settled its collision with your version 1 in store-policy\.md, pushing version 2/); 117+ it("the change page says what a settle kept in words that read for each choice", async () => { 118+ for (const [how, said] of [ 119+ ["yours", "kept bob's"], 120+ ["theirs", "kept alice's"], 121+ ["both", "kept both"], 123+ const { alice, bob } = await held(); 124+ assert.equal((await run(bob, "pull")).exit, 0); 125+ assert.equal((await run(bob, "settle", "store-policy.md", `--${how}`)).exit, 0); 126+ assert.equal((await run(bob, "push", "-m", `Settle: keep ${how}`)).exit, 0); 127+ const review = await run(alice, "review", "bob"); 128+ assert.ok(review.out.includes(`Settled: store-policy.md (${said}, against alice's version 1).`), review.out);