"""
ShelfAI test suite.

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

import pytest


class TestShelf:
    """Tests for the Shelf class."""

    def test_exists(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        assert shelf.exists()

    def test_not_exists(self, tmp_path):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(tmp_path / "nope"))
        assert not shelf.exists()

    def test_read_file(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        content = shelf.read_file("skills/code_review.md")
        assert "Code Review" in content

    def test_read_file_default(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        content = shelf.read_file("nonexistent.md", default="fallback")
        assert content == "fallback"

    def test_read_file_missing_raises(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        with pytest.raises(FileNotFoundError):
            shelf.read_file("nonexistent.md")

    def test_list_files(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        files = shelf.list_files()
        assert "skills/code_review.md" in files
        assert "knowledge/api_docs.md" in files
        assert "index.md" not in files

    def test_list_files_by_category(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        files = shelf.list_files(categories=["skills"])
        assert all("skills/" in f for f in files)

    def test_status(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        stats = shelf.status()
        assert stats["total_files"] > 0
        assert stats["indexed_count"] > 0
        assert "skills" in stats["categories"]

    def test_export(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        exported = shelf.export()
        assert "ShelfAI Index" in exported
        assert "Code Review" in exported

    def test_export_index_only(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        exported = shelf.export(index_only=True)
        assert "ShelfAI Index" in exported
        # Should not contain full file content
        assert "Review code for quality" not in exported

    def test_append_to_file(self, shelf_dir):
        from shelfai.core.shelf import Shelf

        shelf = Shelf(str(shelf_dir))
        shelf.append_to_file("memory/agent/lessons.md", "\n## 2026-03-14\n- New lesson\n")
        content = shelf.read_file("memory/agent/lessons.md")
        assert "New lesson" in content


# ──────────────────────────────────────────────
# Test: Config
# ──────────────────────────────────────────────


