import { describe, expect, test } from "bun:test"; import { parseScanLimit } from "../scan"; describe("parseScanLimit", () => { test("accepts positive integers and nullish/empty as no-limit", () => { expect(parseScanLimit(5)).toEqual({ ok: true, value: 5 }); expect(parseScanLimit(1)).toEqual({ ok: true, value: 1 }); expect(parseScanLimit(10_000)).toEqual({ ok: true, value: 10_000 }); expect(parseScanLimit(null)).toEqual({ ok: true, value: null }); expect(parseScanLimit(undefined)).toEqual({ ok: true, value: null }); expect(parseScanLimit("")).toEqual({ ok: true, value: null }); }); test("coerces numeric strings (env var path) but rejects garbage", () => { expect(parseScanLimit("7")).toEqual({ ok: true, value: 7 }); expect(parseScanLimit("abc")).toEqual({ ok: false }); expect(parseScanLimit("12abc")).toEqual({ ok: false }); }); test("rejects the footguns that would silently disable the cap", () => { // NaN: processed >= NaN never trips → cap never fires. expect(parseScanLimit(Number.NaN)).toEqual({ ok: false }); // Negative: off-by-one bugs in Math.min(limit, total). expect(parseScanLimit(-1)).toEqual({ ok: false }); expect(parseScanLimit(0)).toEqual({ ok: false }); // Float: Math.min is fine but percentage math breaks on non-integers. expect(parseScanLimit(1.5)).toEqual({ ok: false }); // Infinity is technically a number but has no business as a cap. expect(parseScanLimit(Number.POSITIVE_INFINITY)).toEqual({ ok: false }); }); });