"""
Tests for LangChain integration.

These tests mock LangChain to avoid requiring it as a test dependency.
"""

import sys
import tempfile
from pathlib import Path
from unittest.mock import MagicMock

import pytest


# Mock langchain_core before importing our module
@pytest.fixture(autouse=True)
def mock_langchain():
    """Mock langchain_core module."""
    mock_memory = MagicMock()
    mock_memory.BaseMemory = object
    
    mock_messages = MagicMock()
    mock_messages.BaseMessage = object
    mock_messages.HumanMessage = lambda content: {"role": "human", "content": content}
    mock_messages.AIMessage = lambda content: {"role": "ai", "content": content}
    
    sys.modules["langchain_core"] = MagicMock()
    sys.modules["langchain_core.memory"] = mock_memory
    sys.modules["langchain_core.messages"] = mock_messages
    
    yield
    
    # Cleanup
    del sys.modules["langchain_core"]
    del sys.modules["langchain_core.memory"]
    del sys.modules["langchain_core.messages"]


class TestEngramMemoryBasic:
    """Basic tests that don't require full LangChain."""
    
    def test_memory_tree_operations(self):
        """Test the underlying MemoryTree works correctly."""
        from engram import MemoryTree
        
        with tempfile.TemporaryDirectory() as tmpdir:
            path = Path(tmpdir) / "test.engram"
            
            tree = MemoryTree()
            tree.add("Human message", tags=["conversation", "human"])
            tree.add("AI response", tags=["conversation", "ai"])
            tree.add("Some knowledge", tags=["knowledge", "facts"])
            tree.save(path)
            
            # Reload
            tree2 = MemoryTree.from_file(path)
            
            conv = tree2.find_by_tag("conversation")
            assert len(conv) == 2
            
            knowledge = tree2.find_by_tag("knowledge")
            assert len(knowledge) == 1
            
            humans = tree2.find_by_tag("human")
            assert len(humans) == 1


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
