Files
mesh-catalog/modules/gitea/test/calls.test.ts
T
jschoubben ce04011f96 gitea: a member whose base is no watched branch is merged plainly with the note, not refused
G1b unwatched-base-merged: judgeMember marks a member plain when its base is no
watched branch, whatever its repository; the note's words are the one constant
for both cases (DQ-49); the refusal row leaves the table and a row is added.

Builds: design 47 H2 H-A-704, H-A-717 (DQ-49)
2026-11-25 17:05:52 +01:00

153 lines
12 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { memory } from "./memstate.ts";
// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): the key
// call.<call> of the state calls holds by, the proved account, at, each member with its base and its state (judged before
// the first merge, merged with its commit as it merges) and said; a call past call-retention is deleted on a new call's
// write, oldest first.
const A = "a".repeat(40), B = "b".repeat(40);
test("a merge call is kept before its first merge, each member judged, then merged with its commit", async () => {
const { mergeCall } = await import("../dist/mergecall.js");
const { Calls } = await import("../dist/calls.js");
const { runAs } = await import("@novox/mesh-sdk/callers");
const { kv, st } = memory();
const calls = new Calls(st as any);
const seenAtMerge: any[] = [];
const pulls: Record<string, any> = { 5: { head: A, merged: false }, 9: { head: B, merged: false } };
const forge = {
getPullRequest: async (_o: string, r: string, n: number) => ({ number: n, state: "open", merged: pulls[n].merged, base: "main",
head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }),
mergePullRequest: async (_o: string, _r: string, n: number) => {
seenAtMerge.push(structuredClone(kv.get("call.0123456789abcdef")));
pulls[n].merged = true;
},
commitParents: async () => [],
};
const args = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/app", number: 9, head: B }] };
const at = new Date("2026-11-20T03:00:00Z");
await runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, { registered: () => true,
watched: () => new Map([["novox/app", new Set(["main"])]]), judge: async () => null, newCall: () => "0123456789abcdef",
calls, now: () => at } as any));
const first = seenAtMerge[0];
assert.deepEqual([first.by, first.account, first.at, first.said], ["lead@shanks", "shanks.claude-code", at.toISOString(), false]);
assert.deepEqual(first.members.map((m: any) => [m.number, m.branch, m.head, m.state]), [[5, "main", A, "judged"], [9, "main", B, "judged"]]);
assert.deepEqual(seenAtMerge[1].members.map((m: any) => [m.state, m.commit]), [["merged", "5".repeat(40)], ["judged", undefined]]);
const kept = await calls.get("0123456789abcdef");
assert.deepEqual(kept?.members.map((m: any) => m.state), ["merged", "merged"]);
});
test("a call past call-retention is deleted on a new call's write, oldest first; a younger one is kept", async () => {
const { Calls } = await import("../dist/calls.js");
const { kv, st } = memory();
const calls = new Calls(st as any, () => 30);
const call = (at: string) => ({ by: "a@b", account: "x", at, members: [], said: true });
await calls.put("1111111111111111", call("2026-09-01T00:00:00Z"), new Date("2026-09-01T00:00:00Z"));
await calls.put("2222222222222222", call("2026-10-25T00:00:00Z"), new Date("2026-10-25T00:00:00Z"));
await calls.put("3333333333333333", call("2026-11-20T00:00:00Z"), new Date("2026-11-20T00:00:00Z"));
assert.deepEqual([...kv.keys()].sort(), ["call.2222222222222222", "call.3333333333333333"]);
assert.deepEqual((await calls.all()).map((c) => c.call), ["3333333333333333", "2222222222222222"], "newest first");
});
// **The merge call says one event** (design 47 H §736, §766; test 111's stop case): merge-called on merge-called.<call> with
// the landed members, each with its base and merge commit; a call that landed none says none; at the start a call whose
// event is not said (a stop after the second of three merges) says one naming the two that landed, and is kept said.
test("a call says one merge-called of what landed; none for a call that landed none; the start says an unsaid one", async () => {
const { mergeCall } = await import("../dist/mergecall.js");
const { Calls, sayUnsaid } = await import("../dist/calls.js");
const { runAs } = await import("@novox/mesh-sdk/callers");
const said: { subject: string; body: any; id?: string }[] = [];
const say = async (subject: string, body: unknown, id?: string) => { said.push({ subject, body, id }); };
const { st } = memory();
const calls = new Calls(st as any);
const pulls: Record<number, any> = { 5: { head: A, merged: false, state: "open" }, 9: { head: B, merged: false, state: "closed" } };
const forge = {
getPullRequest: async (_o: string, _r: string, n: number) => ({ number: n, state: pulls[n].state, merged: pulls[n].merged, base: "main",
head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }),
mergePullRequest: async (_o: string, _r: string, n: number) => { pulls[n].merged = true; },
commitParents: async () => [],
};
const deps = (call: string) => ({ registered: () => true, watched: () => new Map([["novox/app", new Set(["main"])]]),
judge: async () => null, newCall: () => call, calls, say, now: () => new Date("2026-11-20T03:00:00Z") });
const run = (call: string, numbers: number[]) => {
const args = { by: "lead@shanks", pulls: numbers.map((n) => ({ repository: "novox/app", number: n, head: pulls[n].head })) };
return runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, deps(call) as any));
};
await run("00000000000000aa", [5, 9]); // refused at judging: no event
assert.deepEqual(said, []);
await run("00000000000000bb", [5]);
assert.equal(said.length, 1);
assert.deepEqual([said[0].subject, said[0].id], ["mesh.seat.git.event.merge-called.00000000000000bb", "merge-called.00000000000000bb"]);
assert.deepEqual(said[0].body, { call: "00000000000000bb", by: "lead@shanks", account: "shanks.claude-code", at: "2026-11-20T03:00:00.000Z",
members: [{ repository: "novox/app", number: 5, branch: "main", head: A, commit: "5".repeat(40) }] });
assert.equal((await calls.get("00000000000000bb"))?.said, true);
// A stop after the second of three merges: kept, not said.
const m = (n: number, state: string) => ({ repository: "novox/app", number: n, branch: "main", head: A, state, ...(state === "merged" ? { commit: String(n).repeat(40) } : {}) });
await calls.put("00000000000000cc", { by: "lead@shanks", account: "x", at: "2026-11-20T03:01:00Z", members: [m(1, "merged"), m(2, "merged"), m(3, "judged")], said: false } as any);
said.length = 0;
assert.deepEqual(await sayUnsaid(calls, say), ["00000000000000cc"]);
assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[1, 2]]);
assert.equal(said[0].body.refused, undefined, "a member not merged after a stop is no refused member");
assert.deepEqual(await sayUnsaid(calls, say), [], "said once");
// A member merged plainly, its base being no watched branch, starts no delivery: the start's re-say leaves it
// out by the kept note (design 47 H2 H-A-471 addendum (2)), and a call of such members alone says none.
const noted = { ...m(4, "merged"), repository: "novox/hq", note: "no delivery: its base is no watched branch" };
await calls.put("00000000000000dd", { by: "lead@shanks", account: "x", at: "2026-11-20T03:02:00Z", members: [noted, m(5, "merged")], said: false } as any);
await calls.put("00000000000000ee", { by: "lead@shanks", account: "x", at: "2026-11-20T03:03:00Z", members: [noted], said: false } as any);
said.length = 0;
assert.deepEqual(await sayUnsaid(calls, say), ["00000000000000dd"]);
assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[5]]);
assert.equal((await calls.get("00000000000000ee"))?.said, true, "kept said, with nothing to say");
});
// The merge calls' tools (design 47 H §766): forge_calls answers the calls newest first with each member's state, and
// forge_call_say_again says a call's merge-called again once the hand-act log took it.
test("forge_calls reads the calls newest first; forge_call_say_again says one again after the hand-act log", async () => {
const { Calls } = await import("../dist/calls.js");
const { Partials } = await import("../dist/partials.js");
const { callTools } = await import("../dist/tools/calls.js");
const { st } = memory();
const calls = new Calls(st as any);
const m = (n: number, state: string) => ({ repository: "novox/app", number: n, branch: "main", head: A, state, ...(state === "merged" ? { commit: String(n).repeat(40) } : {}) });
await calls.put("00000000000000aa", { by: "a@b", account: "x", at: "2026-11-20T01:00:00Z", members: [m(1, "merged")], said: true } as any);
await calls.put("00000000000000bb", { by: "a@b", account: "x", at: "2026-11-20T02:00:00Z", members: [m(2, "refused")], said: true } as any);
const said: any[] = [], acts: any[] = [];
const tools = callTools(calls, new Partials(memory().st as any, calls), async (subject: string, body: unknown) => { said.push([subject, body]); },
async (a) => { acts.push(a); });
const tool = (name: string) => tools.find((t) => t.name === name)!;
const read: any = await tool("forge_calls").run({});
assert.deepEqual(read.calls.map((c: any) => [c.call, c.members[0].state]), [["00000000000000bb", "refused"], ["00000000000000aa", "merged"]]);
await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000aa" }), /names the call .* and why/);
await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000ff", why: "x" }), /keeps no merge call/);
const again: any = await tool("forge_call_say_again").run({ call: "00000000000000aa", why: "the gate missed it" });
assert.deepEqual([again.said, acts[0].verb, said[0][0]], [true, "forge_call_say_again", "mesh.seat.git.event.merge-called.00000000000000aa"]);
const none: any = await tool("forge_call_say_again").run({ call: "00000000000000bb", why: "x" });
assert.equal(none.said, false, "a call that landed none says none");
});
// 47 H2 §812, §811, H-A-402: forge_merged lists a repository's merged pull requests by number, from..to, each with its merge
// commit, when, and the merge call that made it; a number the forge does not hold or one not merged is left out; capped at
// 768 KB with the next number as the cursor.
test("forge_merged lists merged pull requests by number with the merge call that made each", async () => {
const { mergedTool, ANSWER_CAP_BYTES } = await import("../dist/tools/calls.js");
const sha = (n: number) => String(n % 10).repeat(40);
const forge = { getPullRequest: async (_o: string, _r: string, n: number) => {
if (n === 3) throw new Error("Gitea API /repos/novox/app/pulls/3: 404 Not Found");
return n === 4 ? { number: n, state: "open", merged: false } : { number: n, state: "closed", merged: true, merge_commit_sha: sha(n), merged_at: `2026-11-2${n}T10:00:00Z` };
} };
const calls = { all: async () => [{ call: "00000000000000aa", members: [{ repository: "novox/app", number: 2, state: "merged" }, { repository: "novox/lib", number: 1, state: "merged" }] }] };
const got: any = await mergedTool(forge as any, calls as any).run({ repository: "novox/app", from: 1, to: 5 });
assert.deepEqual(got, { repository: "novox/app", from: 1, to: 5, total: 5, merged: [
{ number: 1, "merge-commit": sha(1), "merged-at": "2026-11-21T10:00:00Z" },
{ number: 2, "merge-commit": sha(2), "merged-at": "2026-11-22T10:00:00Z", call: "00000000000000aa" },
{ number: 5, "merge-commit": sha(5), "merged-at": "2026-11-25T10:00:00Z" }] });
await assert.rejects(mergedTool(forge as any, calls as any).run({ repository: "novox/app", from: 5, to: 4 }), /to not below from/);
// Past 768 KB it is capped, the cursor the next number to read from.
const many: any = await mergedTool(forge as any, { all: async () => [] } as any).run({ repository: "novox/app", from: 5, to: 20_000 });
assert.equal(many.capped, true);
assert.ok(JSON.stringify(many.merged).length <= ANSWER_CAP_BYTES);
assert.equal(many.cursor, many.merged.at(-1).number + 1);
});