"use strict"; // adpanel:env-int-clamp — unit pins for the ONE shared env int-clamp // resolver. Two zero/negative policies exist because the campaign's // variables documented them that way before the consolidation (the four // private resolver copies this helper replaced had already diverged on // exactly this point): // floor (the helper's default): any finite parse clamps into [min, max] — // a "0" can never disable the control (timeouts, budgets, counters). // "default": a non-positive parse means "not configured" and yields the // fallback (BCRYPT_ROUNDS: 0 or a negative is operator nonsense and the // old inline parse fell back to the documented default 12, never to the // band floor). const test = require("node:test"); const assert = require("node:assert"); const { resolveClampedInt } = require("./clamped-int"); // Shapes mirroring the two real consumers: the millisecond/counter vars // (floor policy) and BCRYPT_ROUNDS (default policy). const FLOOR = { fallback: 30, min: 5, max: 24 }; const DEF = { fallback: 12, min: 10, max: 15, zeroPolicy: "default" }; test("floor policy (the helper default): unset, empty, undefined and garbage all yield the fallback", () => { assert.strictEqual(resolveClampedInt({}, "X", FLOOR), 30); assert.strictEqual(resolveClampedInt({ X: "" }, "X", FLOOR), 30); assert.strictEqual(resolveClampedInt({ X: undefined }, "X", FLOOR), 30); assert.strictEqual(resolveClampedInt({ X: "garbage" }, "X", FLOOR), 30); }); test("floor policy: zero and negatives clamp to the floor — a zero can never disable the control", () => { assert.strictEqual(resolveClampedInt({ X: "0" }, "X", FLOOR), 5, "0 clamps to the floor"); assert.strictEqual(resolveClampedInt({ X: "-5" }, "X", FLOOR), 5, "negatives clamp to the floor"); assert.strictEqual(resolveClampedInt({ X: "1" }, "X", FLOOR), 5, "tiny in-parse values clamp up too"); assert.strictEqual(resolveClampedInt({ X: "999" }, "X", FLOOR), 24, "above-ceiling values clamp down"); }); test("floor policy: in-range values pass through, out-of-band values clamp", () => { assert.strictEqual(resolveClampedInt({ X: "7" }, "X", FLOOR), 7); assert.strictEqual(resolveClampedInt({ X: "24" }, "X", FLOOR), 24, "the ceiling passes through"); assert.strictEqual(resolveClampedInt({ X: "4" }, "X", FLOOR), 5, "below-floor clamps up"); assert.strictEqual(resolveClampedInt({ X: "100" }, "X", FLOOR), 24, "above-ceiling clamps down"); }); test("default policy: zero and negatives mean 'not configured' and yield the fallback", () => { assert.strictEqual(resolveClampedInt({ X: "0" }, "X", DEF), 12, "0 must NOT clamp to the band floor"); assert.strictEqual(resolveClampedInt({ X: "-5" }, "X", DEF), 12, "negatives must NOT clamp to the band floor"); }); test("default policy: positive values clamp exactly like the floor policy", () => { assert.strictEqual(resolveClampedInt({ X: "8" }, "X", DEF), 10, "below-floor clamps up"); assert.strictEqual(resolveClampedInt({ X: "12" }, "X", DEF), 12); assert.strictEqual(resolveClampedInt({ X: "20" }, "X", DEF), 15, "above-ceiling clamps down"); assert.strictEqual(resolveClampedInt({ X: "31" }, "X", DEF), 15); }); test("both policies keep the historical lenient parseInt semantics of the resolvers they replaced", () => { assert.strictEqual(resolveClampedInt({ X: "12abc" }, "X", DEF), 12, "prefix parse ('12abc' → 12)"); assert.strictEqual(resolveClampedInt({ X: " 12" }, "X", DEF), 12, "leading whitespace tolerated"); assert.strictEqual(resolveClampedInt({ X: "12.7" }, "X", DEF), 12, "decimal truncation"); assert.strictEqual(resolveClampedInt({ X: "0x10" }, "X", DEF), 12, "radix-10 parse of '0x10' is 0 → 'default' fallback"); assert.strictEqual(resolveClampedInt({ X: "0x10" }, "X", FLOOR), 5, "radix-10 parse of '0x10' is 0 → floor"); }); test("an unknown zeroPolicy behaves as the documented default (floor)", () => { assert.strictEqual(resolveClampedInt({ X: "0" }, "X", { fallback: 30, min: 5, max: 24, zeroPolicy: "typo" }), 5); });