Files
mesh-catalog/modules/gitea/test/merge-call.test.ts
T
jschoubben 7ae223d4ff gitea: merge-pull merges plainly only from a watched-branches view read since the last change
An unread or stale view held no entry for a watched repository, so every
member would have merged plainly with the note no delivery: a merge that
reports success and delivers nothing. The registrations holder now keeps
the time of its last read and of the last registration-changed heard; the
merge call refuses every member ("the watched branches are not read yet")
while the view is not read since that change, and a read that fails is
said and asked again with the module's growing wait until it answers.

Builds: design 47 H2 H-A-471 (second addendum); review finding H-A-471 addendum (2)
2026-11-22 14:14:09 +01:00

197 lines
13 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);
}
});
// **A repository of no module is merged plainly** (design 47 H2 H-A-471 and its addendum): a member whose repository has
// no watched branch (no entry, or an empty set, in the registrations' watched branches) is judged against nothing of the
// module graph (no failed required step), merged in the order named, answered merged with the note no delivery, and left
// out of merge-called; the watched members of the same call are judged and delivered as any call's. A watched
// repository whose base is unwatched stays refused (the row above).
test("a member of a repository with no watched branch is merged plainly, noted no delivery, and left out of merge-called", async () => {
const NOTE = "no delivery: the repository has no watched branch";
const rows: { name: string; watched: Map<string, Set<string>> }[] = [
{ name: "no entry", watched: new Map([["novox/app", new Set(["main"])]]) },
{ name: "an empty set", watched: new Map([["novox/app", new Set(["main"])], ["novox/hq", new Set<string>()]]) },
];
for (const r of rows) {
const pulls = open();
pulls["novox/hq#799"] = { base: "main", head: B, state: "open", merged: false };
const f = forge(pulls);
const judged: string[] = [];
const said: any[] = [];
const mixed = { by: "lead@shanks", pulls: [{ repository: "novox/hq", number: 799, head: B }, two.pulls[0]] };
const got = await call(f, mixed, { watched: () => r.watched, say: async (_s: string, body: unknown) => { said.push(body); },
judge: async (h: string) => { judged.push(h); return { run: "main.7", module: "hq", step: "unit", fingerprint: "fp" }; } });
assert.deepEqual(f.merges, [], `${r.name}: a refused watched member merges none`);
assert.deepEqual(judged, [A], `${r.name}: the unwatched member is judged against nothing of the module graph`);
// The watched member's failed required step refuses the call whole, the unwatched one judged plainly beside it.
assert.equal(got.members[0].state, "judged", r.name);
assert.equal(got.members[1].state, "refused", r.name);
// Without the failed step the call lands both: the unwatched noted, merge-called naming only the watched.
const f2 = forge(Object.assign(open(), { "novox/hq#799": { base: "main", head: B, state: "open", merged: false } }));
said.length = 0;
const landed = await call(f2, mixed, { watched: () => r.watched, say: async (_s: string, body: unknown) => { said.push(body); } });
assert.deepEqual(f2.merges, ["novox/hq#799", "novox/app#5"], `${r.name}: merged in the order named`);
assert.deepEqual(landed.members.map((m: any) => [m.repository, m.state, m.note, m.why]),
[["novox/hq", "merged", NOTE, undefined], ["novox/app", "merged", undefined, undefined]], r.name);
assert.deepEqual(said.map((b) => b.members.map((m: any) => m.repository)), [["novox/app"]], `${r.name}: merge-called leaves it out`);
// A call of unwatched members alone merges and starts no delivery: no merge-called.
const f3 = forge({ "novox/hq#799": { base: "main", head: B, state: "open", merged: false } });
said.length = 0;
const alone = await call(f3, { by: "lead@shanks", pulls: [mixed.pulls[0]] }, { watched: () => r.watched,
say: async (_s: string, body: unknown) => { said.push(body); } });
assert.deepEqual([f3.merges, alone.members[0].state, alone.members[0].note, said], [["novox/hq#799"], "merged", NOTE, []], r.name);
}
});
// **A plain merge needs a view read since the last change heard** (design 47 H2 H-A-471, second addendum): while the
// watched branches are unread, or were read before a later registration-changed, every member is refused ("the watched
// branches are not read yet"), never merged with the note; a view read after the change merges the unwatched member
// plainly. A read that fails is said and asked again until it answers.
test("a member is merged plainly only from a watched-branches view read since the last registration-changed", async () => {
const { Registrations } = await import("../dist/registrations.js");
const NOT_READ = "the watched branches are not read yet";
const said: string[] = [];
const waits: number[] = [];
let answer: unknown = new Error("no responders");
const reg = new Registrations(async () => { if (answer instanceof Error) throw answer; return answer; }, (l) => said.push(l),
async (ms) => { waits.push(ms); answer = { registrations: [{ module: "app", origin: "novox/app", branches: ["main"] }] }; });
const hq = { by: "lead@shanks", pulls: [{ repository: "novox/hq", number: 799, head: B }, two.pulls[0]] };
const pulls = () => Object.assign(open(), { "novox/hq#799": { base: "main", head: B, state: "open", merged: false } });
const view = () => (reg.current ? reg.watched : null);
reg.hear({ module: "app", moved: ["watch"], version: 1, warrant: "q-1" });
// Unread: every member refused, nothing merged.
const f1 = forge(pulls());
const unread = await call(f1, hq, { watched: view });
assert.deepEqual([f1.merges, unread.members.map((m: any) => [m.state, m.why, m.note])],
[[], [["refused", NOT_READ, undefined], ["refused", NOT_READ, undefined]]], "an unread view refuses every member");
// A failed read is said and asked again until it answers; read, the view is current.
await reg.start();
assert.deepEqual([waits, reg.current], [[2_000], true]);
assert.match(said.join("\n"), /could not be read: no responders; no member is merged plainly until they are, asked again in 2s/);
assert.ok(reg.readAt && reg.changedAt && reg.readAt >= reg.changedAt, "the times of the last read and the last change are kept");
// A registration-changed heard after the read: stale, every member refused again.
reg.hear({ module: "app", moved: ["watch"], version: 2, warrant: "q-2" });
const f2 = forge(pulls());
const stale = await call(f2, hq, { watched: view });
assert.deepEqual([f2.merges, stale.members.map((m: any) => m.why)], [[], [NOT_READ, NOT_READ]], "a view read before a later change refuses");
// A change heard while a read is asked leaves the view stale.
let release!: () => void;
answer = { registrations: [{ module: "app", origin: "novox/app", branches: ["main"] }] };
const slow = new Registrations(() => new Promise((r) => { release = () => r(answer); }));
const asking = slow.start();
slow.hear({ module: "app", moved: ["watch"], version: 3, warrant: "q-3" });
release();
await asking;
assert.equal(slow.current, false, "a read asked before the change heard is not since it");
// Read again after the change: the unwatched member merges plainly with the note, the watched one as any call's.
await reg.start();
const f3 = forge(pulls());
const landed = await call(f3, hq, { watched: view });
assert.deepEqual([f3.merges, landed.members.map((m: any) => [m.state, m.note])],
[["novox/hq#799", "novox/app#5"], [["merged", "no delivery: the repository has no watched branch"], ["merged", undefined]]]);
});
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/);
});