"""Tests for the bundled SKILL.md frontmatter constraints.

Feature: SKILL.md Claude skill format

  Scenario: frontmatter is present and well-formed
  Scenario: name satisfies Claude skill naming rules (kebab-case, max 64 chars)
  Scenario: description satisfies Claude skill constraints (max 1024 chars, no angle brackets)
  Scenario: skill content is accessible via importlib.resources
"""

from __future__ import annotations

import re

from importlib.resources import files as pkg_files


def _skill_content() -> str:
    return (pkg_files("qrm.llm") / "SKILL.md").read_text(encoding="utf-8")


def _parse_frontmatter(content: str) -> dict[str, str]:
    """Return the raw string values of 'name' and 'description' from YAML frontmatter."""
    match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
    assert match, "SKILL.md must start with a --- ... --- frontmatter block"
    fm = match.group(1)

    name_m = re.search(r"^name:\s*(.+)$", fm, re.MULTILINE)
    assert name_m, "frontmatter must contain a 'name' field"

    # description may be a plain scalar or a block scalar (>- / >)
    desc_m = re.search(
        r"^description:\s*(.+?)(?=\n\S|\Z)",
        fm,
        re.DOTALL | re.MULTILINE,
    )
    assert desc_m, "frontmatter must contain a 'description' field"

    raw_desc = desc_m.group(1)
    # Strip YAML block scalar indicator and collapse indented continuation lines
    raw_desc = re.sub(r"^>-?\s*\n", "", raw_desc)
    description = " ".join(line.strip() for line in raw_desc.splitlines()).strip()

    return {"name": name_m.group(1).strip(), "description": description}


def test_skill_md_is_readable():
    content = _skill_content()
    assert len(content) > 0


def test_frontmatter_present():
    content = _skill_content()
    assert content.startswith("---"), "SKILL.md must begin with YAML frontmatter"


def test_name_kebab_case():
    fm = _parse_frontmatter(_skill_content())
    assert re.match(
        r"^[a-z0-9]+(-[a-z0-9]+)*$", fm["name"]
    ), f"name '{fm['name']}' must be kebab-case ([a-z0-9] groups separated by single hyphens)"


def test_name_no_leading_or_trailing_hyphens():
    fm = _parse_frontmatter(_skill_content())
    assert not fm["name"].startswith("-"), "name must not start with a hyphen"
    assert not fm["name"].endswith("-"), "name must not end with a hyphen"


def test_name_no_consecutive_hyphens():
    fm = _parse_frontmatter(_skill_content())
    assert "--" not in fm["name"], "name must not contain consecutive hyphens"


def test_name_max_length():
    fm = _parse_frontmatter(_skill_content())
    assert len(fm["name"]) <= 64, f"name is {len(fm['name'])} chars; max is 64"


def test_description_max_length():
    fm = _parse_frontmatter(_skill_content())
    assert (
        len(fm["description"]) <= 1024
    ), f"description is {len(fm['description'])} chars; max is 1024"


def test_description_no_angle_brackets():
    fm = _parse_frontmatter(_skill_content())
    assert "<" not in fm["description"], "description must not contain '<'"
    assert ">" not in fm["description"], "description must not contain '>'"
