Files
mesh-catalog/modules/gitea/test/taken.test.ts
T
jschoubben b93253e698 C2: the forge module takes an act of the seat git once by its ask id
Each act (status, comment, view, note, merge-pull, tag) carrying an ask id is
taken in the state taken, key git.<ask>, for the setting ask-taken-bound
(10m, read from MESH_CONFIG_ASK_TAKEN_BOUND): a repeat while the act runs
joins it; after it ended, the repeat answers what the forge holds of the act,
read afresh (a merge call's pull requests merged with their merge commits),
and the act is never done twice. An act that performed nothing drops its
key, so the kept call's repeat acts.

Builds: design 47 H2 §775, §820; H-A-381, H-A-400, H-A-359a
2026-11-22 00:51:01 +01:00

64 lines
4.4 KiB
TypeScript

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<string>();
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<void>((r) => { entered = r; });
const forge: Record<string, any> = {
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<void>((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<string, unknown>) =>
runAs({ "mesh/caller": "ace.x", "mesh/ask": ask }, args, () => seat.find((x: any) => x.name === `git.${verb}`)!.run(args)) as Promise<any>;
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]);
});