Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

27.1 KB
Raw Blame
import base64
import json
import mimetypes
from email import policy
from email.message import EmailMessage
from email.parser import BytesParser
from typing import Any
from unittest.mock import Mock

import pytest

from xagent.web.tools.mcp import composio_proxy, gmail

_parse_message = BytesParser(policy=policy.default).parsebytes


@pytest.fixture(autouse=True)
def _credentials(monkeypatch):
    monkeypatch.setenv("GOOGLE_ACCESS_TOKEN", "access-token")
    monkeypatch.delenv("GOOGLE_REFRESH_TOKEN", raising=False)
    monkeypatch.delenv("GOOGLE_CLIENT_ID", raising=False)
    monkeypatch.delenv("GOOGLE_CLIENT_SECRET", raising=False)
    # This file is the native path; no Composio grant is ever launched here.
    for name in (
        composio_proxy.COMPOSIO_API_KEY_ENV,
        composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV,
        composio_proxy.COMPOSIO_USER_ID_ENV,
    ):
        monkeypatch.delenv(name, raising=False)


def _mock_gmail_service(monkeypatch, users_mock):
    service = Mock()
    service.users.return_value = users_mock
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)
    return service


def _sent_raw_message(users_mock):
    raw = users_mock.messages.return_value.send.call_args.kwargs["body"]["raw"]
    return _parse_message(base64.urlsafe_b64decode(raw))


def _drafted_raw_message(users_mock):
    raw = users_mock.drafts.return_value.create.call_args.kwargs["body"]["message"][
        "raw"
    ]
    return _parse_message(base64.urlsafe_b64decode(raw))


def test_send_messages_without_attachments_still_works(monkeypatch):
    users = Mock()
    users.messages.return_value.send.return_value.execute.return_value = {"id": "m1"}
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [{"to": "[email protected]", "subject": "Hi", "body": "hello"}],
            action="send",
        )
    )

    assert result["status"] == "success"
    assert result["results"] == [{"status": "sent", "id": "m1", "attachments": []}]


def test_send_messages_attaches_workspace_file(monkeypatch, tmp_path):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    pdf_path = tmp_path / "deck.pdf"
    pdf_path.write_bytes(b"%PDF-1.4 fake pdf bytes")

    users = Mock()
    users.messages.return_value.send.return_value.execute.return_value = {"id": "m1"}
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "Deck",
                    "body": "Please find attached.",
                    "attachments": [str(pdf_path)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "success"
    assert result["results"][0]["attachments"] == [
        {"filename": "deck.pdf", "size": len(b"%PDF-1.4 fake pdf bytes")}
    ]

    sent = _sent_raw_message(users)
    assert sent.is_multipart()
    attachment_parts = [p for p in sent.iter_attachments()]
    assert len(attachment_parts) == 1
    assert attachment_parts[0].get_filename() == "deck.pdf"
    assert attachment_parts[0].get_content_type() == "application/pdf"
    assert attachment_parts[0].get_payload(decode=True) == b"%PDF-1.4 fake pdf bytes"
    # A regression where add_attachment clobbered the body set via
    # message.set_content(...) would go undetected without this.
    assert "Please find attached." in sent.get_body(("plain",)).get_content()


def test_send_messages_preserves_cc_and_bcc_alongside_attachments(
    monkeypatch, tmp_path
):
    """add_attachment() converts a single-part message into a multipart one
    (via the stdlib's make_mixed()) — this must not drop headers set before
    the attachment was added."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    pdf_path = tmp_path / "deck.pdf"
    pdf_path.write_bytes(b"content")

    users = Mock()
    users.messages.return_value.send.return_value.execute.return_value = {"id": "m1"}
    _mock_gmail_service(monkeypatch, users)

    gmail.gmail_send_messages(
        [
            {
                "to": "[email protected]",
                "cc": "[email protected]",
                "bcc": "[email protected]",
                "subject": "Deck",
                "body": "B",
                "attachments": [str(pdf_path)],
            }
        ],
        action="send",
    )

    sent = _sent_raw_message(users)
    assert sent["Cc"] == "[email protected]"
    assert sent["Bcc"] == "[email protected]"
    assert len(list(sent.iter_attachments())) == 1


def test_send_messages_attaches_non_ascii_text_with_utf8_charset(monkeypatch, tmp_path):
    """Regression guard: a text-maintype attachment with no charset
    parameter defaults to us-ascii per RFC 2045, mojibaking non-ASCII UTF-8
    content — the message body gets utf-8 automatically via set_content,
    but add_attachment needs it spelled out explicitly."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    notes_path = tmp_path / "notes.txt"
    notes_path.write_text("café", encoding="utf-8")

    users = Mock()
    users.messages.return_value.send.return_value.execute.return_value = {"id": "m1"}
    _mock_gmail_service(monkeypatch, users)

    gmail.gmail_send_messages(
        [
            {
                "to": "[email protected]",
                "subject": "S",
                "body": "B",
                "attachments": [str(notes_path)],
            }
        ],
        action="send",
    )

    sent = _sent_raw_message(users)
    attachment = next(sent.iter_attachments())
    assert attachment.get_content_type() == "text/plain"
    assert attachment.get_content() == "café"


def test_draft_messages_also_supports_attachments(monkeypatch, tmp_path):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    file_path = tmp_path / "notes.txt"
    file_path.write_text("some notes")

    users = Mock()
    users.drafts.return_value.create.return_value.execute.return_value = {"id": "d1"}
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(file_path)],
                }
            ]
        )
    )

    assert result["status"] == "success"
    assert result["results"] == [
        {
            "status": "drafted",
            "id": "d1",
            "attachments": [{"filename": "notes.txt", "size": 10}],
        }
    ]
    drafted = _drafted_raw_message(users)
    assert [p.get_filename() for p in drafted.iter_attachments()] == ["notes.txt"]


def test_send_messages_rejects_attachment_outside_allowed_dirs(monkeypatch, tmp_path):
    outside_file = tmp_path / "outside.txt"
    outside_file.write_text("secret")
    allowed_dir = tmp_path / "workspace"
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(allowed_dir))

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(outside_file)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "outside the allowed" in result["message"]
    assert str(outside_file) not in result["message"]
    assert str(allowed_dir) not in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_rejects_missing_attachment(monkeypatch, tmp_path):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(tmp_path / "missing.pdf")],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "not found" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_rejects_empty_attachment(monkeypatch, tmp_path):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    empty_file = tmp_path / "empty.pdf"
    empty_file.write_bytes(b"")

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(empty_file)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "empty" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_rejects_oversized_attachments(monkeypatch, tmp_path):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    big_file = tmp_path / "big.bin"
    big_file.write_bytes(b"\x00")
    monkeypatch.setattr(gmail, "_MAX_ATTACHMENT_BYTES", 0)

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(big_file)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "budget" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_accumulates_attachment_sizes_correctly(monkeypatch, tmp_path):
    """Regression guard: a cap patched to fit exactly one of two attachments
    must reject on the *second* one, not the first — this distinguishes a
    correct running total (+=) from a bug that just overwrites it (=),
    which a single-attachment test can't tell apart."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    file_a = tmp_path / "a.bin"
    file_a.write_bytes(b"\x00" * 10)
    file_b = tmp_path / "b.bin"
    file_b.write_bytes(b"\x00" * 10)
    monkeypatch.setattr(gmail, "_MAX_ATTACHMENT_BYTES", 15)

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(file_a), str(file_b)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "budget" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_enforces_size_budget_across_whole_batch(monkeypatch, tmp_path):
    """Regression guard: the size cap is a budget shared across every
    message in the call, not a fresh allowance reset per message — two
    messages each individually under the cap but exceeding it combined
    must still be rejected."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    file_a = tmp_path / "a.bin"
    file_a.write_bytes(b"\x00" * 10)
    file_b = tmp_path / "b.bin"
    file_b.write_bytes(b"\x00" * 10)
    monkeypatch.setattr(gmail, "_MAX_ATTACHMENT_BYTES", 15)

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S1",
                    "body": "B1",
                    "attachments": [str(file_a)],
                },
                {
                    "to": "[email protected]",
                    "subject": "S2",
                    "body": "B2",
                    "attachments": [str(file_b)],
                },
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "budget" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_rejects_non_list_attachments(monkeypatch, tmp_path):
    """Regression guard: a bare string (a plausible LLM mistake instead of
    a one-element list) must be rejected with a clear error, not iterated
    character-by-character into a confusing "file not found: /" error."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": "/workspace/deck.pdf",
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "attachments" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_rejects_non_list_messages(monkeypatch):
    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            {"to": "[email protected]", "subject": "S", "body": "B"}  # not a list
        )
    )

    assert result["status"] == "error"
    assert "messages" in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_scrubs_os_error_detail_on_read_failure(monkeypatch, tmp_path):
    """Regression guard: str(OSError) can reveal more than the caller's own
    input (errno text, and — for a caller-supplied relative/short path —
    the fully resolved absolute host path), so the raw exception text must
    not reach the caller/LLM-facing message; only the caller's own
    attachment string (which they already know) may appear, the same way
    the empty-file/oversized-attachment errors already echo it back."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    unreadable = tmp_path / "unreadable.pdf"
    unreadable.write_bytes(b"content")

    def _boom(self, mode="rb"):
        raise OSError(13, "Permission denied", str(unreadable))

    monkeypatch.setattr(gmail.Path, "open", _boom)

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": [str(unreadable)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "Permission denied" not in result["message"]
    assert "Errno 13" not in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_scrubs_resolved_path_on_read_failure_for_relative_input(
    monkeypatch, tmp_path
):
    """Same as above, but from the angle that matters most: when the
    caller supplies a short/relative attachment string, the fully resolved
    absolute host path (which the caller never typed and reveals real
    filesystem layout) must not leak into the error either — only the
    caller's own original string may appear."""
    monkeypatch.chdir(tmp_path)
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    unreadable = tmp_path / "unreadable.pdf"
    unreadable.write_bytes(b"content")

    def _boom(self, mode="rb"):
        raise OSError(13, "Permission denied", str(unreadable))

    monkeypatch.setattr(gmail.Path, "open", _boom)

    users = Mock()
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S",
                    "body": "B",
                    "attachments": ["unreadable.pdf"],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert str(unreadable) not in result["message"]
    users.messages.return_value.send.assert_not_called()


def test_send_messages_validates_all_attachments_before_sending_any(
    monkeypatch, tmp_path
):
    """Regression guard for the production incident this feature closes:
    a batch with one good message and one bad attachment must fail entirely
    up front, rather than silently sending the good message and only then
    failing on the second — which previously looked to the caller like a
    fully-succeeded batch was actually half-sent with no way to tell."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    good_file = tmp_path / "good.pdf"
    good_file.write_bytes(b"real content")

    users = Mock()
    users.messages.return_value.send.return_value.execute.return_value = {"id": "m1"}
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "S1",
                    "body": "B1",
                    "attachments": [str(good_file)],
                },
                {
                    "to": "[email protected]",
                    "subject": "S2",
                    "body": "B2",
                    "attachments": [str(tmp_path / "missing.pdf")],
                },
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    users.messages.return_value.send.assert_not_called()


def test_send_messages_returns_error_payload_on_api_failure(monkeypatch):
    users = Mock()
    users.messages.return_value.send.return_value.execute.side_effect = RuntimeError(
        "boom"
    )
    _mock_gmail_service(monkeypatch, users)

    result = json.loads(
        gmail.gmail_send_messages(
            [{"to": "[email protected]", "subject": "S", "body": "B"}], action="send"
        )
    )

    assert result["status"] == "error"
    assert "boom" in result["message"]


def _native_send_reference(messages: list[dict], action: str) -> str:
    """``gmail_send_messages`` as it was before the proxy transport (d5c68369).

    Kept as the reference the native path is compared against: it resolves
    the attachments, builds and sends each message in turn, and returns the
    bare error on failure.
    """
    try:
        if not isinstance(messages, list):
            raise ValueError("'messages' must be a list of message dicts")

        resolved_attachments: list[list[tuple[str, bytes]]] = []
        budget_remaining = gmail._MAX_ATTACHMENT_BYTES
        for msg_data in messages:
            attachments, budget_remaining = gmail._resolve_message_attachments(
                msg_data, budget_remaining
            )
            resolved_attachments.append(attachments)

        service = gmail.get_gmail_service()
        results = []

        for msg_data, attachments in zip(messages, resolved_attachments):
            message = EmailMessage()
            message.set_content(msg_data.get("body", ""))
            message["To"] = msg_data.get("to", "")
            message["From"] = "me"
            message["Subject"] = msg_data.get("subject", "")

            if "cc" in msg_data:
                message["Cc"] = msg_data["cc"]
            if "bcc" in msg_data:
                message["Bcc"] = msg_data["bcc"]

            attached_summary: list[dict[str, Any]] = []
            for filename, data in attachments:
                guessed_type, _ = mimetypes.guess_type(filename)
                maintype, _, subtype = (
                    guessed_type or "application/octet-stream"
                ).partition("/")
                params = {"charset": "utf-8"} if maintype == "text" else {}
                message.add_attachment(
                    data,
                    maintype=maintype,
                    subtype=subtype or "octet-stream",
                    filename=filename,
                    params=params,
                )
                attached_summary.append({"filename": filename, "size": len(data)})

            encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode()
            create_message = {"raw": encoded_message}

            if action == "send":
                sent_message = (
                    service.users()
                    .messages()
                    .send(userId="me", body=create_message)
                    .execute()
                )
                results.append(
                    {
                        "status": "sent",
                        "id": sent_message["id"],
                        "attachments": attached_summary,
                    }
                )
            else:
                draft = (
                    service.users()
                    .drafts()
                    .create(userId="me", body={"message": create_message})
                    .execute()
                )
                results.append(
                    {
                        "status": "drafted",
                        "id": draft["id"],
                        "attachments": attached_summary,
                    }
                )

        return json.dumps({"status": "success", "results": results})
    except Exception as e:
        return json.dumps({"status": "error", "message": str(e)})


def _native_users(fail_at: int | None = None) -> Mock:
    """A native users resource whose n-th send/draft fails when asked."""
    users = Mock()
    calls = {"count": 0}

    def execute() -> dict[str, str]:
        calls["count"] += 1
        if calls["count"] == fail_at:
            raise RuntimeError("boom")
        return {"id": f"m{calls['count']}"}

    users.messages.return_value.send.return_value.execute.side_effect = execute
    users.drafts.return_value.create.return_value.execute.side_effect = execute
    return users


def _send_transcript(users: Mock) -> list[dict[str, Any]]:
    """What was actually built and sent, message by message."""
    entries: list[dict[str, Any]] = []
    endpoints = (
        ("send", users.messages.return_value.send, "body", "raw"),
        ("draft", users.drafts.return_value.create, "body", "message"),
    )
    for kind, endpoint, outer, inner in endpoints:
        for call in endpoint.call_args_list:
            payload = call.kwargs[outer]
            raw = payload["raw"] if inner == "raw" else payload[inner]["raw"]
            message = _parse_message(base64.urlsafe_b64decode(raw))
            body = message.get_body(preferencelist=("plain",))
            entries.append(
                {
                    "kind": kind,
                    "to": message["To"],
                    "subject": message["Subject"],
                    "cc": message["Cc"],
                    "bcc": message["Bcc"],
                    "body": body.get_content() if body is not None else "",
                    "attachments": [
                        part.get_filename() for part in message.iter_attachments()
                    ],
                }
            )
    return entries


def _run_native_send(
    monkeypatch,
    send,
    messages: list[dict],
    action: str,
    fail_at: int | None = None,
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
    users = _native_users(fail_at)
    service = Mock()
    service.users.return_value = users
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)
    return json.loads(send(messages, action)), _send_transcript(users)


@pytest.mark.parametrize(
    "scenario",
    ["single", "two", "second-send-fails", "second-build-fails", "draft"],
)
def test_native_send_matches_the_original_implementation(monkeypatch, scenario):
    """#3647 acceptance: with no Composio grant, the send tool behaves
    exactly as it did at d5c68369, including every error path."""
    messages: list[dict] = [
        {"to": "[email protected]", "subject": "One", "body": "first"},
        {
            "to": "[email protected]",
            "subject": "Two",
            "body": "second",
            "cc": "[email protected]",
        },
    ]
    action = "send"
    fail_at: int | None = None
    if scenario == "single":
        messages = messages[:1]
    elif scenario == "second-send-fails":
        fail_at = 2
    elif scenario == "second-build-fails":
        messages[1]["subject"] = "bad\nsubject"
    elif scenario == "draft":
        action = "draft"

    expected, expected_transcript = _run_native_send(
        monkeypatch, _native_send_reference, messages, action, fail_at
    )
    actual, actual_transcript = _run_native_send(
        monkeypatch, gmail.gmail_send_messages, messages, action, fail_at
    )

    assert actual == expected
    assert actual_transcript == expected_transcript


def test_a_native_send_that_fails_midway_keeps_the_old_bare_error(monkeypatch):
    """The partial-results payload is Composio-only: native keeps the bare
    error even after the first message went out."""
    users = _native_users(fail_at=2)
    service = Mock()
    service.users.return_value = users
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {"to": "[email protected]", "subject": "One", "body": "1"},
                {"to": "[email protected]", "subject": "Two", "body": "2"},
            ],
            action="send",
        )
    )

    assert result == {"status": "error", "message": "boom"}
    assert "results" not in result
    # The second send was attempted (and failed) after the first went out.
    assert users.messages.return_value.send.return_value.execute.call_count == 2


def test_a_native_send_does_not_pre_build_the_batch(monkeypatch):
    """A message that cannot be built must not hold back the one before it:
    the native path builds and sends each message in turn, as before."""
    users = _native_users()
    service = Mock()
    service.users.return_value = users
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {"to": "[email protected]", "subject": "One", "body": "1"},
                {"to": "[email protected]", "subject": "bad\nsubject", "body": "2"},
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "results" not in result
    assert users.messages.return_value.send.call_count == 1


def test_a_native_send_does_not_run_the_proxy_pre_send_check(monkeypatch):
    """The body-size pre-check is Composio-only; a native request was never
    checked through the proxy transport."""
    checked: list[Any] = []
    users = _native_users()
    service = Mock()
    service.users.return_value = users
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)
    monkeypatch.setattr(
        gmail.google_api_proxy,
        "precheck_body_size",
        lambda request: checked.append(request),
    )

    gmail.gmail_send_messages(
        [{"to": "[email protected]", "subject": "One", "body": "1"}], action="send"
    )

    assert checked == []