import { describe, it, expect } from "vitest";
import { auditHtml } from "../lib/audit-heuristics.js";
function pick(result, name) {
    const r = result.heuristics.find((h) => h.name === name);
    if (!r)
        throw new Error(`heuristic ${name} not found`);
    return r;
}
const GOOD_PAGE = `<!doctype html>
<html><head>
  <title>How much does a boiler service cost? — Acme</title>
  <meta name="description" content="A standard boiler service in Brighton costs £85 to £120, takes about one hour, and includes a written safety report.">
  <link rel="canonical" href="https://acme.test/boiler-service-cost">
  <meta property="og:title" content="How much does a boiler service cost?">
  <meta property="og:description" content="A standard boiler service in Brighton costs £85 to £120, takes about one hour, and includes a written safety report.">
  <script type="application/ld+json">{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Cost?","acceptedAnswer":{"@type":"Answer","text":"£85–£120."}}]}</script>
</head><body>
  <h1>How much does a boiler service cost?</h1>
  <p>A standard boiler service in Brighton costs £85 to £120 and takes about one hour.</p>
  <h2>What's included</h2>
  <p>Safety checks, flue gas analysis, pressure adjustment, written report.</p>
  <h2>Frequently asked questions</h2>
  <p>Common questions are answered above.</p>
</body></html>`;
const BAD_PAGE = `<!doctype html>
<html><head><title>Acme</title></head>
<body>
  <div>Some words.</div>
</body></html>`;
describe("auditHtml — golden good page", () => {
    const result = auditHtml(GOOD_PAGE);
    it("scores 100", () => {
        expect(result.score).toBe(100);
    });
    it("passes every heuristic", () => {
        for (const h of result.heuristics) {
            expect(h.status, `${h.name}: ${h.detail}`).toBe("pass");
        }
    });
});
describe("auditHtml — bad page exercises every fail path", () => {
    const result = auditHtml(BAD_PAGE);
    it("scores 0 or near-zero", () => {
        expect(result.score).toBeLessThanOrEqual(10);
    });
    it("fails h1-present", () => {
        expect(pick(result, "h1-present").status).toBe("fail");
    });
    it("fails jsonld-present", () => {
        expect(pick(result, "jsonld-present").status).toBe("fail");
    });
    it("fails meta-description", () => {
        expect(pick(result, "meta-description").status).toBe("fail");
    });
    it("fails canonical-url", () => {
        expect(pick(result, "canonical-url").status).toBe("fail");
    });
    it("fails og-tags with both missing", () => {
        const r = pick(result, "og-tags");
        expect(r.status).toBe("fail");
        expect(r.detail).toContain("og:title");
        expect(r.detail).toContain("og:description");
    });
    it("emits a rewrite suggestion for every failure", () => {
        for (const h of result.heuristics) {
            if (h.status === "fail") {
                expect(h.suggestion, `${h.name} missing suggestion`).toBeTruthy();
            }
        }
    });
});
describe("auditHtml — warn paths", () => {
    it("warns when first <p> after <h1> exceeds 280 chars", () => {
        const long = "X".repeat(300);
        const html = `<html><head>
      <title>t</title>
      <meta name="description" content="${"d".repeat(120)}">
      <link rel="canonical" href="https://a.test">
      <meta property="og:title" content="t">
      <meta property="og:description" content="t">
      <script type="application/ld+json">{"@type":"FAQPage"}</script>
    </head><body>
      <h1>Topic</h1><p>${long}</p>
      <h2>More</h2><p>x</p>
    </body></html>`;
        const result = auditHtml(html);
        expect(pick(result, "structured-answer").status).toBe("warn");
    });
    it("warns when meta-description length is outside 80–160", () => {
        const html = `<html><head>
      <title>t</title>
      <meta name="description" content="too short">
      <link rel="canonical" href="https://a.test">
      <meta property="og:title" content="t">
      <meta property="og:description" content="t">
      <script type="application/ld+json">{"@type":"FAQPage"}</script>
    </head><body>
      <h1>Topic</h1><p>short answer</p>
    </body></html>`;
        expect(pick(auditHtml(html), "meta-description").status).toBe("warn");
    });
    it("warns when there are multiple <h1> elements", () => {
        const html = `<html><body><h1>A</h1><h1>B</h1><p>x</p></body></html>`;
        expect(pick(auditHtml(html), "h1-present").status).toBe("warn");
    });
    it("fails heading-hierarchy on h1→h3 skip", () => {
        const html = `<html><body><h1>A</h1><p>x</p><h3>Skipped</h3></body></html>`;
        const r = pick(auditHtml(html), "heading-hierarchy");
        expect(r.status).toBe("fail");
        expect(r.detail).toContain("h1");
        expect(r.detail).toContain("h3");
    });
    it("warns on unparseable JSON-LD when other blocks parse", () => {
        const html = `<html><head>
      <script type="application/ld+json">{"@type":"FAQPage"}</script>
      <script type="application/ld+json">not json</script>
    </head><body><h1>T</h1><p>a</p></body></html>`;
        expect(pick(auditHtml(html), "jsonld-present").status).toBe("warn");
    });
});
//# sourceMappingURL=audit-heuristics.test.js.map