"""Unit tests for SendGridEmailBackend — no Postgres needed."""
import pytest


def test_sendgrid_send_calls_api(monkeypatch):
    calls = []

    class FakeResponse:
        status_code = 202
        body = b""

    class FakeClient:
        def send(self, msg):
            calls.append(msg)
            return FakeResponse()

    monkeypatch.setattr("app.services.email.SendGridAPIClient", lambda _key: FakeClient())

    from app.services.email import SendGridEmailBackend
    backend = SendGridEmailBackend()
    backend.send(to="to@example.com", subject="Hello", html="<p>Hi</p>")

    assert len(calls) == 1


def test_sendgrid_raises_on_4xx(monkeypatch):
    class FakeResponse:
        status_code = 400
        body = b"bad request"

    class FakeClient:
        def send(self, msg):
            return FakeResponse()

    monkeypatch.setattr("app.services.email.SendGridAPIClient", lambda _key: FakeClient())

    from app.services.email import SendGridEmailBackend
    backend = SendGridEmailBackend()
    with pytest.raises(RuntimeError, match="400"):
        backend.send(to="to@example.com", subject="Hello", html="<p>Hi</p>")
