#!/usr/bin/env python3 """Test script for hybrid memory system.""" import os import sys import asyncio from pathlib import Path # Add the tools package to Python path sys.path.insert(0, str(Path(__file__).parent / "hanzo_tools")) from hanzo_tools.core import PermissionManager from hanzo_tools.database.memory_tool import MemoryTool from hanzo_tools.database.memory_manager import MemoryManager async def test_memory_system(): """Test the hybrid memory system.""" print("๐Ÿง  Testing Hybrid Memory System") print("=" * 50) # Create permission manager (allow all for testing) permission_manager = PermissionManager(allowed_paths=["/"]) # Create memory manager memory_manager = MemoryManager(permission_manager) # Create memory tool memory_tool = MemoryTool(permission_manager) # Test project path test_project = Path("/tmp/test_hanzo_project") test_project.mkdir(exist_ok=True) os.chdir(test_project) print(f"๐Ÿ“ Test project: {test_project}") # Mock MCP context (simplified) class MockContext: pass ctx = MockContext() # Test 1: Initialize memory structure print("\n1. ๐Ÿ“‹ Initialize memory structure...") from hanzo_tools.database.init_memory import init_global_memory, init_project_memory try: init_global_memory() init_project_memory(str(test_project)) print("โœ“ Memory structure initialized") except Exception as e: print(f"โœ— Initialization failed: {e}") # Test 2: Write memory files print("\n2. โœ๏ธ Write memory files...") try: # Global memory result = await memory_tool.call( ctx, action="write", file_path="test_rules.md", content="# Test Rules\n\nThis is a test rule file.", scope="global", category="test", ) print(f"โœ“ Global write: {result}") # Project memory result = await memory_tool.call( ctx, action="write", file_path="test_architecture.md", content="# Test Architecture\n\nThis describes the test architecture.", scope="project", category="architecture", ) print(f"โœ“ Project write: {result}") except Exception as e: print(f"โœ— Write failed: {e}") # Test 3: Read memory files print("\n3. ๐Ÿ“– Read memory files...") try: result = await memory_tool.call( ctx, action="read", file_path="test_rules.md", scope="global" ) print("โœ“ Global read successful") print(f"Content preview: {result[:100]}...") result = await memory_tool.call( ctx, action="read", file_path="test_architecture.md", scope="project" ) print("โœ“ Project read successful") print(f"Content preview: {result[:100]}...") except Exception as e: print(f"โœ— Read failed: {e}") # Test 4: Append to memory files print("\n4. โž• Append to memory files...") try: result = await memory_tool.call( ctx, action="append", file_path="sessions/test_session.md", content="This is a test session entry with important insights.", scope="project", category="session", ) print(f"โœ“ Append: {result}") except Exception as e: print(f"โœ— Append failed: {e}") # Test 5: Create structured memories print("\n5. ๐Ÿ—ƒ๏ธ Create structured memories...") try: result = await memory_tool.call( ctx, action="create", content="This is a test structured memory with high importance", category="test", importance=8, scope="project", ) print(f"โœ“ Create structured: {result}") except Exception as e: print(f"โœ— Create failed: {e}") # Test 6: Search memories print("\n6. ๐Ÿ” Search memories...") try: result = await memory_tool.call( ctx, action="search", content="test", scope="both", limit=5 ) print("โœ“ Search successful") print(f"Results preview: {result[:300]}...") except Exception as e: print(f"โœ— Search failed: {e}") # Test 7: List memory files print("\n7. ๐Ÿ“‹ List memory files...") try: result = await memory_tool.call(ctx, action="list", scope="both") print("โœ“ List successful") print(f"List preview: {result[:200]}...") except Exception as e: print(f"โœ— List failed: {e}") # Test 8: Get statistics print("\n8. ๐Ÿ“Š Get memory statistics...") try: result = await memory_tool.call(ctx, action="stats", scope="both") print("โœ“ Stats successful") print(f"Stats preview: {result[:300]}...") except Exception as e: print(f"โœ— Stats failed: {e}") # Test 9: Test database features print("\n9. ๐Ÿ—„๏ธ Test database features...") try: # Test FTS5 search results = memory_manager.search_memories( "test architecture", "both", str(test_project) ) print(f"โœ“ FTS5 search found {len(results)} results") # Get stats stats = memory_manager.get_memory_stats("both", str(test_project)) print( f"โœ“ Database stats: {stats['markdown_files']} markdown files, {stats['structured_memories']} memories" ) # Test sqlite-vec availability try: import sqlite3 conn = sqlite3.connect(":memory:") conn.enable_load_extension(True) conn.load_extension("vec0") conn.close() print("โœ“ sqlite-vec extension available") except Exception: print("โš ๏ธ sqlite-vec extension not available (optional)") except Exception as e: print(f"โœ— Database test failed: {e}") print("\n" + "=" * 50) print("๐ŸŽ‰ Memory system test completed!") # Cleanup import shutil try: shutil.rmtree(test_project) print(f"๐Ÿงน Cleaned up test project: {test_project}") except Exception: pass if __name__ == "__main__": asyncio.run(test_memory_system())