import { admitWaiter, runBarrier, writingSeats } from "../../../tools/core/coordinators/board.coordinator.ts"; import { describe, it } from "node:test"; import { mkdtempSync, writeFileSync } from "node:fs"; import assert from "node:assert/strict"; import { currentWaiters } from "../../../tools/core/registries/board.registry.ts"; import { join } from "node:path"; import { tmpdir } from "node:os"; const NOW = 1_000_000; const TIMEOUT = 5000; const BOARD = "Agent A — ACTIVE\n Owns: tools\nAgent B — ACTIVE\n Owns: config\n"; const rootWithBoard = function rootWithBoard(): { root: string; board: string } { const root = mkdtempSync(join(tmpdir(), "coordination-board-")); const board = join(root, "collab.comms.active"); writeFileSync(board, BOARD); return { board, root }; }; describe("writingSeats", () => { it("keeps a parked seat and drops a seat with no claim and no recent interaction", () => { const { root } = rootWithBoard(); assert.deepEqual(writingSeats(root, ["A", "B"], ["B"], NOW), ["B"]); }); }); describe("runBarrier", () => { it("reports the barrier open when the board is missing and nobody is counted", () => { const root = mkdtempSync(join(tmpdir(), "coordination-barrier-")); assert.equal(runBarrier(root, join(root, "absent.board"), NOW).code, 1); }); }); describe("admitWaiter", () => { it("parks the first waiter, then refuses a second one when every counted seat is already parked", () => { const { root, board } = rootWithBoard(); const first = admitWaiter(root, board, { agent: "A", now: NOW, pid: 7, target: board, timeout: TIMEOUT }); assert.equal(first.code, 0); assert.equal(first.id, `7-${String(NOW)}`); assert.deepEqual( currentWaiters(root, NOW).map((waiter) => waiter.agent), ["A"], ); const second = admitWaiter(root, board, { agent: "B", now: NOW, pid: 8, target: board, timeout: TIMEOUT }); assert.equal(second.code, 3); assert.equal(second.id, ""); }); });