import { describe, expect, test } from "bun:test";
import { CHECKLIST } from "./checklist-data";
import { listSpecPages, parseFrontmatter } from "./spec-content";

// Mirror of the script's derivation, tested in isolation (no network).
function mdUrl(url: string): string {
  return `${url.replace(/\/+$/, "")}.md`;
}

describe("md URL derivation", () => {
  test("strips trailing slash and appends .md", () => {
    expect(mdUrl("https://specification.website/spec/seo/meta-robots/")).toBe(
      "https://specification.website/spec/seo/meta-robots.md",
    );
  });

  test("handles a URL without a trailing slash", () => {
    expect(mdUrl("https://specification.website/spec/seo/meta-robots")).toBe(
      "https://specification.website/spec/seo/meta-robots.md",
    );
  });

  test("collapses multiple trailing slashes", () => {
    expect(mdUrl("https://x.test/a///")).toBe("https://x.test/a.md");
  });
});

const FIXTURE = `---
title: "Meta robots and X-Robots-Tag"
slug: meta-robots
category: seo
status: required
url: https://specification.website/spec/seo/meta-robots/
summary: "An explicit indexing policy on every page."
sources:
  - title: "HTML Living Standard — robots"
    url: "https://html.spec.whatwg.org/multipage/semantics.html#meta-robots"
    publisher: "WHATWG"
  - title: "Google Search Central"
    url: "https://developers.google.com/search"
    publisher: "Google Search Central"
relatedSlugs: [robots-txt, canonical-url, redirects]
---
# Meta robots and X-Robots-Tag

> The summary blockquote.

## What it is

Body content with a [link](/spec/seo/robots-txt/).

| Directive | Effect |
|---|---|
| index | allow |
`;

describe("parseFrontmatter", () => {
  const { frontmatter, body } = parseFrontmatter(FIXTURE);

  test("parses scalar fields", () => {
    expect(frontmatter.title).toBe("Meta robots and X-Robots-Tag");
    expect(frontmatter.slug).toBe("meta-robots");
    expect(frontmatter.category).toBe("seo");
    expect(frontmatter.status).toBe("required");
    expect(frontmatter.url).toBe(
      "https://specification.website/spec/seo/meta-robots/",
    );
    expect(frontmatter.summary).toBe(
      "An explicit indexing policy on every page.",
    );
  });

  test("parses the sources block", () => {
    expect(frontmatter.sources).toHaveLength(2);
    expect(frontmatter.sources[0]).toEqual({
      title: "HTML Living Standard — robots",
      url: "https://html.spec.whatwg.org/multipage/semantics.html#meta-robots",
      publisher: "WHATWG",
    });
    expect(frontmatter.sources[1].publisher).toBe("Google Search Central");
  });

  test("parses the inline relatedSlugs list", () => {
    expect(frontmatter.relatedSlugs).toEqual([
      "robots-txt",
      "canonical-url",
      "redirects",
    ]);
  });

  test("strips the frontmatter from the body", () => {
    expect(body.startsWith("# Meta robots")).toBe(true);
    expect(body).not.toContain("relatedSlugs");
    expect(body).toContain("| Directive | Effect |");
  });

  test("handles a file with no frontmatter", () => {
    const { frontmatter: fm, body: b } = parseFrontmatter("# Just a body\n");
    expect(fm.title).toBe("");
    expect(fm.sources).toEqual([]);
    expect(b).toBe("# Just a body");
  });
});

describe("listSpecPages", () => {
  const pages = listSpecPages();

  test("yields one entry per checklist item", () => {
    const total = CHECKLIST.reduce((n, c) => n + c.items.length, 0);
    expect(pages).toHaveLength(total);
  });

  test("derives category + slug from each item id", () => {
    const seo = pages.find((p) => p.slug === "meta-robots");
    expect(seo).toEqual({ category: "seo", slug: "meta-robots" });
  });

  test("every slug is filesystem-safe", () => {
    for (const p of pages) {
      expect(p.slug).toMatch(/^[a-z0-9-]+$/);
      expect(p.category).toMatch(/^[a-z0-9-]+$/);
    }
  });
});
