import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
SKILLS_MD = ROOT / "SKILLS.md"
SOURCES_LIST = ROOT / "codex" / "skills" / ".sources" / "superpowers.list"


def read_sources_list() -> list[str]:
    return [
        line.strip()
        for line in SOURCES_LIST.read_text(encoding="utf-8").splitlines()
        if line.strip() and not line.strip().startswith("#")
    ]


def read_skills_md_list() -> list[str]:
    lines = SKILLS_MD.read_text(encoding="utf-8").splitlines()
    start = "<!-- superpowers:skills:start -->"
    end = "<!-- superpowers:skills:end -->"
    try:
        start_idx = lines.index(start) + 1
        end_idx = lines.index(end)
    except ValueError as exc:
        raise AssertionError("superpowers markers missing in SKILLS.md") from exc

    items = []
    for line in lines[start_idx:end_idx]:
        stripped = line.strip()
        if not stripped.startswith("-"):
            continue
        items.append(stripped.lstrip("- ").strip())
    return items


class SuperpowersListSyncTests(unittest.TestCase):
    def test_superpowers_list_matches_skills_md(self):
        self.assertEqual(read_sources_list(), read_skills_md_list())


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