import assert from "node:assert/strict"; import { test } from "node:test"; // 47 H2 ยง775, H-A-381, H-A-400: the forge module takes an act of the seat git once by its ask id. Given the same ask id // twice it acts once: a repeat while the act runs joins it, a repeat after it ended answers what the forge holds of the // act; an act that performed nothing (away, a refusal before any merge) drops its key, so the repeat acts. test("an act of the seat git given the same ask id twice acts once", async () => { const { seatTools } = await import("../dist/seat.js"); const { Once, durationMs, askTakenBoundMs } = await import("../dist/taken.js"); const { runAs } = await import("@novox/mesh-sdk/callers"); const SHA = "a".repeat(40), MERGE = "c".repeat(40); const keys = new Set(); const taken = { take: async (ask: string) => (keys.has(ask) ? false : (keys.add(ask), true)), drop: async (ask: string) => { keys.delete(ask); } }; let merges = 0, comments: { id: number; body: string }[] = [], added = 0, away = false; const pr = () => ({ number: 9, state: merges ? "closed" : "open", merged: merges > 0, head: "feat/x", head_sha: SHA, base: "main", ...(merges ? { merge_commit_sha: MERGE } : {}) }); let release: () => void = () => {}, entered: () => void = () => {}; const merging = new Promise((r) => { entered = r; }); const forge: Record = { forgeVersion: async () => (away ? { ok: false, words: "no answer", store: true } : { ok: true, version: "1.27.3" }), getPullRequest: async () => { if (away) throw new Error("Gitea API /repos/novox/app/pulls/9: 503 Service Unavailable"); return pr(); }, mergePullRequest: async () => { const held = new Promise((r) => { release = r; }); entered(); await held; merges++; }, commitParents: async () => [SHA, SHA], listComments: async () => comments, addComment: async (_o: string, _r: string, _n: number, body: string) => { added++; comments.push({ id: added, body }); }, }; const seat = seatTools(forge as any, { run: (async () => ({ stdout: "", code: 1 })) as any, container: "gitea", registered: () => true, judge: async () => null, watched: () => new Map([["novox/app", new Set(["main"])]]), once: new Once(taken) }); const call = (verb: string, ask: string, args: Record) => runAs({ "mesh/caller": "ace.x", "mesh/ask": ask }, args, () => seat.find((x: any) => x.name === `git.${verb}`)!.run(args)) as Promise; const merge = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 9, head: SHA }] }; // Away first: nothing performed, the key dropped, so the kept call's repeat under the same id acts (H-A-400 (1)). away = true; assert.equal((await call("merge-pull", "ask1", merge))["error-kind"], "away"); assert.equal(keys.has("ask1"), false, "an act that performed nothing keeps no taken key"); away = false; // The repeat while the first runs joins it: one merge, both answered the same. const first = call("merge-pull", "ask1", merge); await merging; const joined = call("merge-pull", "ask1", merge); release(); const [a, b] = await Promise.all([first, joined]); assert.equal(merges, 1, "a repeat while the act runs joins it"); assert.deepEqual(b, a); assert.equal(a.members[0].state, "merged"); // After it ended, the repeat answers the pull request as the forge holds it, merged with its merge commit, and merges nothing. const again = await call("merge-pull", "ask1", merge); assert.deepEqual([merges, again.members], [1, [{ repository: "novox/app", number: 9, state: "merged", commit: MERGE }]]); // A comment given its ask again is not written twice; a repeat whose comment the forge does not hold is refused, naming it. const comment = { id: "c1", repository: "novox/app", number: 9, text: "hi" }; assert.equal((await call("comment", "ask2", comment)).performed, true); assert.deepEqual([(await call("comment", "ask2", comment)).held, added], [true, 1]); comments = []; const gone = await call("comment", "ask2", comment); assert.deepEqual([gone["error-kind"], /ask2 was taken/.test(gone.error), added], ["refused", true, 1]); // ask-taken-bound is read from the word the node-engine composes (H-A-400 (2)), 10 minutes by default. assert.deepEqual([askTakenBoundMs({}), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "90s" }), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "soon" }), durationMs("1h30m")], [600_000, 90_000, 600_000, 5_400_000]); });