test_read.py•5.59 kB
"""Tests for mcp_read tool."""
import pytest
from unittest.mock import AsyncMock, patch
class TestMcpReadModes:
"""Test different read modes."""
@pytest.mark.asyncio
async def test_read_invalid_mode(self):
"""Test invalid mode returns error."""
from mcp_copyq.tools.read import mcp_read
result = await mcp_read(mode="invalid_mode")
assert "error" in result
assert "INVALID_MODE" in result
@pytest.mark.asyncio
async def test_read_list_missing_tab(self):
"""Test list mode without tab returns error."""
from mcp_copyq.tools.read import mcp_read
result = await mcp_read(mode="list")
assert "error" in result
assert "MISSING_PARAM" in result
assert "tab" in result
@pytest.mark.asyncio
async def test_read_item_missing_index(self):
"""Test item mode without index returns error."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.tab_exists = AsyncMock(return_value=True)
result = await mcp_read(mode="item", tab="info")
assert "error" in result
assert "MISSING_PARAM" in result
assert "index" in result
@pytest.mark.asyncio
async def test_read_search_missing_query(self):
"""Test search mode without query returns error."""
from mcp_copyq.tools.read import mcp_read
result = await mcp_read(mode="search")
assert "error" in result
assert "MISSING_PARAM" in result
assert "query" in result
@pytest.mark.asyncio
async def test_read_list_tab_not_found(self):
"""Test list mode with non-existent tab."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.tab_exists = AsyncMock(return_value=False)
result = await mcp_read(mode="list", tab="nonexistent")
assert "error" in result
assert "TAB_NOT_FOUND" in result
class TestMcpReadList:
"""Test list mode functionality."""
@pytest.mark.asyncio
async def test_read_list_success(self):
"""Test successful list read."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.tab_exists = AsyncMock(return_value=True)
mock.get_count = AsyncMock(return_value=3)
mock.read_item_preview = AsyncMock(return_value={
"index": 0,
"text_preview": "Sample text",
"tags": ["tag1"],
"has_note": True,
})
result = await mcp_read(mode="list", tab="info", max_items=5)
assert "mode:list" in result
assert "tab:info" in result
assert "total:3" in result
@pytest.mark.asyncio
async def test_read_list_pagination(self):
"""Test list pagination with skip."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.tab_exists = AsyncMock(return_value=True)
mock.get_count = AsyncMock(return_value=10)
mock.read_item_preview = AsyncMock(return_value={
"index": 5,
"text_preview": "Item 5",
"tags": [],
"has_note": False,
})
result = await mcp_read(mode="list", tab="info", skip=5, max_items=3)
assert "showing:5-7" in result
class TestMcpReadItem:
"""Test item mode functionality."""
@pytest.mark.asyncio
async def test_read_item_success(self):
"""Test successful item read."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.tab_exists = AsyncMock(return_value=True)
mock.read_item_full = AsyncMock(return_value={
"index": 0,
"text": "Full text content",
"tags": ["tag1", "tag2"],
"note": "Note content",
})
result = await mcp_read(
mode="item",
tab="info",
index=0,
include_note=True
)
assert "mode:item" in result
assert "text:Full text content" in result
assert "tags:[tag1,tag2]" in result
assert "note:Note content" in result
class TestMcpReadSearch:
"""Test search mode functionality."""
@pytest.mark.asyncio
async def test_read_search_success(self):
"""Test successful search."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.search = AsyncMock(return_value=[
{
"tab": "info",
"index": 0,
"text_preview": "Found item",
"tags": ["tag1"],
"match_in": "text",
}
])
result = await mcp_read(mode="search", query="Found")
assert "mode:search" in result
assert "results:1" in result
assert "match:text" in result
@pytest.mark.asyncio
async def test_read_search_no_results(self):
"""Test search with no results."""
from mcp_copyq.tools.read import mcp_read
with patch("mcp_copyq.tools.read.client") as mock:
mock.search = AsyncMock(return_value=[])
result = await mcp_read(mode="search", query="notfound")
assert "results:0" in result