Files

44 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
defineValidationBudget,
responseValidationBudget,
} from "../../../src/platform/validation/validation-budget.ts";
const BUDGET = {
maxBytes: 1_024,
maxItems: 1_000,
maxDepth: 20,
maxStringLength: 32_768,
};
describe("defineValidationBudget", () => {
it("rejects unbounded validation configuration", () => {
expect(() =>
defineValidationBudget({ ...BUDGET, maxBytes: Infinity }),
).toThrow(/maxBytes/);
});
it.each(["maxBytes", "maxItems", "maxDepth", "maxStringLength"] as const)(
"rejects a non-positive or fractional %s",
(key) => {
expect(() => defineValidationBudget({ ...BUDGET, [key]: 0 })).toThrow(key);
expect(() => defineValidationBudget({ ...BUDGET, [key]: 1.5 })).toThrow(key);
expect(() => defineValidationBudget({ ...BUDGET, [key]: Number.NaN })).toThrow(key);
},
);
it("returns an immutable copy", () => {
const budget = defineValidationBudget(BUDGET);
expect(budget).toEqual(BUDGET);
expect(Object.isFrozen(budget)).toBe(true);
});
it("derives a finite response structure budget from the byte ceiling", () => {
const budget = responseValidationBudget(262_144);
expect(budget.maxBytes).toBe(262_144);
expect(Number.isSafeInteger(budget.maxItems)).toBe(true);
expect(Number.isSafeInteger(budget.maxDepth)).toBe(true);
});
});