import { afterEach, describe, expect, it } from "vitest"; import { runConstraintAudit } from "./constraintAudit"; const audit = new Function("scope", `return (${runConstraintAudit.toString()})(scope)`) as ( scope?: string, ) => ReturnType; afterEach(() => { document.body.innerHTML = ""; }); describe("constraint audit scope and painted visibility", () => { it("audits the requested surface while keeping the document-wide default", () => { document.body.innerHTML = `
`; expect(audit("#story").violations.map((entry) => entry.id)).toEqual(["inside"]); expect(audit().violations.map((entry) => entry.id)).toEqual(["outside", "inside"]); }); it("rejects a missing requested surface instead of returning a clean report", () => { expect(() => audit("#missing")).toThrow(/scope/i); }); it.each(["display:none", "visibility:hidden", "opacity:0"])( "ignores unpainted descendants under %s and reports them when shown", (style) => { document.body.innerHTML = `
`; expect(audit("#story").violations).toEqual([]); document.querySelector("#hidden")!.removeAttribute("style"); expect(audit("#story").violations.map((entry) => entry.id)).toEqual(["bad"]); }, ); it("keeps painted children that explicitly override inherited visibility", () => { document.body.innerHTML = `
`; expect(audit("#story").violations.map((entry) => entry.id)).toEqual(["shown"]); }); it("retains distinct stable paths for otherwise identical failing controls", () => { document.body.innerHTML = `
`; const first = audit("#story").violations; expect(first).toHaveLength(2); expect(first[0].key).toBeTruthy(); expect(first[1].key).not.toBe(first[0].key); expect(audit("#story").violations.map((entry) => entry.key)).toEqual( first.map((entry) => entry.key), ); }); });