summaryrefslogtreecommitdiff
path: root/servers/gitlab_glab/tests/test_cli.py
blob: 316556ca5dba084dce0e86e5b703cfb1a33e69d2 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""Tests for the GitLab CLI MCP server CLI functionality."""

import argparse
import logging
from unittest.mock import MagicMock, patch

from mcp_server_gitlab_glab.cli import parse_args, setup_logging, validate_args


class TestCli:
    """Tests for the CLI functionality."""

    def test_parse_args_defaults(self) -> None:
        """Test parsing command-line arguments with defaults."""
        # Mock sys.argv
        with patch("sys.argv", ["mcp-gitlab-glab"]):
            args = parse_args()
            assert args.transport == "stdio"
            assert args.host == "127.0.0.1"
            assert args.port == 8080
            assert args.log_level == "INFO"

    def test_parse_args_custom(self) -> None:
        """Test parsing command-line arguments with custom values."""
        # Mock sys.argv
        with patch(
            "sys.argv",
            [
                "mcp-gitlab-glab",
                "--transport",
                "remote",
                "--host",
                "0.0.0.0",
                "--port",
                "9000",
                "--log-level",
                "DEBUG",
            ],
        ):
            args = parse_args()
            assert args.transport == "remote"
            assert args.host == "0.0.0.0"
            assert args.port == 9000
            assert args.log_level == "DEBUG"

    def test_validate_args_low_port_warning(self) -> None:
        """Test validation warning for low port on Unix-like systems."""
        # Create args with low port
        args = argparse.Namespace(transport="remote", port=80, host="0.0.0.0")

        # Mock logger and platform
        with patch("mcp_server_gitlab_glab.cli.logger") as mock_logger:
            with patch("sys.platform", "linux"):
                validate_args(args)
                mock_logger.warning.assert_called_once()

    def test_validate_args_no_warning_windows(self) -> None:
        """Test no validation warning for low port on Windows."""
        # Create args with low port
        args = argparse.Namespace(transport="remote", port=80, host="0.0.0.0")

        # Mock logger and platform
        with patch("mcp_server_gitlab_glab.cli.logger") as mock_logger:
            with patch("sys.platform", "win32"):
                validate_args(args)
                mock_logger.warning.assert_not_called()

    def test_validate_args_no_warning_high_port(self) -> None:
        """Test no validation warning for high port."""
        # Create args with high port
        args = argparse.Namespace(transport="remote", port=8080, host="0.0.0.0")

        # Mock logger
        with patch("mcp_server_gitlab_glab.cli.logger") as mock_logger:
            validate_args(args)
            mock_logger.warning.assert_not_called()

    def test_setup_logging_stdio(self) -> None:
        """Test logging setup for stdio transport."""
        # Mock os.makedirs and logging configuration
        with patch("os.makedirs") as mock_makedirs:
            with patch("logging.FileHandler") as mock_file_handler:
                with patch("logging.StreamHandler") as mock_stream_handler:
                    with patch("logging.getLogger") as mock_get_logger:
                        # Mock root logger
                        mock_root_logger = MagicMock()
                        mock_get_logger.return_value = mock_root_logger

                        # Mock handlers
                        mock_file_handler_instance = MagicMock()
                        mock_file_handler.return_value = mock_file_handler_instance
                        mock_stream_handler_instance = MagicMock()
                        mock_stream_handler.return_value = mock_stream_handler_instance

                        # Call setup_logging
                        setup_logging("INFO", "stdio")

                        # Verify logs directory creation
                        mock_makedirs.assert_called_once_with("logs", exist_ok=True)

                        # Verify file handler setup
                        mock_file_handler.assert_called_once_with("logs/mcp_server.log")
                        mock_file_handler_instance.setLevel.assert_called_once_with(
                            logging.INFO
                        )
                        mock_file_handler_instance.setFormatter.assert_called_once()

                        # Verify stream handler setup for stdio
                        mock_stream_handler_instance.setLevel.assert_called_once_with(
                            logging.WARNING
                        )
                        mock_stream_handler_instance.setFormatter.assert_called_once()

                        # Verify root logger setup
                        mock_root_logger.setLevel.assert_called_once_with(logging.INFO)
                        assert mock_root_logger.handlers == []
                        assert mock_root_logger.addHandler.call_count == 2

    def test_setup_logging_remote(self) -> None:
        """Test logging setup for remote transport."""
        # Mock os.makedirs and logging configuration
        with patch("os.makedirs") as mock_makedirs:
            with patch("logging.FileHandler") as mock_file_handler:
                with patch("logging.StreamHandler") as mock_stream_handler:
                    with patch("logging.getLogger") as mock_get_logger:
                        # Mock root logger
                        mock_root_logger = MagicMock()
                        mock_get_logger.return_value = mock_root_logger

                        # Mock handlers
                        mock_file_handler_instance = MagicMock()
                        mock_file_handler.return_value = mock_file_handler_instance
                        mock_stream_handler_instance = MagicMock()
                        mock_stream_handler.return_value = mock_stream_handler_instance

                        # Call setup_logging
                        setup_logging("DEBUG", "remote")

                        # Verify logs directory creation
                        mock_makedirs.assert_called_once_with("logs", exist_ok=True)

                        # Verify file handler setup
                        mock_file_handler.assert_called_once_with("logs/mcp_server.log")
                        mock_file_handler_instance.setLevel.assert_called_once_with(
                            logging.DEBUG
                        )
                        mock_file_handler_instance.setFormatter.assert_called_once()

                        # Verify stream handler setup for remote
                        mock_stream_handler_instance.setLevel.assert_called_once_with(
                            logging.DEBUG
                        )
                        mock_stream_handler_instance.setFormatter.assert_called_once()

                        # Verify root logger setup
                        mock_root_logger.setLevel.assert_called_once_with(logging.DEBUG)
                        assert mock_root_logger.handlers == []
                        assert mock_root_logger.addHandler.call_count == 2