-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathtest_tool_build_context.py
More file actions
101 lines (77 loc) · 3.15 KB
/
test_tool_build_context.py
File metadata and controls
101 lines (77 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Tests for discussion context MCP tool."""
import pytest
from datetime import datetime
from mcp.server.fastmcp.exceptions import ToolError
from basic_memory.mcp.tools import build_context
from basic_memory.schemas.memory import (
GraphContext,
)
@pytest.mark.asyncio
async def test_get_basic_discussion_context(client, test_graph):
"""Test getting basic discussion context."""
context = await build_context(url="memory://test/root")
assert isinstance(context, GraphContext)
assert len(context.primary_results) == 1
assert context.primary_results[0].permalink == "test/root"
assert len(context.related_results) > 0
# Verify metadata
assert context.metadata.uri == "test/root"
assert context.metadata.depth == 1 # default depth
assert context.metadata.timeframe is not None
assert isinstance(context.metadata.generated_at, datetime)
@pytest.mark.asyncio
async def test_get_discussion_context_pattern(client, test_graph):
"""Test getting context with pattern matching."""
context = await build_context(url="memory://test/*", depth=1)
assert isinstance(context, GraphContext)
assert len(context.primary_results) > 1 # Should match multiple test/* paths
assert all("test/" in e.permalink for e in context.primary_results)
assert context.metadata.depth == 1
@pytest.mark.asyncio
async def test_get_discussion_context_timeframe(client, test_graph):
"""Test timeframe parameter filtering."""
# Get recent context
recent_context = await build_context(
url="memory://test/root",
timeframe="1d", # Last 24 hours
)
# Get older context
older_context = await build_context(
url="memory://test/root",
timeframe="30d", # Last 30 days
)
assert len(older_context.related_results) >= len(recent_context.related_results)
@pytest.mark.asyncio
async def test_get_discussion_context_not_found(client):
"""Test handling of non-existent URIs."""
context = await build_context(url="memory://test/does-not-exist")
assert isinstance(context, GraphContext)
assert len(context.primary_results) == 0
assert len(context.related_results) == 0
# Test data for different timeframe formats
valid_timeframes = [
"7d", # Standard format
"yesterday", # Natural language
"0d", # Zero duration
]
invalid_timeframes = [
"invalid", # Nonsense string
"tomorrow", # Future date
]
@pytest.mark.asyncio
async def test_build_context_timeframe_formats(client, test_graph):
"""Test that build_context accepts various timeframe formats."""
test_url = "memory://specs/test"
# Test each valid timeframe
for timeframe in valid_timeframes:
try:
result = await build_context(
url=test_url, timeframe=timeframe, page=1, page_size=10, max_related=10
)
assert result is not None
except Exception as e:
pytest.fail(f"Failed with valid timeframe '{timeframe}': {str(e)}")
# Test invalid timeframes should raise ValidationError
for timeframe in invalid_timeframes:
with pytest.raises(ToolError):
await build_context(url=test_url, timeframe=timeframe)