summaryrefslogtreecommitdiff
path: root/tests/test_transports.py
blob: a8bf12fe89c69928490da7d160923d3ffcc9b352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Tests for MCP server transport CLI argument parsing."""

from unittest.mock import patch

import pytest

from mml_composemusic_mcp.server import main


def test_transport_stdio_default():
    """Default transport should be stdio without host/port."""
    with patch("sys.argv", ["server", "--output-dir", "./data"]):
        with patch("mml_composemusic_mcp.server.mcp.run") as mock_run:
            main()
            mock_run.assert_called_once()
            call_kwargs = mock_run.call_args
            assert call_kwargs[1]["transport"] == "stdio"
            assert "host" not in call_kwargs[1]
            assert "port" not in call_kwargs[1]


def test_transport_http():
    """HTTP transport should pass host and port."""
    with patch(
        "sys.argv",
        ["server", "--transport", "http", "--host", "0.0.0.0", "--port", "9000"],
    ):
        with patch("mml_composemusic_mcp.server.mcp.run") as mock_run:
            main()
            call_kwargs = mock_run.call_args
            assert call_kwargs[1]["transport"] == "http"
            assert call_kwargs[1]["host"] == "0.0.0.0"
            assert call_kwargs[1]["port"] == 9000


def test_transport_sse():
    """SSE transport should pass host and port."""
    with patch("sys.argv", ["server", "--transport", "sse", "--port", "8080"]):
        with patch("mml_composemusic_mcp.server.mcp.run") as mock_run:
            main()
            call_kwargs = mock_run.call_args
            assert call_kwargs[1]["transport"] == "sse"
            assert "host" in call_kwargs[1]
            assert call_kwargs[1]["port"] == 8080


def test_transport_streamable_http():
    """Streamable HTTP transport should pass host and port."""
    with patch("sys.argv", ["server", "--transport", "streamable-http"]):
        with patch("mml_composemusic_mcp.server.mcp.run") as mock_run:
            main()
            call_kwargs = mock_run.call_args
            assert call_kwargs[1]["transport"] == "streamable-http"
            assert "host" in call_kwargs[1]
            assert "port" in call_kwargs[1]


def test_transport_invalid_rejected():
    """Invalid transport should be rejected by argparse."""
    with patch("sys.argv", ["server", "--transport", "invalid"]):
        with pytest.raises(SystemExit):
            main()


def test_output_dir_override(tmp_path):
    """--output-dir should set OUTPUT_DIR."""
    with patch("sys.argv", ["server", "--output-dir", str(tmp_path / "out")]):
        with patch("mml_composemusic_mcp.server.mcp.run"):
            import mml_composemusic_mcp.server as srv

            main()
            assert srv.OUTPUT_DIR == tmp_path / "out"