Split the monolithic syncthing_mcp.py into a proper src/syncthing_mcp/ package with focused modules: client, models, registry, server, and tool submodules (system, folders, devices, config, instances). New tools: browse_folder, file_info, folder_need (file-level queries), override_folder, revert_folder (conflict resolution), check_upgrade (system management). Bumps tool count from 30 to 36. Adds comprehensive test suite (96 tests) using pytest + respx for HTTP mocking, covering client methods, registry logic, model validation, and all tool functions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
"""Tests for instance registry loading and resolution."""
|
|
|
|
import json
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from syncthing_mcp.client import SyncthingClient
|
|
from syncthing_mcp.registry import (
|
|
format_bytes,
|
|
get_instance,
|
|
handle_error_global,
|
|
load_instances,
|
|
reload_instances,
|
|
)
|
|
|
|
|
|
class TestFormatBytes:
|
|
def test_zero(self):
|
|
assert format_bytes(0) == "0.0 B"
|
|
|
|
def test_bytes(self):
|
|
assert format_bytes(512) == "512.0 B"
|
|
|
|
def test_kilobytes(self):
|
|
assert format_bytes(2048) == "2.0 KB"
|
|
|
|
def test_megabytes(self):
|
|
assert format_bytes(5 * 1024 * 1024) == "5.0 MB"
|
|
|
|
def test_gigabytes(self):
|
|
assert format_bytes(3 * 1024**3) == "3.0 GB"
|
|
|
|
def test_terabytes(self):
|
|
assert format_bytes(2 * 1024**4) == "2.0 TB"
|
|
|
|
|
|
class TestLoadInstances:
|
|
def test_single_instance_defaults(self, monkeypatch):
|
|
monkeypatch.delenv("SYNCTHING_INSTANCES", raising=False)
|
|
monkeypatch.setenv("SYNCTHING_API_KEY", "mykey")
|
|
monkeypatch.delenv("SYNCTHING_URL", raising=False)
|
|
instances = load_instances()
|
|
assert "default" in instances
|
|
assert instances["default"].url == "http://localhost:8384"
|
|
assert instances["default"].api_key == "mykey"
|
|
|
|
def test_single_instance_custom_url(self, monkeypatch):
|
|
monkeypatch.delenv("SYNCTHING_INSTANCES", raising=False)
|
|
monkeypatch.setenv("SYNCTHING_API_KEY", "mykey")
|
|
monkeypatch.setenv("SYNCTHING_URL", "http://nas.local:8384")
|
|
instances = load_instances()
|
|
assert instances["default"].url == "http://nas.local:8384"
|
|
|
|
def test_multi_instance(self, monkeypatch):
|
|
cfg = {
|
|
"alpha": {"url": "http://a:8384", "api_key": "ka"},
|
|
"beta": {"url": "http://b:8384", "api_key": "kb"},
|
|
}
|
|
monkeypatch.setenv("SYNCTHING_INSTANCES", json.dumps(cfg))
|
|
instances = load_instances()
|
|
assert len(instances) == 2
|
|
assert instances["alpha"].url == "http://a:8384"
|
|
assert instances["beta"].api_key == "kb"
|
|
|
|
def test_invalid_json(self, monkeypatch):
|
|
monkeypatch.setenv("SYNCTHING_INSTANCES", "{bad json")
|
|
with pytest.raises(ValueError, match="Invalid SYNCTHING_INSTANCES JSON"):
|
|
load_instances()
|
|
|
|
def test_empty_object(self, monkeypatch):
|
|
monkeypatch.setenv("SYNCTHING_INSTANCES", "{}")
|
|
with pytest.raises(ValueError, match="non-empty"):
|
|
load_instances()
|
|
|
|
def test_non_dict_entry(self, monkeypatch):
|
|
monkeypatch.setenv("SYNCTHING_INSTANCES", json.dumps({"a": "not a dict"}))
|
|
with pytest.raises(ValueError, match="must be a JSON object"):
|
|
load_instances()
|
|
|
|
def test_url_trailing_slash_stripped(self, monkeypatch):
|
|
monkeypatch.delenv("SYNCTHING_INSTANCES", raising=False)
|
|
monkeypatch.setenv("SYNCTHING_API_KEY", "k")
|
|
monkeypatch.setenv("SYNCTHING_URL", "http://localhost:8384/")
|
|
instances = load_instances()
|
|
assert instances["default"].url == "http://localhost:8384"
|
|
|
|
|
|
class TestGetInstance:
|
|
def test_auto_select_single(self, monkeypatch, single_instance_env):
|
|
reload_instances()
|
|
inst = get_instance(None)
|
|
assert inst.name == "default"
|
|
|
|
def test_explicit_name(self, monkeypatch, multi_instance_env):
|
|
reload_instances()
|
|
inst = get_instance("alpha")
|
|
assert inst.name == "alpha"
|
|
|
|
def test_missing_name(self, monkeypatch, multi_instance_env):
|
|
reload_instances()
|
|
with pytest.raises(ValueError, match="not found"):
|
|
get_instance("nonexistent")
|
|
|
|
def test_ambiguous_without_name(self, monkeypatch, multi_instance_env):
|
|
reload_instances()
|
|
with pytest.raises(ValueError, match="Multiple instances"):
|
|
get_instance(None)
|
|
|
|
|
|
class TestHandleErrorGlobal:
|
|
def test_value_error(self):
|
|
msg = handle_error_global(ValueError("bad input"))
|
|
assert msg == "Error: bad input"
|
|
|
|
def test_generic_error(self):
|
|
msg = handle_error_global(RuntimeError("boom"))
|
|
assert "RuntimeError" in msg
|
|
assert "boom" in msg
|