Files
procleaning-mcp/tests/test_server.py

91 lines
2.6 KiB
Python

"""Tests for procleaning-mcp server."""
import sys
import asyncio
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
import pytest
from procleaning_mcp.server import mcp
@pytest.fixture
def event_loop():
"""Create a test event loop."""
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.mark.asyncio
async def test_list_processes():
"""Test that list_processes returns valid output."""
result = await mcp.call_tool(
"list_processes", {"name_filter": "python", "max_processes": 5}
)
# call_tool returns (content_list, structured_content)
content_list, structured = result
assert isinstance(content_list, list)
assert content_list[0].type == "text"
text = content_list[0].text
assert "Found" in text or "No processes" in text
@pytest.mark.asyncio
async def test_system_resource_usage():
"""Test that system_resource_usage returns valid output."""
result = await mcp.call_tool(
"system_resource_usage", {"top_n": 3, "sort_by": "memory"}
)
content_list, structured = result
assert isinstance(content_list, list)
assert content_list[0].type == "text"
text = content_list[0].text
assert "CPU Usage" in text
assert "Memory Usage" in text
assert "Top 3" in text
@pytest.mark.asyncio
async def test_find_zombies():
"""Test that find_zombies returns valid output."""
result = await mcp.call_tool("find_zombies", {"include_details": False})
content_list, structured = result
assert isinstance(content_list, list)
assert content_list[0].type == "text"
@pytest.mark.asyncio
async def test_list_processes_empty_filter():
"""Test list_processes with no filter returns processes."""
result = await mcp.call_tool(
"list_processes", {"max_processes": 3}
)
content_list, structured = result
assert isinstance(content_list, list)
text = content_list[0].text
assert "Found" in text
@pytest.mark.asyncio
async def test_kill_process_no_confirm():
"""Test that kill_process without confirm returns cancellation message."""
result = await mcp.call_tool(
"kill_process", {"pid": 1, "confirm": False}
)
content_list, structured = result
text = content_list[0].text
assert "cancelled" in text.lower()
@pytest.mark.asyncio
async def test_kill_by_name_no_match():
"""Test kill_by_name with non-existent process name."""
result = await mcp.call_tool(
"kill_by_name", {"name_pattern": "thisprocessdoesnotexist12345", "confirm": False}
)
content_list, structured = result
text = content_list[0].text
assert "cancelled" in text.lower()