import pytest
from starlette.middleware.cors import CORSMiddleware
from ckan_mcp.main import CKANMCPServer, create_http_app
def test_create_streamable_http_app_requires_leading_slash():
server = CKANMCPServer()
with pytest.raises(ValueError):
server.create_streamable_http_app(mount_path="mcp")
def test_create_streamable_http_app_is_singleton():
server = CKANMCPServer()
app_one = server.create_streamable_http_app(mount_path="/mcp")
app_two = server.create_streamable_http_app(mount_path="/mcp")
assert app_one is app_two
def test_create_http_app_respects_env(monkeypatch):
monkeypatch.setenv("CKAN_MCP_HTTP_ALLOW_ORIGINS", "https://example.com,https://data.local")
monkeypatch.setenv("CKAN_MCP_HTTP_JSON_RESPONSE", "true")
monkeypatch.setenv("CKAN_MCP_HTTP_PATH", "/custom")
app = create_http_app()
assert isinstance(app, CORSMiddleware)
# Ensure the Starlette app was mounted at the custom path and contains MCP server state
inner_app = app.app
assert getattr(inner_app.state, "ckan_server", None) is not None
assert getattr(inner_app.state, "session_manager", None) is not None
assert inner_app.routes[0].path == "/custom"