Files
mcp-synology-filestation/tests/test_tools_filestation.py
T
marcus a3e1a557c3 fix: use share paths, drop additional from list_dir, remove debug logging
Root cause: DSM expects share paths (/dev) not volume paths (/volume1/dev).
The 408 errors were triggered by any additional field on the wrong path format.

- list_shares: use share["path"] directly (e.g. /dev), drop real_path from
  additional — only volume_status remains
- list_dir: remove additional parameter entirely; table now shows name + type
  (isdir is returned by default); update docstring to show share path examples
- client.py: remove diagnostic REQUEST and RAW ERROR stderr logging
- tests: update assertions to match share paths and two-column table output

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 09:11:10 +02:00

233 lines
7.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Tests for tools/filestation.py: list_shares and list_dir."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from mcp_synology_filestation.client import SynologyError
from mcp_synology_filestation.config import AppConfig, ConnectionConfig
@pytest.fixture()
def config() -> AppConfig:
return AppConfig(
schema_version=1,
connection=ConnectionConfig(host="nas.example.com"),
)
def _make_mcp_and_tools(config: AppConfig, client: MagicMock) -> dict:
"""Register FileStation tools on a mock FastMCP and collect them by name."""
from mcp_synology_filestation.tools.filestation import register_filestation
registered: dict[str, object] = {}
mcp = MagicMock()
def tool_decorator():
"""Simulate @mcp.tool() — capture the decorated function."""
def decorator(fn):
registered[fn.__name__] = fn
return fn
return decorator
mcp.tool = tool_decorator
register_filestation(mcp, config, client)
return registered
# ──────────────────────────────────────────────────────────────────────────
# list_shares
# ──────────────────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_list_shares_success(config: AppConfig) -> None:
"""list_shares returns a formatted table on success."""
client = MagicMock()
client.request = AsyncMock(
return_value={
"shares": [
{
"name": "data",
"path": "/data",
"additional": {
"real_path": "/volume1/data",
"volume_status": {
"totalspace": 2_000_000_000,
"usedspace": 500_000_000,
},
},
},
{
"name": "photos",
"path": "/photos",
"additional": {
"real_path": "/volume1/photos",
"volume_status": {},
},
},
]
}
)
tools = _make_mcp_and_tools(config, client)
result = await tools["list_shares"]()
assert "data" in result
assert "/data" in result # share path, not volume path
assert "/volume1/data" not in result
assert "photos" in result
assert "2 share(s) found" in result
# Check usage percentage appears
assert "%" in result
@pytest.mark.asyncio
async def test_list_shares_empty(config: AppConfig) -> None:
"""list_shares returns a friendly message when no shares exist."""
client = MagicMock()
client.request = AsyncMock(return_value={"shares": []})
tools = _make_mcp_and_tools(config, client)
result = await tools["list_shares"]()
assert "No shared folders" in result
@pytest.mark.asyncio
async def test_list_shares_dsm_error(config: AppConfig) -> None:
"""list_shares returns an Error: message on SynologyError."""
client = MagicMock()
client.request = AsyncMock(
side_effect=SynologyError("Permission denied — check DSM user permissions", code=105)
)
tools = _make_mcp_and_tools(config, client)
result = await tools["list_shares"]()
assert result.startswith("Error:")
assert "Permission denied" in result
# ──────────────────────────────────────────────────────────────────────────
# list_dir
# ──────────────────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_list_dir_success(config: AppConfig) -> None:
"""list_dir returns a formatted table with name and type columns."""
client = MagicMock()
client.request = AsyncMock(
return_value={
"total": 3,
"files": [
{"name": "documents", "isdir": True},
{"name": "photo.jpg", "isdir": False},
{"name": "readme.txt", "isdir": False},
],
}
)
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/dev")
assert "documents" in result
assert "photo.jpg" in result
assert "readme.txt" in result
assert "dir" in result
assert "file" in result
assert "Showing 13 of 3 item(s)" in result
@pytest.mark.asyncio
async def test_list_dir_pagination(config: AppConfig) -> None:
"""list_dir shows pagination hint when more items are available."""
client = MagicMock()
client.request = AsyncMock(
return_value={
"total": 200,
"files": [
{"name": f"file{i}.txt", "isdir": False}
for i in range(100)
],
}
)
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/volume1/data", limit=100)
assert "Showing 1100 of 200" in result
assert "offset=100" in result
@pytest.mark.asyncio
async def test_list_dir_empty(config: AppConfig) -> None:
"""list_dir returns a friendly message for empty directories."""
client = MagicMock()
client.request = AsyncMock(return_value={"total": 0, "files": []})
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/volume1/empty")
assert "empty" in result.lower() or "does not exist" in result.lower()
@pytest.mark.asyncio
async def test_list_dir_invalid_sort_by(config: AppConfig) -> None:
"""list_dir returns an Error: message for invalid sort_by."""
client = MagicMock()
client.request = AsyncMock()
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/volume1/data", sort_by="invalid_field")
assert result.startswith("Error:")
assert "sort_by" in result
client.request.assert_not_called()
@pytest.mark.asyncio
async def test_list_dir_invalid_sort_direction(config: AppConfig) -> None:
"""list_dir returns an Error: message for invalid sort_direction."""
client = MagicMock()
client.request = AsyncMock()
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/volume1/data", sort_direction="random")
assert result.startswith("Error:")
assert "sort_direction" in result
client.request.assert_not_called()
@pytest.mark.asyncio
async def test_list_dir_limit_clamped(config: AppConfig) -> None:
"""list_dir clamps limit to _MAX_LIMIT (500)."""
client = MagicMock()
client.request = AsyncMock(return_value={"total": 1, "files": []})
tools = _make_mcp_and_tools(config, client)
await tools["list_dir"](path="/volume1/data", limit=9999)
call_params = client.request.call_args[1]["params"]
assert call_params["limit"] == 500
@pytest.mark.asyncio
async def test_list_dir_dsm_error(config: AppConfig) -> None:
"""list_dir returns Error: on SynologyError (e.g. path not found)."""
client = MagicMock()
client.request = AsyncMock(side_effect=SynologyError("File or folder not found", code=1800))
tools = _make_mcp_and_tools(config, client)
result = await tools["list_dir"](path="/volume1/nonexistent")
assert result.startswith("Error:")
assert "not found" in result.lower()