|
| 1 | +"""Tests for client long_term_memory_strategy parameter support.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from agent_memory_client import MemoryAPIClient, MemoryClientConfig |
| 5 | +from agent_memory_client.models import MemoryStrategyConfig |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +async def memory_client(use_test_redis_connection): |
| 10 | + """Create a memory client for testing.""" |
| 11 | + from agent_memory_client import __version__ |
| 12 | + |
| 13 | + config = MemoryClientConfig( |
| 14 | + base_url="http://test", |
| 15 | + disable_auth=True, |
| 16 | + ) |
| 17 | + |
| 18 | + # Import here to avoid circular imports |
| 19 | + from httpx import ASGITransport, AsyncClient |
| 20 | + |
| 21 | + from agent_memory_server.main import app |
| 22 | + |
| 23 | + async with AsyncClient( |
| 24 | + transport=ASGITransport(app=app), |
| 25 | + base_url="http://test", |
| 26 | + headers={ |
| 27 | + "User-Agent": f"agent-memory-client/{__version__}", |
| 28 | + "X-Client-Version": __version__, |
| 29 | + }, |
| 30 | + ) as http_client: |
| 31 | + client = MemoryAPIClient(config=config) |
| 32 | + client._client = http_client |
| 33 | + yield client |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.asyncio |
| 37 | +async def test_get_or_create_working_memory_with_strategy( |
| 38 | + memory_client: MemoryAPIClient, |
| 39 | +): |
| 40 | + """Test get_or_create_working_memory with long_term_memory_strategy parameter.""" |
| 41 | + session_id = "test-strategy-session-1" |
| 42 | + |
| 43 | + # Create with custom strategy |
| 44 | + strategy = MemoryStrategyConfig( |
| 45 | + strategy="summary", config={"max_summary_length": 500} |
| 46 | + ) |
| 47 | + |
| 48 | + created, memory = await memory_client.get_or_create_working_memory( |
| 49 | + session_id=session_id, |
| 50 | + long_term_memory_strategy=strategy, |
| 51 | + ) |
| 52 | + |
| 53 | + assert created is True |
| 54 | + assert memory.session_id == session_id |
| 55 | + assert memory.long_term_memory_strategy.strategy == "summary" |
| 56 | + assert memory.long_term_memory_strategy.config == {"max_summary_length": 500} |
| 57 | + |
| 58 | + # Get existing session - strategy should be preserved |
| 59 | + created2, memory2 = await memory_client.get_or_create_working_memory( |
| 60 | + session_id=session_id, |
| 61 | + ) |
| 62 | + |
| 63 | + assert created2 is False |
| 64 | + assert memory2.session_id == session_id |
| 65 | + assert memory2.long_term_memory_strategy.strategy == "summary" |
| 66 | + assert memory2.long_term_memory_strategy.config == {"max_summary_length": 500} |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_get_or_create_working_memory_tool_with_strategy( |
| 71 | + memory_client: MemoryAPIClient, |
| 72 | +): |
| 73 | + """Test get_or_create_working_memory_tool with long_term_memory_strategy parameter.""" |
| 74 | + session_id = "test-strategy-session-2" |
| 75 | + |
| 76 | + # Create with preferences strategy |
| 77 | + strategy = MemoryStrategyConfig(strategy="preferences", config={}) |
| 78 | + |
| 79 | + result = await memory_client.get_or_create_working_memory_tool( |
| 80 | + session_id=session_id, |
| 81 | + long_term_memory_strategy=strategy, |
| 82 | + ) |
| 83 | + |
| 84 | + assert result["created"] is True |
| 85 | + assert result["session_id"] == session_id |
| 86 | + |
| 87 | + # Verify the strategy was applied by getting the session |
| 88 | + created, memory = await memory_client.get_or_create_working_memory( |
| 89 | + session_id=session_id, |
| 90 | + ) |
| 91 | + |
| 92 | + assert created is False |
| 93 | + assert memory.long_term_memory_strategy.strategy == "preferences" |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_get_or_create_working_memory_with_custom_strategy( |
| 98 | + memory_client: MemoryAPIClient, |
| 99 | +): |
| 100 | + """Test get_or_create_working_memory with custom strategy.""" |
| 101 | + session_id = "test-strategy-session-3" |
| 102 | + |
| 103 | + # Create with custom strategy |
| 104 | + strategy = MemoryStrategyConfig( |
| 105 | + strategy="custom", |
| 106 | + config={ |
| 107 | + "custom_prompt": "Extract technical decisions from: {message}\nReturn JSON." |
| 108 | + }, |
| 109 | + ) |
| 110 | + |
| 111 | + created, memory = await memory_client.get_or_create_working_memory( |
| 112 | + session_id=session_id, |
| 113 | + long_term_memory_strategy=strategy, |
| 114 | + ) |
| 115 | + |
| 116 | + assert created is True |
| 117 | + assert memory.session_id == session_id |
| 118 | + assert memory.long_term_memory_strategy.strategy == "custom" |
| 119 | + assert "custom_prompt" in memory.long_term_memory_strategy.config |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.asyncio |
| 123 | +async def test_get_or_create_working_memory_default_strategy( |
| 124 | + memory_client: MemoryAPIClient, |
| 125 | +): |
| 126 | + """Test get_or_create_working_memory uses default strategy when not specified.""" |
| 127 | + session_id = "test-strategy-session-4" |
| 128 | + |
| 129 | + # Create without specifying strategy |
| 130 | + created, memory = await memory_client.get_or_create_working_memory( |
| 131 | + session_id=session_id, |
| 132 | + ) |
| 133 | + |
| 134 | + assert created is True |
| 135 | + assert memory.session_id == session_id |
| 136 | + # Should default to discrete strategy |
| 137 | + assert memory.long_term_memory_strategy.strategy == "discrete" |
| 138 | + assert memory.long_term_memory_strategy.config == {} |
0 commit comments