"""
ShelfAI test suite.

Tests core functionality: shelf operations, index parsing,
transcript parsing, session management, and CLI commands.
"""

import pytest


class TestIndex:
    """Tests for the Index class."""

    def test_parse_index(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        assert index.count == 6
        assert "Skills" in index.categories
        assert "Knowledge" in index.categories

    def test_get_entry(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        entry = index.get("skills/code_review.md")
        assert entry is not None
        assert "review code" in entry.abstract.lower()

    def test_get_missing_entry(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        assert index.get("nonexistent.md") is None

    def test_search(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        results = index.search("code review python")
        assert len(results) > 0
        assert results[0].file_path == "skills/code_review.md"

    def test_search_no_results(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        results = index.search("xyznonexistent")
        assert len(results) == 0

    def test_add_entry(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        index.add("skills/debugging.md", "Use when debugging errors.", "Skills")
        assert index.count == 7
        entry = index.get("skills/debugging.md")
        assert entry is not None
        assert entry.abstract == "Use when debugging errors."

    def test_update_entry(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        index.update("skills/code_review.md", "Updated abstract.")
        entry = index.get("skills/code_review.md")
        assert entry.abstract == "Updated abstract."

    def test_update_missing_raises(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        with pytest.raises(KeyError):
            index.update("nonexistent.md", "abstract")

    def test_remove_entry(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        index.remove("skills/code_review.md")
        assert index.count == 5
        assert index.get("skills/code_review.md") is None

    def test_remove_missing_raises(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        with pytest.raises(KeyError):
            index.remove("nonexistent.md")

    def test_unindexed_files(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        unindexed = index.unindexed_files(
            ["skills/code_review.md", "skills/new_skill.md"]
        )
        assert "skills/new_skill.md" in unindexed
        assert "skills/code_review.md" not in unindexed

    def test_stale_entries(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        stale = index.stale_entries(["skills/code_review.md"])
        # All other entries should be stale
        stale_paths = [e.file_path for e in stale]
        assert "skills/data_analysis.md" in stale_paths

    def test_roundtrip(self, shelf_dir):
        from shelfai.core.index import Index

        index = Index(shelf_dir / "index.md")
        original_count = index.count

        # Write and reload
        markdown = index.to_markdown()
        (shelf_dir / "index.md").write_text(markdown)
        index2 = Index(shelf_dir / "index.md")

        assert index2.count == original_count


# ──────────────────────────────────────────────
# Test: Shelf
# ──────────────────────────────────────────────


