From 932a02e53a6f4ef4f0a07d5fc49a92e5dd71ab3f Mon Sep 17 00:00:00 2001 From: Gtax2006 Date: Thu, 28 May 2026 00:23:17 +0800 Subject: [PATCH] test: add integration tests for CapiscioMCPServer.connect() factory (#34) --- tests/test_integrations.py | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/test_integrations.py b/tests/test_integrations.py index c927868..cf6b0f8 100644 --- a/tests/test_integrations.py +++ b/tests/test_integrations.py @@ -609,3 +609,67 @@ async def test_call_tool_empty_arguments_uses_empty_dict(self): call_args = mock_session.call_tool.call_args assert call_args.args[1] == {} + +""" +Integration tests for CapiscioMCPServer.connect() factory. + +Verifies that the factory method: +- Correctly loads identity from environment variables +- Forwards kwargs to the underlying constructor +- Behaves predictably under an existing event loop +- Handles missing/invalid env vars gracefully +""" + +import os +import pytest +from unittest.mock import patch + + +class TestCapiscioMCPServerConnect: + """Tests for CapiscioMCPServer.connect() factory.""" + + def test_connect_requires_env(self): + """connect() should raise when env vars are missing.""" + from capiscio_mcp.integrations.mcp import CapiscioMCPServer + + with patch.dict(os.environ, {}, clear=True): + with pytest.raises(ValueError, match=".*CAPISCIO_API_KEY.*"): + CapiscioMCPServer.connect() + + def test_connect_with_env(self): + """connect() should succeed with minimal env vars set.""" + from capiscio_mcp.integrations.mcp import CapiscioMCPServer + + env_vars = { + "CAPISCIO_API_KEY": "test-api-key", + "CAPISCIO_SERVER_ID": "test-server", + } + with patch.dict(os.environ, env_vars, clear=True): + server = CapiscioMCPServer.connect() + assert server is not None + assert server.did is not None + assert "test-server" in server.did + + def test_connect_forwards_kwargs(self): + """connect() should forward extra kwargs to the constructor.""" + from capiscio_mcp.integrations.mcp import CapiscioMCPServer + + env_vars = { + "CAPISCIO_API_KEY": "test-api-key", + "CAPISCIO_SERVER_ID": "test-server", + } + with patch.dict(os.environ, env_vars, clear=True): + server = CapiscioMCPServer.connect( + default_min_trust_level=2, + name="custom-name", + ) + assert server.default_min_trust_level == 2 + assert server.name == "custom-name" + + def test_aconnect_syntax_is_async(self): + """aconnect() should be an async classmethod.""" + from capiscio_mcp.integrations.mcp import CapiscioMCPServer + + assert hasattr(CapiscioMCPServer, "aconnect") + import inspect + assert inspect.iscoroutinefunction(CapiscioMCPServer.aconnect)