from __future__ import annotations

import re
import unittest
from pathlib import Path


SKILL_ROOT = Path(__file__).resolve().parents[1]


class SkillContractTest(unittest.TestCase):
    def test_skill_is_mandatory_for_small_and_implicit_resume_requests(self) -> None:
        text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8").lower()
        self.assertIn("mandatory entrypoint for every resume or cv request", text)
        self.assertIn("quick wording questions", text)
        self.assertIn("even when the user does not name the", text)

        metadata = (SKILL_ROOT / "agents" / "openai.yaml").read_text(
            encoding="utf-8"
        )
        self.assertIn("allow_implicit_invocation: true", metadata)

    def test_router_stays_small_and_interactive(self) -> None:
        text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
        self.assertLessEqual(len(text.splitlines()), 120)
        self.assertIn("Default: interactive tailoring", text)
        self.assertIn("Use one-shot mode only", text)
        self.assertLess(text.lower().find("strategy checkpoint"), text.lower().find("assembly checkpoint"))

    def test_all_direct_references_exist(self) -> None:
        text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
        references = set(re.findall(r"`(references/[^`]+\.md)`", text))
        self.assertTrue(references)
        for reference in references:
            self.assertTrue((SKILL_ROOT / reference).is_file(), reference)

    def test_persistence_reference_matches_cli(self) -> None:
        text = (SKILL_ROOT / "references" / "persistence-and-state.md").read_text(
            encoding="utf-8"
        )
        for command in [
            "init",
            "approve",
            "mark-draft",
            "content-approved",
            "status",
            "finalize",
        ]:
            self.assertIn(command, text)


if __name__ == "__main__":
    unittest.main()
