refactor(mail): address PR #935 round-1 review

- models/mail.py: lowercase `list` generics per CLAUDE.md convention.
- client/mail.py: _ocs_get now inspects ocs.meta.statuscode (re-raises >=400
  as HTTPStatusError carrying the OCS code so callers' 404/403 handling
  applies) and guards response.json() against non-JSON bodies (RequestError).
- Extract the duplicated _format_addresses + content reconstruction into
  vector/mail_content.py, used by both processor.py and context.py (fixes the
  SonarCloud new_duplicated_lines_density gate).
- processor.py: add the missing mail_message Qdrant payload block so the
  computed mail metadata (subject/from/to/cc/date_int/has_attachments/
  account_id/mailbox_id) is actually stored, not dropped.
- Rename the list_messages `filter` param to `search_filter` (avoid shadowing
  builtins.filter); still maps to the OCS `filter` query param.
- Docstring notes: has_more heuristic, attachment content size.
- Tests: OCS meta-failure + non-JSON client paths; initial-sync scanner tests
  (tests/unit/vector/test_scanner_mail.py).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-20 12:31:32 +02:00
co-authored by Claude Opus 4.8
parent 3074622455
commit 62ee3e9f32
8 changed files with 303 additions and 94 deletions
+38 -1
View File
@@ -108,7 +108,7 @@ async def test_list_messages_builds_params(mocker):
client = MailClient(mock_client, "testuser")
messages = await client.list_messages(
10, cursor=42, filter="hello", limit=50, view="threaded"
10, cursor=42, search_filter="hello", limit=50, view="threaded"
)
assert len(messages) == 1
@@ -204,3 +204,40 @@ async def test_empty_data_returns_empty_list(mocker):
client = MailClient(mock_client, "testuser")
assert await client.list_accounts() == []
async def test_ocs_meta_failure_raises_httpstatuserror(mocker):
"""HTTP 200 with an OCS meta failure code is re-raised as HTTPStatusError.
The synthetic response carries the OCS statuscode so callers' 404/403
handling applies (e.g. nc_mail_get_message maps 404 to 'not found').
"""
mock_response = create_mock_response(
status_code=200,
json_data={
"ocs": {
"meta": {"status": "failure", "statuscode": 404, "message": "nope"},
"data": None,
}
},
)
mock_client = mocker.AsyncMock(spec=httpx.AsyncClient)
mocker.patch.object(MailClient, "_make_request", return_value=mock_response)
client = MailClient(mock_client, "testuser")
with pytest.raises(httpx.HTTPStatusError) as excinfo:
await client.get_message(100)
assert excinfo.value.response.status_code == 404
async def test_non_json_response_raises_requesterror(mocker):
"""A non-JSON 200 body (e.g. Mail app absent) raises a RequestError."""
mock_response = create_mock_response(
status_code=200, content=b"<html>not found</html>"
)
mock_client = mocker.AsyncMock(spec=httpx.AsyncClient)
mocker.patch.object(MailClient, "_make_request", return_value=mock_response)
client = MailClient(mock_client, "testuser")
with pytest.raises(httpx.RequestError):
await client.list_accounts()
+123
View File
@@ -0,0 +1,123 @@
"""Unit tests for the mail-message scanner (initial-sync path).
The incremental path depends on live Qdrant lookups (``_scroll_all_points`` /
``query_document_metadata``); these tests cover the initial-sync enumeration —
accounts → mailboxes → newest-N messages — which is the bulk of the new logic
and needs no Qdrant.
"""
from unittest.mock import AsyncMock, MagicMock
import pytest
from nextcloud_mcp_server.vector import scanner as scanner_module
from nextcloud_mcp_server.vector.scanner import DocumentTask, scan_mail_messages
pytestmark = pytest.mark.unit
class _CollectingStream:
"""Minimal TaskProducer stand-in that records sent DocumentTasks."""
def __init__(self) -> None:
self.tasks: list[DocumentTask] = []
async def send(self, task: DocumentTask) -> None:
self.tasks.append(task)
async def test_initial_sync_enumerates_accounts_mailboxes_messages(mocker):
nc_client = MagicMock()
nc_client.mail.list_accounts = AsyncMock(return_value=[{"id": 1}])
nc_client.mail.get_mailboxes = AsyncMock(
return_value=[{"databaseId": 10}, {"databaseId": 11}]
)
async def list_messages(mailbox_id, *, limit):
if mailbox_id == 10:
return [
{"databaseId": 100, "dateInt": 1700000000},
{"databaseId": 101, "dateInt": 1700000001},
]
return [{"databaseId": 200, "dateInt": 1700000002}]
nc_client.mail.list_messages = AsyncMock(side_effect=list_messages)
placeholder = mocker.patch.object(
scanner_module, "write_placeholder_point", new=AsyncMock()
)
mocker.patch.object(scanner_module, "record_vector_sync_scan")
stream = _CollectingStream()
queued = await scan_mail_messages(
user_id="alice",
send_stream=stream,
nc_client=nc_client,
initial_sync=True,
scan_id=1,
)
assert queued == 3
assert len(stream.tasks) == 3
# All are mail_message index tasks carrying account/mailbox metadata.
assert {t.doc_id for t in stream.tasks} == {"100", "101", "200"}
assert all(t.doc_type == "mail_message" for t in stream.tasks)
assert all(t.operation == "index" for t in stream.tasks)
t100 = next(t for t in stream.tasks if t.doc_id == "100")
assert t100.modified_at == 1700000000
assert t100.metadata == {"account_id": 1, "mailbox_id": 10}
# A placeholder is written per message before queueing.
assert placeholder.await_count == 3
# The per-mailbox cap is passed through.
nc_client.mail.list_messages.assert_any_await(
10, limit=scanner_module.MAIL_SCAN_MAX_PER_MAILBOX
)
async def test_initial_sync_skips_mailbox_on_list_error(mocker):
"""A failing mailbox is logged and skipped; other mailboxes still index."""
nc_client = MagicMock()
nc_client.mail.list_accounts = AsyncMock(return_value=[{"id": 1}])
nc_client.mail.get_mailboxes = AsyncMock(
return_value=[{"databaseId": 10}, {"databaseId": 11}]
)
async def list_messages(mailbox_id, *, limit):
if mailbox_id == 10:
raise RuntimeError("imap hiccup")
return [{"databaseId": 200, "dateInt": 1700000002}]
nc_client.mail.list_messages = AsyncMock(side_effect=list_messages)
mocker.patch.object(scanner_module, "write_placeholder_point", new=AsyncMock())
mocker.patch.object(scanner_module, "record_vector_sync_scan")
stream = _CollectingStream()
queued = await scan_mail_messages(
user_id="alice",
send_stream=stream,
nc_client=nc_client,
initial_sync=True,
scan_id=1,
)
assert queued == 1
assert {t.doc_id for t in stream.tasks} == {"200"}
async def test_no_accounts_queues_nothing(mocker):
nc_client = MagicMock()
nc_client.mail.list_accounts = AsyncMock(return_value=[])
mocker.patch.object(scanner_module, "write_placeholder_point", new=AsyncMock())
mocker.patch.object(scanner_module, "record_vector_sync_scan")
stream = _CollectingStream()
queued = await scan_mail_messages(
user_id="alice",
send_stream=stream,
nc_client=nc_client,
initial_sync=True,
scan_id=1,
)
assert queued == 0
assert stream.tasks == []