import { describe, expect, it } from "vitest"; import { sourceLocationFor } from "./sourceLocations"; import { lintHyperframeHtml } from "./hyperframeLinter"; import type { HyperframeLintFinding } from "./types"; const finding = (fields: Partial): HyperframeLintFinding => ({ code: "probe", severity: "error", message: "bad", ...fields, }); describe("original source locations", () => { it("locates a tag after comments and a template wrapper", () => { const html = '\n'; expect(sourceLocationFor(html, finding({ elementId: "bad" }))).toEqual({ line: 5, column: 3 }); }); it("locates normalized CSS snippets across CRLF", () => { const html = ""; expect(sourceLocationFor(html, finding({ snippet: ".bad { position: fixed; }" }))).toEqual({ line: 2, column: 3, }); }); it("locates a script snippet", () => { expect( sourceLocationFor( '', finding({ snippet: 'gsap.to("#x", { x: 1 });' }), ), ).toEqual({ line: 2, column: 3 }); }); it("does not invent a location for repeated snippets or global findings", () => { expect( sourceLocationFor("
x
\n
x
", finding({ snippet: "
x
" })), ).toEqual({}); expect(sourceLocationFor("", finding({}))).toEqual({}); }); it("does not use a commented-out id", () => { expect( sourceLocationFor('\n
', finding({ elementId: "x" })), ).toEqual({ line: 2, column: 1 }); }); it("attaches locations to real lint findings", async () => { const html = '
\n
x
\n
'; const r = await lintHyperframeHtml(html, { filePath: "index.html" }); expect(r.findings.find((f) => f.code === "timeline_element_missing_timing")).toMatchObject({ file: "index.html", line: 2, column: 3, }); }); }); it("does not point synthesized code snippets at commented examples", () => { expect( sourceLocationFor( "\n", finding({ snippet: "p.getTotalLength()" }), ), ).toEqual({}); }); describe("rule-owned source positions", () => { const shell = (content: string) => `
\n${content}\n
`; const at = (html: string, token: string) => { const offset = html.indexOf(token); const before = html.slice(0, offset); return { line: before.split(/\r\n|\r|\n/).length, column: offset - Math.max(before.lastIndexOf("\n"), before.lastIndexOf("\r")), }; }; it.each(["\n", "\r\n"])( "locates the offending JS expression through comments and templates (%j)", async (nl) => { const html = ( `\n" ).replaceAll("\n", nl); const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "non_deterministic_code")).toMatchObject( at(html, "Math.random();"), ); }, ); it("locates an Acorn syntax error rather than the script prefix", async () => { const html = shell(""); const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "invalid_inline_script_syntax")).toMatchObject( at(html, ";\n"), ); }); it("maps CSS parser positions to original HTML", async () => { const html = "\n"; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "css_parse_error")).toMatchObject( at(html, "color red"), ); }); it("keeps an external stylesheet's identity", async () => { const result = await lintHyperframeHtml(shell(""), { filePath: "scenes/a.html", externalStyles: [{ href: "theme.css", content: ".x {\n color red;\n}" }], }); expect(result.findings.find((f) => f.code === "css_parse_error")).toMatchObject({ file: "theme.css", line: 2, column: 2, }); }); it("locates a unique font declaration and GSAP timeline call", async () => { const html = shell( '\n', ); const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "font_family_without_font_face")).toMatchObject( at(html, "font-family"), ); expect(result.findings.find((f) => f.code === "gsap_timeline_not_registered")).toMatchObject( at(html, "gsap.timeline"), ); }); it("does not arbitrarily locate a multi-declaration font finding", async () => { const result = await lintHyperframeHtml( shell(''), ); expect( result.findings.find((f) => f.code === "font_family_without_font_face")?.line, ).toBeUndefined(); }); }); it("locates repeated GSAP config errors at the triggering Acorn call", async () => { const html = ''; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "gsap_infinite_repeat")).toMatchObject({ line: 4, column: 1, }); }); it("uses Acorn without executing source and preserves classic/module handling", async () => { const classic = await lintHyperframeHtml( "", ); expect(classic.findings.some((f) => f.code === "invalid_inline_script_syntax")).toBe(false); expect("__lintMustNotRun" in globalThis).toBe(false); const module = await lintHyperframeHtml( '', ); expect(module.findings.some((f) => f.code === "invalid_inline_script_syntax")).toBe(false); }); it("maps fixpoint comment deletion and repeated script text without guessing", async () => { const html = "<!-- removed -->\n"; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "non_deterministic_code")).toMatchObject({ line: 4, column: 25, }); }); it("counts same-line Unicode columns as UTF-16 units", async () => { const html = ''; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "non_deterministic_code")).toMatchObject({ line: 1, column: html.indexOf("Math.random") + 1, }); }); it("leaves the mapped EOF error at the end of its own script", async () => { const html = "\n\n"; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "invalid_inline_script_syntax")).toMatchObject({ line: 2, column: 22, }); }); it("maps PostCSS offsets when bare CR and LF are mixed", async () => { const html = ""; const result = await lintHyperframeHtml(html); expect(result.findings.find((f) => f.code === "css_parse_error")).toMatchObject({ line: 4, column: 2, }); }); it("uses classic-script Acorn semantics for new.target and hashbang", async () => { const target = await lintHyperframeHtml(""); expect(target.findings.some((f) => f.code === "invalid_inline_script_syntax")).toBe(true); const hashbang = await lintHyperframeHtml(""); expect(hashbang.findings.some((f) => f.code === "invalid_inline_script_syntax")).toBe(false); }); it("locates a snippet at the end of a large source in linear time", () => { const filler = "
\n

a b

\n
\n".repeat(12_000); const html = `${filler}`; const start = performance.now(); const location = sourceLocationFor(html, finding({ snippet: ".late { position: fixed; }" })); expect(location).toEqual({ line: 36_002, column: 3 }); expect(performance.now() - start).toBeLessThan(1_000); });