from __future__ import annotations

import asyncio

from app.mcp_server import (
    get_inventory_item,
    list_inventory,
    mcp,
    read_doc,
    search_docs,
    validate_repository_tool,
)


def test_mcp_lists_core_tools() -> None:
    tools = asyncio.run(mcp.list_tools())
    names = {tool.name for tool in tools}

    assert "search_docs" in names
    assert "read_doc" in names
    assert "list_inventory" in names
    assert "update_doc" in names
    assert "commit_changes" in names


def test_mcp_tool_functions_read_repository() -> None:
    doc = read_doc("10-systems/applications/gitbucket.md")
    assert doc["path"] == "10-systems/applications/gitbucket.md"
    assert "GitBucket" in doc["body"]

    results = search_docs("GitBucket")
    assert any(result["path"] == "10-systems/applications/gitbucket.md" for result in results)

    inventory_types = list_inventory()
    assert "services" in inventory_types

    service = get_inventory_item("services", "gitbucket")
    assert service["name"] == "GitBucket"


def test_mcp_validation_tool_is_clean() -> None:
    report = validate_repository_tool()
    assert report["status"] == "ok"
