|
| 1 | +import pytest |
| 2 | +from unittest.mock import Mock, AsyncMock |
| 3 | + |
| 4 | +from mcp_neo4j_memory.server import format_namespace, create_mcp_server |
| 5 | +from mcp_neo4j_memory.neo4j_memory import Neo4jMemory, KnowledgeGraph |
| 6 | + |
| 7 | + |
| 8 | +class TestFormatNamespace: |
| 9 | + """Test the format_namespace function behavior.""" |
| 10 | + |
| 11 | + def testformat_namespace_empty_string(self): |
| 12 | + """Test format_namespace with empty string returns empty string.""" |
| 13 | + assert format_namespace("") == "" |
| 14 | + |
| 15 | + def testformat_namespace_no_hyphen(self): |
| 16 | + """Test format_namespace adds hyphen when not present.""" |
| 17 | + assert format_namespace("myapp") == "myapp-" |
| 18 | + |
| 19 | + def testformat_namespace_with_hyphen(self): |
| 20 | + """Test format_namespace returns string as-is when hyphen already present.""" |
| 21 | + assert format_namespace("myapp-") == "myapp-" |
| 22 | + |
| 23 | + def testformat_namespace_complex_name(self): |
| 24 | + """Test format_namespace with complex namespace names.""" |
| 25 | + assert format_namespace("company.product") == "company.product-" |
| 26 | + assert format_namespace("app_v2") == "app_v2-" |
| 27 | + |
| 28 | + |
| 29 | +class TestNamespacing: |
| 30 | + """Test namespacing functionality.""" |
| 31 | + |
| 32 | + @pytest.fixture |
| 33 | + def mock_memory(self): |
| 34 | + """Create a mock Neo4jMemory for testing.""" |
| 35 | + memory = Mock(spec=Neo4jMemory) |
| 36 | + # Mock all the async methods that the tools will call |
| 37 | + knowledge_graph = KnowledgeGraph(entities=[], relations=[]) |
| 38 | + memory.read_graph = AsyncMock(return_value=knowledge_graph) |
| 39 | + memory.create_entities = AsyncMock(return_value=[]) |
| 40 | + memory.create_relations = AsyncMock(return_value=[]) |
| 41 | + memory.add_observations = AsyncMock(return_value=[]) |
| 42 | + memory.delete_entities = AsyncMock(return_value=None) |
| 43 | + memory.delete_observations = AsyncMock(return_value=None) |
| 44 | + memory.delete_relations = AsyncMock(return_value=None) |
| 45 | + memory.search_memories = AsyncMock(return_value=knowledge_graph) |
| 46 | + memory.find_memories_by_name = AsyncMock(return_value=knowledge_graph) |
| 47 | + return memory |
| 48 | + |
| 49 | + @pytest.mark.asyncio |
| 50 | + async def test_namespace_tool_prefixes(self, mock_memory): |
| 51 | + """Test that tools are correctly prefixed with namespace.""" |
| 52 | + # Test with namespace |
| 53 | + namespaced_server = create_mcp_server(mock_memory, namespace="test-ns") |
| 54 | + tools = await namespaced_server.get_tools() |
| 55 | + |
| 56 | + expected_tools = [ |
| 57 | + "test-ns-read_graph", |
| 58 | + "test-ns-create_entities", |
| 59 | + "test-ns-create_relations", |
| 60 | + "test-ns-add_observations", |
| 61 | + "test-ns-delete_entities", |
| 62 | + "test-ns-delete_observations", |
| 63 | + "test-ns-delete_relations", |
| 64 | + "test-ns-search_memories", |
| 65 | + "test-ns-find_memories_by_name" |
| 66 | + ] |
| 67 | + |
| 68 | + for expected_tool in expected_tools: |
| 69 | + assert expected_tool in tools.keys(), f"Tool {expected_tool} not found in tools" |
| 70 | + |
| 71 | + # Test without namespace (default tools) |
| 72 | + default_server = create_mcp_server(mock_memory) |
| 73 | + default_tools = await default_server.get_tools() |
| 74 | + |
| 75 | + expected_default_tools = [ |
| 76 | + "read_graph", |
| 77 | + "create_entities", |
| 78 | + "create_relations", |
| 79 | + "add_observations", |
| 80 | + "delete_entities", |
| 81 | + "delete_observations", |
| 82 | + "delete_relations", |
| 83 | + "search_memories", |
| 84 | + "find_memories_by_name" |
| 85 | + ] |
| 86 | + |
| 87 | + for expected_tool in expected_default_tools: |
| 88 | + assert expected_tool in default_tools.keys(), f"Default tool {expected_tool} not found" |
| 89 | + |
| 90 | + @pytest.mark.asyncio |
| 91 | + async def test_namespace_tool_functionality(self, mock_memory): |
| 92 | + """Test that namespaced tools function correctly.""" |
| 93 | + namespaced_server = create_mcp_server(mock_memory, namespace="test") |
| 94 | + tools = await namespaced_server.get_tools() |
| 95 | + |
| 96 | + # Test that a namespaced tool exists and works |
| 97 | + read_tool = tools.get("test-read_graph") |
| 98 | + assert read_tool is not None |
| 99 | + |
| 100 | + # Call the tool function and verify it works |
| 101 | + result = await read_tool.fn() |
| 102 | + mock_memory.read_graph.assert_called_once() |
| 103 | + |
| 104 | + @pytest.mark.asyncio |
| 105 | + async def test_multiple_namespace_isolation(self, mock_memory): |
| 106 | + """Test that different namespaces create isolated tool sets.""" |
| 107 | + server_a = create_mcp_server(mock_memory, namespace="app-a") |
| 108 | + server_b = create_mcp_server(mock_memory, namespace="app-b") |
| 109 | + |
| 110 | + tools_a = await server_a.get_tools() |
| 111 | + tools_b = await server_b.get_tools() |
| 112 | + |
| 113 | + # Verify app-a tools exist in server_a but not server_b |
| 114 | + assert "app-a-read_graph" in tools_a.keys() |
| 115 | + assert "app-a-read_graph" not in tools_b.keys() |
| 116 | + |
| 117 | + # Verify app-b tools exist in server_b but not server_a |
| 118 | + assert "app-b-read_graph" in tools_b.keys() |
| 119 | + assert "app-b-read_graph" not in tools_a.keys() |
| 120 | + |
| 121 | + # Verify both servers have the same number of tools |
| 122 | + assert len(tools_a) == len(tools_b) |
| 123 | + |
| 124 | + @pytest.mark.asyncio |
| 125 | + async def test_namespace_hyphen_handling(self, mock_memory): |
| 126 | + """Test namespace hyphen handling edge cases.""" |
| 127 | + # Namespace already ending with hyphen |
| 128 | + server_with_hyphen = create_mcp_server(mock_memory, namespace="myapp-") |
| 129 | + tools_with_hyphen = await server_with_hyphen.get_tools() |
| 130 | + assert "myapp-read_graph" in tools_with_hyphen.keys() |
| 131 | + |
| 132 | + # Namespace without hyphen |
| 133 | + server_without_hyphen = create_mcp_server(mock_memory, namespace="myapp") |
| 134 | + tools_without_hyphen = await server_without_hyphen.get_tools() |
| 135 | + assert "myapp-read_graph" in tools_without_hyphen.keys() |
| 136 | + |
| 137 | + # Both should result in identical tool names |
| 138 | + assert set(tools_with_hyphen.keys()) == set(tools_without_hyphen.keys()) |
| 139 | + |
| 140 | + @pytest.mark.asyncio |
| 141 | + async def test_complex_namespace_names(self, mock_memory): |
| 142 | + """Test complex namespace naming scenarios.""" |
| 143 | + complex_namespaces = [ |
| 144 | + "company.product", |
| 145 | + "app_v2", |
| 146 | + "client-123", |
| 147 | + "test.env.staging" |
| 148 | + ] |
| 149 | + |
| 150 | + for namespace in complex_namespaces: |
| 151 | + server = create_mcp_server(mock_memory, namespace=namespace) |
| 152 | + tools = await server.get_tools() |
| 153 | + |
| 154 | + # Verify tools are properly prefixed |
| 155 | + expected_tool = f"{namespace}-read_graph" |
| 156 | + assert expected_tool in tools.keys(), f"Tool {expected_tool} not found for namespace '{namespace}'" |
| 157 | + |
| 158 | + @pytest.mark.asyncio |
| 159 | + async def test_namespace_tool_count_consistency(self, mock_memory): |
| 160 | + """Test that namespaced and default servers have the same number of tools.""" |
| 161 | + default_server = create_mcp_server(mock_memory) |
| 162 | + namespaced_server = create_mcp_server(mock_memory, namespace="test") |
| 163 | + |
| 164 | + default_tools = await default_server.get_tools() |
| 165 | + namespaced_tools = await namespaced_server.get_tools() |
| 166 | + |
| 167 | + # Should have the same number of tools |
| 168 | + assert len(default_tools) == len(namespaced_tools) |
| 169 | + |
| 170 | + # Verify we have the expected number of tools (9 tools based on the server implementation) |
| 171 | + assert len(default_tools) == 9 |
| 172 | + assert len(namespaced_tools) == 9 |
0 commit comments