|
| 1 | +"""Tests for discussion context MCP tool.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp.server.fastmcp.exceptions import ToolError |
| 6 | + |
| 7 | +from basic_memory.mcp.tools import recent_activity |
| 8 | +from basic_memory.schemas.memory import ( |
| 9 | + EntitySummary, |
| 10 | + ObservationSummary, |
| 11 | + RelationSummary, |
| 12 | +) |
| 13 | +from basic_memory.schemas.search import SearchItemType |
| 14 | + |
| 15 | +# Test data for different timeframe formats |
| 16 | +valid_timeframes = [ |
| 17 | + "7d", # Standard format |
| 18 | + "yesterday", # Natural language |
| 19 | + "0d", # Zero duration |
| 20 | +] |
| 21 | + |
| 22 | +invalid_timeframes = [ |
| 23 | + "invalid", # Nonsense string |
| 24 | + "tomorrow", # Future date |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_recent_activity_timeframe_formats(client, test_graph): |
| 30 | + """Test that recent_activity accepts various timeframe formats.""" |
| 31 | + # Test each valid timeframe |
| 32 | + for timeframe in valid_timeframes: |
| 33 | + try: |
| 34 | + result = await recent_activity( |
| 35 | + type=["entity"], timeframe=timeframe, page=1, page_size=10, max_related=10 |
| 36 | + ) |
| 37 | + assert result is not None |
| 38 | + except Exception as e: |
| 39 | + pytest.fail(f"Failed with valid timeframe '{timeframe}': {str(e)}") |
| 40 | + |
| 41 | + # Test invalid timeframes should raise ValidationError |
| 42 | + for timeframe in invalid_timeframes: |
| 43 | + with pytest.raises(ToolError): |
| 44 | + await recent_activity(timeframe=timeframe) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_recent_activity_type_filters(client, test_graph): |
| 49 | + """Test that recent_activity correctly filters by types.""" |
| 50 | + |
| 51 | + # Test single string type |
| 52 | + result = await recent_activity(type=SearchItemType.ENTITY) |
| 53 | + assert result is not None |
| 54 | + assert all(isinstance(r, EntitySummary) for r in result.primary_results) |
| 55 | + |
| 56 | + # Test single string type |
| 57 | + result = await recent_activity(type="entity") |
| 58 | + assert result is not None |
| 59 | + assert all(isinstance(r, EntitySummary) for r in result.primary_results) |
| 60 | + |
| 61 | + # Test single type |
| 62 | + result = await recent_activity(type=["entity"]) |
| 63 | + assert result is not None |
| 64 | + assert all(isinstance(r, EntitySummary) for r in result.primary_results) |
| 65 | + |
| 66 | + # Test multiple types |
| 67 | + result = await recent_activity(type=["entity", "observation"]) |
| 68 | + assert result is not None |
| 69 | + assert all( |
| 70 | + isinstance(r, EntitySummary) or isinstance(r, ObservationSummary) |
| 71 | + for r in result.primary_results |
| 72 | + ) |
| 73 | + |
| 74 | + # Test multiple types |
| 75 | + result = await recent_activity(type=[SearchItemType.ENTITY, SearchItemType.OBSERVATION]) |
| 76 | + assert result is not None |
| 77 | + assert all( |
| 78 | + isinstance(r, EntitySummary) or isinstance(r, ObservationSummary) |
| 79 | + for r in result.primary_results |
| 80 | + ) |
| 81 | + |
| 82 | + # Test all types |
| 83 | + result = await recent_activity(type=["entity", "observation", "relation"]) |
| 84 | + assert result is not None |
| 85 | + # Results can be any type |
| 86 | + assert all( |
| 87 | + isinstance(r, EntitySummary) |
| 88 | + or isinstance(r, ObservationSummary) |
| 89 | + or isinstance(r, RelationSummary) |
| 90 | + for r in result.primary_results |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_recent_activity_type_invalid(client, test_graph): |
| 96 | + """Test that recent_activity correctly filters by types.""" |
| 97 | + |
| 98 | + # Test single invalid string type |
| 99 | + with pytest.raises(ValueError) as e: |
| 100 | + await recent_activity(type="note") |
| 101 | + assert ( |
| 102 | + str(e.value) == "Invalid type: note. Valid types are: ['entity', 'observation', 'relation']" |
| 103 | + ) |
| 104 | + |
| 105 | + # Test invalid string array type |
| 106 | + with pytest.raises(ValueError) as e: |
| 107 | + await recent_activity(type=["note"]) |
| 108 | + assert ( |
| 109 | + str(e.value) == "Invalid type: note. Valid types are: ['entity', 'observation', 'relation']" |
| 110 | + ) |
0 commit comments