|
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
| 9 | +from contextlib import asynccontextmanager |
| 10 | +from pathlib import Path |
| 11 | +from types import SimpleNamespace |
| 12 | + |
9 | 13 | import pytest |
10 | 14 | from mcp.server.fastmcp.exceptions import ToolError |
11 | 15 |
|
@@ -157,6 +161,109 @@ async def test_read_content_allows_safe_path_integration(client, test_project): |
157 | 161 | assert "safe note" in result["text"] |
158 | 162 |
|
159 | 163 |
|
| 164 | +@pytest.mark.asyncio |
| 165 | +async def test_read_content_workspace_memory_url_routes_with_local_config( |
| 166 | + monkeypatch, |
| 167 | + config_manager, |
| 168 | +): |
| 169 | + """Workspace-qualified memory URLs should route even when local projects exist.""" |
| 170 | + import importlib |
| 171 | + |
| 172 | + import basic_memory.mcp.project_context as project_context |
| 173 | + from basic_memory.config import ProjectEntry |
| 174 | + from basic_memory.mcp.project_context import ( |
| 175 | + WorkspaceProjectEntry, |
| 176 | + _build_workspace_project_index, |
| 177 | + ) |
| 178 | + from basic_memory.schemas.cloud import WorkspaceInfo |
| 179 | + from basic_memory.schemas.project_info import ProjectItem |
| 180 | + |
| 181 | + read_content_module = importlib.import_module("basic_memory.mcp.tools.read_content") |
| 182 | + config = config_manager.load_config() |
| 183 | + config.projects["hermes-memory"] = ProjectEntry( |
| 184 | + path=str(config_manager.config_dir.parent / "hermes-memory") |
| 185 | + ) |
| 186 | + config.cloud_api_key = "bmc_test123" |
| 187 | + config_manager.save_config(config) |
| 188 | + |
| 189 | + personal = WorkspaceInfo( |
| 190 | + tenant_id="personal-tenant", |
| 191 | + workspace_type="personal", |
| 192 | + slug="personal", |
| 193 | + name="Personal", |
| 194 | + role="owner", |
| 195 | + is_default=True, |
| 196 | + ) |
| 197 | + project = ProjectItem( |
| 198 | + id=1, |
| 199 | + external_id="11111111-1111-1111-1111-111111111111", |
| 200 | + name="main", |
| 201 | + path="/tmp/main", |
| 202 | + is_default=False, |
| 203 | + ) |
| 204 | + index = _build_workspace_project_index( |
| 205 | + (personal,), |
| 206 | + (WorkspaceProjectEntry(workspace=personal, project=project),), |
| 207 | + ) |
| 208 | + |
| 209 | + async def fake_index(context=None): |
| 210 | + return index |
| 211 | + |
| 212 | + @asynccontextmanager |
| 213 | + async def fake_get_project_client(project=None, context=None, project_id=None): |
| 214 | + assert project == "personal/main" |
| 215 | + assert project_id is None |
| 216 | + yield ( |
| 217 | + object(), |
| 218 | + SimpleNamespace( |
| 219 | + name="main", |
| 220 | + external_id="11111111-1111-1111-1111-111111111111", |
| 221 | + home=Path("/tmp/main"), |
| 222 | + ), |
| 223 | + ) |
| 224 | + |
| 225 | + async def fake_resolve_project_and_path(client, identifier, project=None, context=None): |
| 226 | + assert identifier == "memory://personal/main/docs/report" |
| 227 | + assert project == "main" |
| 228 | + return None, "personal/main/docs/report", True |
| 229 | + |
| 230 | + async def fake_resolve_entity_id(client, project_id, url): |
| 231 | + assert project_id == "11111111-1111-1111-1111-111111111111" |
| 232 | + assert url == "personal/main/docs/report" |
| 233 | + return "entity-1" |
| 234 | + |
| 235 | + class FakeResponse: |
| 236 | + headers = {"content-type": "text/markdown", "content-length": "17"} |
| 237 | + text = "# Routed Content" |
| 238 | + content = b"# Routed Content" |
| 239 | + |
| 240 | + async def fake_call_get(client, path, **kwargs): |
| 241 | + assert path == "/v2/projects/11111111-1111-1111-1111-111111111111/resource/entity-1" |
| 242 | + return FakeResponse() |
| 243 | + |
| 244 | + monkeypatch.setattr(project_context, "_ensure_workspace_project_index", fake_index) |
| 245 | + monkeypatch.setattr("basic_memory.mcp.async_client.is_factory_mode", lambda: False) |
| 246 | + monkeypatch.setattr("basic_memory.mcp.async_client._explicit_routing", lambda: False) |
| 247 | + monkeypatch.setattr("basic_memory.mcp.async_client._force_local_mode", lambda: False) |
| 248 | + monkeypatch.setattr(read_content_module, "get_project_client", fake_get_project_client) |
| 249 | + monkeypatch.setattr( |
| 250 | + read_content_module, |
| 251 | + "resolve_project_and_path", |
| 252 | + fake_resolve_project_and_path, |
| 253 | + ) |
| 254 | + monkeypatch.setattr(read_content_module, "resolve_entity_id", fake_resolve_entity_id) |
| 255 | + monkeypatch.setattr(read_content_module, "call_get", fake_call_get) |
| 256 | + |
| 257 | + result = await read_content(path="memory://personal/main/docs/report") |
| 258 | + |
| 259 | + assert result == { |
| 260 | + "type": "text", |
| 261 | + "text": "# Routed Content", |
| 262 | + "content_type": "text/markdown", |
| 263 | + "encoding": "utf-8", |
| 264 | + } |
| 265 | + |
| 266 | + |
160 | 267 | @pytest.mark.asyncio |
161 | 268 | async def test_read_content_empty_path_does_not_trigger_security_error(client, test_project): |
162 | 269 | try: |
|
0 commit comments