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_instructions_tell_agents_to_use_knowledge_base_proactively() -> None:
    assert mcp.instructions is not None
    assert "canonical knowledge base" in mcp.instructions
    assert "Use it proactively" in mcp.instructions
    assert "do not wait for the user" in mcp.instructions
    assert "Do not store raw passwords" in mcp.instructions


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"
