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. 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 = { 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. 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 = { 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"); });