import test from "node:test"; import assert from "node:assert/strict"; import { Limits } from "../src/Limits.js"; import { LIMITS, REFUSALS } from "../src/spec.js"; const MB = 1024 * 1024; function at(hour, minute = 0) { return () => new Date(2026, 0, 15, hour, minute); } test("defaults come from the baked spec", () => { const limits = new Limits(); assert.equal(limits.concurrency, LIMITS.max_concurrent_per_node); assert.equal(limits.bandwidthBytes, LIMITS.default_donor_mb_per_day * MB); assert.equal(limits.maxBodyBytes, LIMITS.max_body_bytes); }); test("local settings are clamped", () => { const limits = new Limits({ local: { bandwidthMb: 10 ** 9, maxConcurrent: 99 } }); assert.equal(limits.local.bandwidthMb, LIMITS.max_donor_mb_per_day); assert.equal(limits.local.maxConcurrent, LIMITS.max_concurrent_per_node); limits.setLocal({ bandwidthMb: -5, maxConcurrent: 0 }); assert.equal(limits.local.bandwidthMb, LIMITS.default_donor_mb_per_day); assert.equal(limits.local.maxConcurrent, LIMITS.max_concurrent_per_node); limits.setLocal({ activeFrom: "25:99", activeTo: "10:00" }); assert.equal(limits.local.activeFrom, ""); }); test("a welcome can lower caps and never raise them", () => { const limits = new Limits(); limits.applyWelcome( { max_body_bytes: 10 ** 9, max_concurrent_per_node: 1, job_timeout_seconds: 5, ping_seconds: 999, reconnect_min_seconds: 2, reconnect_max_seconds: 10 ** 6, }, { bandwidth_mb_day: 5, max_concurrent: 99 }, ); assert.equal(limits.maxBodyBytes, LIMITS.max_body_bytes); assert.equal(limits.concurrency, 1); assert.equal(limits.timeoutSeconds(25), 5); assert.equal(limits.pingSeconds, LIMITS.ping_seconds); assert.equal(limits.bandwidthBytes, 5 * MB); assert.equal(limits.reconnectMin, 2); assert.equal(limits.reconnectMax, LIMITS.reconnect_max_seconds); }); test("concurrency cap admits and releases", () => { const limits = new Limits({ local: { maxConcurrent: 1 } }); assert.equal(limits.admit(0), null); assert.equal(limits.admit(0), REFUSALS.CONCURRENCY_EXCEEDED); limits.release(); assert.equal(limits.admit(0), null); limits.release(); limits.release(); assert.equal(limits.inflight, 0); }); test("bandwidth cap refuses when used up", () => { const limits = new Limits({ local: { bandwidthMb: 1 } }); assert.equal(limits.admit(MB - 1), null); limits.release(); assert.equal(limits.admit(MB), REFUSALS.BANDWIDTH_EXHAUSTED); assert.equal(limits.remainingBytes(2 * MB), 0); }); test("active hours, including a window that wraps midnight", () => { const day = new Limits({ clock: at(12), local: { activeFrom: "09:00", activeTo: "17:00" } }); assert.equal(day.admit(0), null); const early = new Limits({ clock: at(8, 59), local: { activeFrom: "09:00", activeTo: "17:00" } }); assert.equal(early.admit(0), REFUSALS.OUTSIDE_HOURS); const end = new Limits({ clock: at(17), local: { activeFrom: "09:00", activeTo: "17:00" } }); assert.equal(end.admit(0), REFUSALS.OUTSIDE_HOURS); const wrapIn = new Limits({ clock: at(23, 30), local: { activeFrom: "22:00", activeTo: "06:00" } }); assert.equal(wrapIn.inActiveHours(), true); const wrapEarly = new Limits({ clock: at(3), local: { activeFrom: "22:00", activeTo: "06:00" } }); assert.equal(wrapEarly.inActiveHours(), true); const wrapOut = new Limits({ clock: at(12), local: { activeFrom: "22:00", activeTo: "06:00" } }); assert.equal(wrapOut.inActiveHours(), false); const always = new Limits({ clock: at(12) }); assert.equal(always.inActiveHours(), true); }); test("hello limits advertise the effective concurrency", () => { const limits = new Limits({ local: { maxConcurrent: 1 } }); assert.deepEqual(limits.helloLimits(), { max_concurrent: 1 }); });