fix(scouts): fetch single Gmail message instead of bulk in fetch_content
Replace bulk GmailClient.fetch_messages() + linear search with a direct service.users().messages().get(format="full") call. Adds _extract_plain_text_body helper for recursive MIME part walking. Update test to patch _get_gmail_service. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -53,16 +52,24 @@ async def test_fetch_metadata_returns_subject_and_snippet():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_content_returns_body_text():
|
||||
import base64
|
||||
scout = _make_scout()
|
||||
conn = GmailConnector()
|
||||
# decrypt_token is patched because the test doesn't set OAUTH_ENCRYPTION_KEY.
|
||||
with patch("app.scouts.connectors.gmail.decrypt_token", return_value={}), \
|
||||
patch("app.scouts.connectors.gmail.GmailClient") as MockClient:
|
||||
instance = MockClient.return_value
|
||||
instance.fetch_messages = AsyncMock(return_value=[
|
||||
MagicMock(id="msg-1", subject="S", sender="a@b", body_text="hello world",
|
||||
date=datetime.now(tz=timezone.utc), labels=[]),
|
||||
])
|
||||
body_data = base64.urlsafe_b64encode(b"hello world").decode()
|
||||
fake_message = {
|
||||
"id": "msg-1",
|
||||
"snippet": "hello world",
|
||||
"payload": {
|
||||
"mimeType": "text/plain",
|
||||
"headers": [
|
||||
{"name": "Subject", "value": "S"},
|
||||
{"name": "From", "value": "a@b"},
|
||||
],
|
||||
"body": {"data": body_data},
|
||||
},
|
||||
}
|
||||
with patch("app.scouts.connectors.gmail._get_gmail_service") as mock_svc:
|
||||
mock_svc.return_value.users().messages().get().execute.return_value = fake_message
|
||||
content = await conn.fetch_content(scout, ItemRef(source_msg_ref="msg-1"))
|
||||
assert content.body_text == "hello world"
|
||||
assert content.metadata.subject == "S"
|
||||
|
||||
Reference in New Issue
Block a user