"""
ShelfAI test suite.

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



class TestSchemaValidation:
    """Tests for extraction schema validation."""

    def test_valid_extraction(self):
        from shelfai.agents.session import _validate_extraction

        data = {
            "user_preferences": [
                {"content": "Likes Python", "confidence": "high", "evidence": "said so"}
            ],
            "agent_lessons": [],
            "task_patterns": [],
            "knowledge_corrections": [],
            "new_context": [],
            "abstract_updates": [],
        }
        result = _validate_extraction(data)
        assert len(result["user_preferences"]) == 1

    def test_missing_required_keys_dropped(self):
        from shelfai.agents.session import _validate_extraction

        data = {
            "user_preferences": [
                {"text": "bad schema"}  # missing 'content' and 'confidence'
            ],
            "agent_lessons": [],
            "task_patterns": [],
            "knowledge_corrections": [],
            "new_context": [],
            "abstract_updates": [],
        }
        result = _validate_extraction(data)
        assert len(result["user_preferences"]) == 0  # dropped

    def test_non_list_category_handled(self):
        from shelfai.agents.session import _validate_extraction

        data = {
            "user_preferences": "not a list",
            "agent_lessons": [],
            "task_patterns": [],
            "knowledge_corrections": [],
            "new_context": [],
            "abstract_updates": [],
        }
        result = _validate_extraction(data)
        assert result["user_preferences"] == []

    def test_non_dict_items_dropped(self):
        from shelfai.agents.session import _validate_extraction

        data = {
            "user_preferences": ["just a string", 42, None],
            "agent_lessons": [],
            "task_patterns": [],
            "knowledge_corrections": [],
            "new_context": [],
            "abstract_updates": [],
        }
        result = _validate_extraction(data)
        assert len(result["user_preferences"]) == 0


# ──────────────────────────────────────────────
# Test: Safety — Backups
# ──────────────────────────────────────────────


