|
| 1 | +"""Integration tests for build_context memory URL validation.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastmcp import Client |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.asyncio |
| 8 | +async def test_build_context_valid_urls(mcp_server, app): |
| 9 | + """Test that build_context works with valid memory URLs.""" |
| 10 | + |
| 11 | + async with Client(mcp_server) as client: |
| 12 | + # Create a test note to ensure we have something to find |
| 13 | + await client.call_tool( |
| 14 | + "write_note", |
| 15 | + { |
| 16 | + "title": "URL Validation Test", |
| 17 | + "folder": "testing", |
| 18 | + "content": "# URL Validation Test\n\nThis note tests URL validation.", |
| 19 | + "tags": "test,validation", |
| 20 | + }, |
| 21 | + ) |
| 22 | + |
| 23 | + # Test various valid URL formats |
| 24 | + valid_urls = [ |
| 25 | + "memory://testing/url-validation-test", # Full memory URL |
| 26 | + "testing/url-validation-test", # Relative path |
| 27 | + "testing/*", # Pattern matching |
| 28 | + ] |
| 29 | + |
| 30 | + for url in valid_urls: |
| 31 | + result = await client.call_tool("build_context", {"url": url}) |
| 32 | + |
| 33 | + # Should return a valid GraphContext response |
| 34 | + assert len(result) == 1 |
| 35 | + response = result[0].text |
| 36 | + assert '"results"' in response # Should contain results structure |
| 37 | + assert '"metadata"' in response # Should contain metadata |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_build_context_invalid_urls_fail_validation(mcp_server, app): |
| 42 | + """Test that build_context properly validates and rejects invalid memory URLs.""" |
| 43 | + |
| 44 | + async with Client(mcp_server) as client: |
| 45 | + # Test cases: (invalid_url, expected_error_fragment) |
| 46 | + invalid_test_cases = [ |
| 47 | + ("memory//test", "double slashes"), |
| 48 | + ("invalid://test", "protocol scheme"), |
| 49 | + ("notes<brackets>", "invalid characters"), |
| 50 | + ('notes"quotes"', "invalid characters"), |
| 51 | + ] |
| 52 | + |
| 53 | + for invalid_url, expected_error in invalid_test_cases: |
| 54 | + with pytest.raises(Exception) as exc_info: |
| 55 | + await client.call_tool("build_context", {"url": invalid_url}) |
| 56 | + |
| 57 | + error_message = str(exc_info.value).lower() |
| 58 | + assert expected_error in error_message, ( |
| 59 | + f"URL '{invalid_url}' should fail with '{expected_error}' error" |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_build_context_empty_urls_fail_validation(mcp_server, app): |
| 65 | + """Test that empty or whitespace-only URLs fail validation.""" |
| 66 | + |
| 67 | + async with Client(mcp_server) as client: |
| 68 | + # These should fail MinLen validation |
| 69 | + empty_urls = [ |
| 70 | + "", # Empty string |
| 71 | + " ", # Whitespace only |
| 72 | + ] |
| 73 | + |
| 74 | + for empty_url in empty_urls: |
| 75 | + with pytest.raises(Exception) as exc_info: |
| 76 | + await client.call_tool("build_context", {"url": empty_url}) |
| 77 | + |
| 78 | + error_message = str(exc_info.value) |
| 79 | + # Should fail with validation error (either MinLen or our custom validation) |
| 80 | + assert ( |
| 81 | + "at least 1" in error_message |
| 82 | + or "too_short" in error_message |
| 83 | + or "empty or whitespace" in error_message |
| 84 | + or "value_error" in error_message |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_build_context_nonexistent_urls_return_empty_results(mcp_server, app): |
| 90 | + """Test that valid but nonexistent URLs return empty results (not errors).""" |
| 91 | + |
| 92 | + async with Client(mcp_server) as client: |
| 93 | + # These are valid URL formats but don't exist in the system |
| 94 | + nonexistent_valid_urls = [ |
| 95 | + "memory://nonexistent/note", |
| 96 | + "nonexistent/note", |
| 97 | + "missing/*", |
| 98 | + ] |
| 99 | + |
| 100 | + for url in nonexistent_valid_urls: |
| 101 | + result = await client.call_tool("build_context", {"url": url}) |
| 102 | + |
| 103 | + # Should return valid response with empty results |
| 104 | + assert len(result) == 1 |
| 105 | + response = result[0].text |
| 106 | + assert '"results": []' in response # Empty results |
| 107 | + assert '"total_results": 0' in response # Zero count |
| 108 | + assert '"metadata"' in response # But should have metadata |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_build_context_error_messages_are_helpful(mcp_server, app): |
| 113 | + """Test that validation error messages provide helpful guidance.""" |
| 114 | + |
| 115 | + async with Client(mcp_server) as client: |
| 116 | + # Test double slash error message |
| 117 | + with pytest.raises(Exception) as exc_info: |
| 118 | + await client.call_tool("build_context", {"url": "memory//bad"}) |
| 119 | + |
| 120 | + error_msg = str(exc_info.value).lower() |
| 121 | + # Should contain validation error info |
| 122 | + assert ( |
| 123 | + "double slashes" in error_msg |
| 124 | + or "value_error" in error_msg |
| 125 | + or "validation error" in error_msg |
| 126 | + ) |
| 127 | + |
| 128 | + # Test protocol scheme error message |
| 129 | + with pytest.raises(Exception) as exc_info: |
| 130 | + await client.call_tool("build_context", {"url": "http://example.com"}) |
| 131 | + |
| 132 | + error_msg = str(exc_info.value).lower() |
| 133 | + assert ( |
| 134 | + "protocol scheme" in error_msg |
| 135 | + or "protocol" in error_msg |
| 136 | + or "value_error" in error_msg |
| 137 | + or "validation error" in error_msg |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.asyncio |
| 142 | +async def test_build_context_pattern_matching_works(mcp_server, app): |
| 143 | + """Test that valid pattern matching URLs work correctly.""" |
| 144 | + |
| 145 | + async with Client(mcp_server) as client: |
| 146 | + # Create multiple test notes |
| 147 | + test_notes = [ |
| 148 | + ("Pattern Test One", "patterns", "# Pattern Test One\n\nFirst pattern test."), |
| 149 | + ("Pattern Test Two", "patterns", "# Pattern Test Two\n\nSecond pattern test."), |
| 150 | + ("Other Note", "other", "# Other Note\n\nNot a pattern match."), |
| 151 | + ] |
| 152 | + |
| 153 | + for title, folder, content in test_notes: |
| 154 | + await client.call_tool( |
| 155 | + "write_note", |
| 156 | + { |
| 157 | + "title": title, |
| 158 | + "folder": folder, |
| 159 | + "content": content, |
| 160 | + }, |
| 161 | + ) |
| 162 | + |
| 163 | + # Test pattern matching |
| 164 | + result = await client.call_tool("build_context", {"url": "patterns/*"}) |
| 165 | + |
| 166 | + assert len(result) == 1 |
| 167 | + response = result[0].text |
| 168 | + |
| 169 | + # Should find the pattern matches but not the other note |
| 170 | + assert '"total_results": 2' in response or '"primary_count": 2' in response |
| 171 | + assert "Pattern Test" in response |
| 172 | + assert "Other Note" not in response |
0 commit comments