"""Tests for timmy.memory_system — Memory system context injection.""" from unittest.mock import patch import pytest from timmy.memory_system import MemorySystem, reset_memory_system @pytest.fixture(autouse=True) def reset_singleton(): """Reset the memory system singleton before each test.""" reset_memory_system() yield reset_memory_system() class TestGetSystemContext: """Tests for get_system_context() soul injection.""" def test_includes_soul_content_when_exists(self, tmp_path): """get_system_context() includes soul content when soul.md exists.""" # Create temp soul.md soul_dir = tmp_path / "memory" / "self" soul_dir.mkdir(parents=True) soul_md = soul_dir / "soul.md" soul_md.write_text("# Soul\n\nI am Timmy. Unique-soul-marker.\n") # Create temp MEMORY.md memory_md = tmp_path / "MEMORY.md" memory_md.write_text("# Timmy Hot Memory\n\n## Current Status\n\nOperational\n") with ( patch("timmy.memory_system.SOUL_PATH", soul_md), patch("timmy.memory_system.HOT_MEMORY_PATH", memory_md), ): memory = MemorySystem() context = memory.get_system_context() assert "## Soul Identity" in context assert "Unique-soul-marker" in context def test_works_without_soul_file(self, tmp_path): """get_system_context() works fine when soul.md is missing.""" # Point to non-existent soul.md nonexistent_soul = tmp_path / "memory" / "self" / "soul.md" # Create temp MEMORY.md memory_md = tmp_path / "MEMORY.md" memory_md.write_text("# Timmy Hot Memory\n\n## Current Status\n\nOperational\n") with ( patch("timmy.memory_system.SOUL_PATH", nonexistent_soul), patch("timmy.memory_system.HOT_MEMORY_PATH", memory_md), ): memory = MemorySystem() context = memory.get_system_context() # Should not contain soul section but should still have hot memory assert "## Soul Identity" not in context assert "## Hot Memory" in context assert "Operational" in context def test_soul_comes_first_in_context(self, tmp_path): """Soul identity should be the first section in context.""" # Create temp soul.md soul_dir = tmp_path / "memory" / "self" soul_dir.mkdir(parents=True) soul_md = soul_dir / "soul.md" soul_md.write_text("# Soul\n\nI am Timmy.\n") # Create temp MEMORY.md memory_md = tmp_path / "MEMORY.md" memory_md.write_text("# Timmy Hot Memory\n\n## Current Status\n\nOperational\n") with ( patch("timmy.memory_system.SOUL_PATH", soul_md), patch("timmy.memory_system.HOT_MEMORY_PATH", memory_md), ): memory = MemorySystem() context = memory.get_system_context() # Soul should appear before hot memory soul_pos = context.find("## Soul Identity") hot_pos = context.find("## Hot Memory") assert soul_pos < hot_pos, "Soul Identity should come before Hot Memory" class TestReadSoul: """Tests for read_soul() method.""" def test_read_soul_returns_content_when_exists(self, tmp_path): """read_soul() returns content when soul.md exists.""" soul_dir = tmp_path / "memory" / "self" soul_dir.mkdir(parents=True) soul_md = soul_dir / "soul.md" soul_md.write_text("# Soul\n\nTest content.\n") with patch("timmy.memory_system.SOUL_PATH", soul_md): memory = MemorySystem() content = memory.read_soul() assert "Test content" in content def test_read_soul_returns_empty_when_missing(self, tmp_path): """read_soul() returns empty string when soul.md doesn't exist.""" nonexistent = tmp_path / "no_such_soul.md" with patch("timmy.memory_system.SOUL_PATH", nonexistent): memory = MemorySystem() content = memory.read_soul() assert content == ""