"""
ShelfAI test suite.

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

import json


class TestTranscriptParser:
    """Tests for transcript parsing."""

    def test_parse_markdown(self):
        from shelfai.core.transcript import TranscriptParser

        content = "## User\nHow do I deploy?\n\n## Assistant\nRun the deploy script.\n"
        parser = TranscriptParser()
        transcript = parser.parse(content)

        assert transcript.message_count == 2
        assert transcript.messages[0].role == "user"
        assert transcript.messages[1].role == "assistant"
        assert transcript.source_format == "markdown"

    def test_parse_json(self):
        from shelfai.core.transcript import TranscriptParser

        content = json.dumps({
            "messages": [
                {"role": "user", "content": "Hello"},
                {"role": "assistant", "content": "Hi!"},
            ]
        })
        parser = TranscriptParser()
        transcript = parser.parse(content)

        assert transcript.message_count == 2
        assert transcript.source_format == "json"

    def test_parse_simple(self):
        from shelfai.core.transcript import TranscriptParser

        content = "User: How do I deploy?\nAssistant: Run the script.\n"
        parser = TranscriptParser()
        transcript = parser.parse(content)

        assert transcript.message_count == 2
        assert transcript.source_format == "simple"

    def test_parse_raw(self):
        from shelfai.core.transcript import TranscriptParser

        content = "Just some raw text without any role markers."
        parser = TranscriptParser()
        transcript = parser.parse(content)

        assert transcript.message_count == 1
        assert transcript.messages[0].role == "user"
        assert transcript.source_format == "raw"

    def test_parse_empty(self):
        from shelfai.core.transcript import TranscriptParser

        parser = TranscriptParser()
        transcript = parser.parse("")
        assert transcript.message_count == 0

    def test_normalize_roles(self):
        from shelfai.core.transcript import TranscriptParser

        content = "## Human\nHello\n\n## AI\nHi there\n"
        parser = TranscriptParser()
        transcript = parser.parse(content)

        assert transcript.messages[0].role == "user"
        assert transcript.messages[1].role == "assistant"

    def test_transcript_text(self):
        from shelfai.core.transcript import TranscriptParser

        content = "## User\nHello\n\n## Assistant\nHi!\n"
        parser = TranscriptParser()
        transcript = parser.parse(content)

        text = transcript.text
        assert "User" in text
        assert "Assistant" in text
        assert "Hello" in text


# ──────────────────────────────────────────────
# Test: Session Manager
# ──────────────────────────────────────────────


