"""Tests for the config module."""

import json
from pathlib import Path

from mcp_diet.config import parse_config


class TestParseConfig:
    def test_mcp_json_format(self, tmp_path: Path):
        config = {
            "mcpServers": {
                "filesystem": {
                    "command": "npx",
                    "args": ["@modelcontextprotocol/server-filesystem", "/tmp"],
                },
                "github": {
                    "command": "npx",
                    "args": ["@modelcontextprotocol/server-github"],
                },
            }
        }
        path = tmp_path / ".mcp.json"
        path.write_text(json.dumps(config))
        servers = parse_config(path)

        assert len(servers) == 2
        assert servers[0].name == "filesystem"
        assert servers[0].command == "npx"
        assert servers[0].args == ["@modelcontextprotocol/server-filesystem", "/tmp"]
        assert servers[1].name == "github"

    def test_claude_settings_format(self, tmp_path: Path):
        config = {
            "mcpServers": {
                "myserver": {
                    "command": "python",
                    "args": ["server.py"],
                }
            }
        }
        path = tmp_path / "settings.json"
        path.write_text(json.dumps(config))
        servers = parse_config(path)

        assert len(servers) == 1
        assert servers[0].name == "myserver"
        assert servers[0].command == "python"

    def test_empty_servers(self, tmp_path: Path):
        config = {"mcpServers": {}}
        path = tmp_path / ".mcp.json"
        path.write_text(json.dumps(config))
        servers = parse_config(path)

        assert len(servers) == 0

    def test_missing_command_skipped(self, tmp_path: Path):
        config = {
            "mcpServers": {
                "broken": {"args": ["something"]},
                "valid": {"command": "python", "args": []},
            }
        }
        path = tmp_path / ".mcp.json"
        path.write_text(json.dumps(config))
        servers = parse_config(path)

        assert len(servers) == 1
        assert servers[0].name == "valid"

    def test_mcp_servers_snake_case_key(self, tmp_path: Path):
        config = {
            "mcp_servers": {
                "test": {"command": "node", "args": ["index.js"]},
            }
        }
        path = tmp_path / "settings.json"
        path.write_text(json.dumps(config))
        servers = parse_config(path)

        assert len(servers) == 1
        assert servers[0].command == "node"
