Files
mesh-catalog/modules/gitea/test/merge-call.test.ts
T
jschoubben 40dc5fe11d FORGE-1: merge-pull is one merge call, every member judged before any is merged
merge-pull takes pulls and by, refuses a call with no proved caller,
judges each member (its head, its base a watched branch, not merged
already, no required step failed at its fingerprint) and no branch
protection, merges none when one is refused, else merges in the order
named and answers the call and each member's outcome once. The seat's
branch answers no protection. The seen-expiry test no longer depends on
today's date.

Builds: design 47 H §734, §735, §764 (the single-pull form; merge-pull's protection judgement); ADR 0682 §13; H-A-21, H-A-22, H-A-24, H-A-47

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 03:42:00 +01:00

107 lines
6.3 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
// **One merge call, every member judged before any is merged** (novox/hq design 47 H §734, §735; ADR 0682 §13; H-A-21,
// H-A-22, H-A-47): the seat git's merge-pull takes pulls and by, judges each member (its head, its base a watched branch,
// not merged already, no required step failed at its fingerprint) and no branch protection, merges none when one is
// refused, else merges them in the order named, and answers the call and each member's outcome once.
const A = "a".repeat(40), B = "b".repeat(40), MOVED = "c".repeat(40);
const CALL = "0123456789abcdef";
interface FakePull { base: string; head: string; state: string; merged: boolean; merge?: string; refuse?: string }
/** A forge of pull requests by `<repository>#<number>`, recording each merge asked. It has no protection API: a call that
* read a branch's protection would throw. */
function forge(pulls: Record<string, FakePull>) {
const merges: string[] = [];
const key = (o: string, r: string, n: number) => `${o}/${r}#${n}`;
return {
merges,
getPullRequest: async (o: string, r: string, n: number) => {
const p = pulls[key(o, r, n)];
if (!p) throw new Error(`Gitea API GET /repos/${o}/${r}/pulls/${n}: 404 {}`);
return { number: n, title: "", state: p.state, merged: p.merged, base: p.base, head: "b", head_sha: p.head,
...(p.merged ? { merge_commit_sha: p.merge ?? "m".repeat(40) } : {}) } as any;
},
mergePullRequest: async (o: string, r: string, n: number, _m: string, _d: boolean, head: string) => {
const p = pulls[key(o, r, n)];
merges.push(key(o, r, n));
if (p.refuse) throw new Error(`Gitea API POST /repos/${o}/${r}/pulls/${n}/merge: 409 ${p.refuse}`);
assert.equal(head, p.head);
p.merged = true;
p.merge = `${n}`.repeat(40).slice(0, 40);
},
commitParents: async (_o: string, _r: string, sha: string) => [sha, "x"],
};
}
const watched = new Map([["novox/app", new Set(["main"])], ["novox/lib", new Set(["main"])]]);
async function call(f: ReturnType<typeof forge>, args: Record<string, unknown>, over: Record<string, unknown> = {}, caller = "person.jochen") {
const { mergeCall } = await import("../dist/mergecall.js");
const { runAs } = await import("@novox/mesh-sdk/callers");
const deps = { registered: () => true, watched: () => watched, judge: async () => null, newCall: () => CALL, ...over };
return runAs({ "mesh/caller": caller }, args, () => mergeCall(f as any, args, deps as any)) as Promise<any>;
}
const two = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/lib", number: 9, head: B }] };
const open = (): Record<string, FakePull> => ({ "novox/app#5": { base: "main", head: A, state: "open", merged: false },
"novox/lib#9": { base: "main", head: B, state: "open", merged: false } });
test("a merge call merges its members in the order named and answers each with its merge commit; one pull request is a list of one", async () => {
const f = forge(open());
const got = await call(f, two);
assert.equal(got.call, CALL);
assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"]);
assert.deepEqual(got.members.map((m: any) => [m.repository, m.number, m.state, m.commit]),
[["novox/app", 5, "merged", "5".repeat(40)], ["novox/lib", 9, "merged", "9".repeat(40)]]);
const one = forge(open());
const single = await call(one, { by: "lead@shanks", pulls: [two.pulls[0]] });
assert.deepEqual([one.merges, single.members.length, single.members[0].state], [["novox/app#5"], 1, "merged"]);
});
test("a member refused at judging merges none and names every refused member and why", async () => {
const rows: { name: string; set: (p: Record<string, FakePull>) => void; over?: Record<string, unknown>; why: RegExp }[] = [
{ name: "the second member's head moved", set: (p) => { p["novox/lib#9"].head = MOVED; }, why: /head-moved: its head is cccccccccccc, not the bbbbbbbbbbbb/ },
{ name: "already merged", set: (p) => { Object.assign(p["novox/lib#9"], { merged: true, merge: "e".repeat(40) }); }, why: /already-merged: .* as e{40}/ },
{ name: "closed", set: (p) => { p["novox/lib#9"].state = "closed"; }, why: /is closed/ },
{ name: "a base no registration watches", set: (p) => { p["novox/lib#9"].base = "feat/x"; }, why: /its base feat\/x is no watched branch/ },
{ name: "a required step failed at its head's fingerprint",
set: () => {}, over: { judge: async (h: string) => (h === B ? { run: "main.7", module: "lib", step: "unit", fingerprint: "fp" } : null) },
why: /the unit step of lib failed in run main\.7 at its build-input fingerprint fp/ },
];
for (const r of rows) {
const pulls = open();
r.set(pulls);
const f = forge(pulls);
const got = await call(f, two, r.over);
assert.deepEqual(f.merges, [], `${r.name}: nothing merged`);
assert.equal(got.members[0].state, "judged", r.name);
assert.equal(got.members[1].state, "refused", r.name);
assert.match(got.members[1].why, r.why, r.name);
}
});
test("a call is refused whole with no proved caller, no by, no registration or no pull request", async () => {
const f = forge(open());
await assert.rejects(call(f, two, {}, ""), /proved no caller account/);
await assert.rejects(call(f, { pulls: two.pulls }), /by is the caller as who@node/);
await assert.rejects(call(f, two, { registered: () => false }), /no module is registered yet/);
await assert.rejects(call(f, { by: "lead@shanks", pulls: [] }), /one or more members/);
await assert.rejects(call(f, { by: "lead@shanks", pulls: [two.pulls[0], two.pulls[0]] }), /named twice/);
assert.deepEqual(f.merges, []);
});
test("the forge refusing a member after every member was judged stops the call: the landed member merged, the rest refused", async () => {
const pulls = open();
pulls["novox/lib#9"].refuse = "merge conflict";
const three = { by: "lead@shanks", pulls: [...two.pulls, { repository: "novox/app", number: 6, head: A }] };
pulls["novox/app#6"] = { base: "main", head: A, state: "open", merged: false };
const f = forge(pulls);
const got = await call(f, three);
assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"], "the third is never asked");
assert.deepEqual(got.members.map((m: any) => m.state), ["merged", "refused", "refused"]);
assert.match(got.members[1].why, /the forge refused the merge: .*merge conflict/);
});