"""
ShelfAI test suite.

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

import pytest


class TestPathSafety:
    """Tests for path traversal protection."""

    def test_valid_path(self, shelf_dir):
        from shelfai.core.safety import validate_path

        result = validate_path(shelf_dir, "skills/code_review.md")
        assert result == (shelf_dir / "skills" / "code_review.md").resolve()

    def test_traversal_blocked(self, shelf_dir):
        from shelfai.core.safety import validate_path

        with pytest.raises(ValueError, match="Path traversal blocked"):
            validate_path(shelf_dir, "../../etc/passwd")

    def test_traversal_with_dotdot(self, shelf_dir):
        from shelfai.core.safety import validate_path

        with pytest.raises(ValueError, match="Path traversal blocked"):
            validate_path(shelf_dir, "skills/../../.ssh/authorized_keys")

    def test_hidden_file_blocked(self, shelf_dir):
        from shelfai.core.safety import validate_path

        with pytest.raises(ValueError, match="Hidden path component"):
            validate_path(shelf_dir, ".env")

    def test_hidden_dir_blocked(self, shelf_dir):
        from shelfai.core.safety import validate_path

        with pytest.raises(ValueError, match="Hidden path component"):
            validate_path(shelf_dir, ".git/config")

    def test_shelfai_dir_allowed(self, shelf_dir):
        from shelfai.core.safety import validate_path

        # .shelfai is the exception — it's our own metadata dir
        result = validate_path(shelf_dir, ".shelfai/backups/test.json")
        assert ".shelfai" in str(result)

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

        shelf = Shelf(str(shelf_dir))
        with pytest.raises(ValueError, match="Path traversal blocked"):
            shelf.write_file("../../evil.md", "pwned")


# ──────────────────────────────────────────────
# Test: Safety — JSON Parsing
# ──────────────────────────────────────────────


