Skip to content

Automate browser based workflows with AI

29.0 KB
Raw Blame
from datetime import datetime, timezone
from types import SimpleNamespace
from unittest.mock import AsyncMock

import httpx
import pytest
import structlog.testing

from skyvern.forge import agent_functions
from skyvern.forge.agent_functions import AgentFunction
from skyvern.forge.sdk.schemas.totp_codes import OTPType
from skyvern.forge.sdk.services import google_gmail_service, google_oauth_service
from skyvern.services import otp_email, otp_service
from skyvern.services.otp_email import MAX_SEEN_EMAIL_MESSAGE_IDS, EmailOTPVerificationContext


def test_email_otp_source_context_caps_seen_message_ids() -> None:
    context = EmailOTPVerificationContext().for_source("gmail")

    for index in range(MAX_SEEN_EMAIL_MESSAGE_IDS + 5):
        context.remember_message("goac_1", f"msg_{index}")

    assert len(context.seen_message_keys) == MAX_SEEN_EMAIL_MESSAGE_IDS
    assert not context.has_seen_message("goac_1", "msg_0")
    assert context.has_seen_message("goac_1", f"msg_{MAX_SEEN_EMAIL_MESSAGE_IDS + 4}")


@pytest.mark.asyncio
async def test_gmail_source_filters_seen_candidates_for_current_credential(monkeypatch: pytest.MonkeyPatch) -> None:
    source = otp_email.GmailOTPSource(AsyncMock(return_value=SimpleNamespace(token="AT")))
    candidates = [
        google_gmail_service.GmailMessageCandidate(
            message_id="seen",
            content="Verification code 111111",
            internal_date=datetime.now(timezone.utc),
        ),
        google_gmail_service.GmailMessageCandidate(
            message_id="unseen",
            content="Verification code 222222",
            internal_date=datetime.now(timezone.utc),
        ),
    ]
    search_messages = AsyncMock(return_value=candidates)
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    context = EmailOTPVerificationContext().for_source("gmail")
    context.remember_message("goac_1", "seen")
    context.remember_message("different_credential", "unseen")

    async with httpx.AsyncClient() as client:
        results = await source.search_recent_otp_messages(
            organization_id="org_1",
            credential_id="goac_1",
            totp_identifier="[email protected]",
            created_after=None,
            max_results=5,
            context=context,
            client=client,
        )

    assert [candidate.message_id for candidate in results] == ["unseen"]
    assert search_messages.await_args is not None
    assert search_messages.await_args.kwargs["excluded_message_ids"] == {"seen"}


@pytest.mark.asyncio
async def test_gmail_source_withholds_hydration_failed_candidate_then_degrades(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = otp_email.GmailOTPSource(AsyncMock(return_value=SimpleNamespace(token="AT")))
    flagged = google_gmail_service.GmailMessageCandidate(
        message_id="msg_flaky",
        content="Subject: Sign in\nSnippet: Continue signing in",
        internal_date=datetime.now(timezone.utc),
        hydration_failed=True,
    )
    monkeypatch.setattr(
        otp_email.google_gmail_service,
        "search_recent_otp_messages",
        AsyncMock(return_value=[flagged]),
    )
    context = EmailOTPVerificationContext().for_source("gmail")

    async def search() -> list[otp_email.EmailOTPCandidate]:
        async with httpx.AsyncClient() as client:
            return await source.search_recent_otp_messages(
                organization_id="org_1",
                credential_id="goac_1",
                totp_identifier="[email protected]",
                created_after=None,
                max_results=5,
                context=context,
                client=client,
            )

    withheld_rounds = [await search() for _ in range(otp_email.MAX_HYDRATION_FAILURE_WITHHOLDS)]
    degraded = await search()

    assert withheld_rounds == [[], []]
    assert [candidate.message_id for candidate in degraded] == ["msg_flaky"]


@pytest.mark.asyncio
async def test_get_otp_value_from_email_uses_gmail_scoped_credentials(monkeypatch: pytest.MonkeyPatch) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    list_credentials = AsyncMock(return_value=[credential])
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", list_credentials)

    get_credentials = AsyncMock(return_value=SimpleNamespace(token="AT"))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", get_credentials)
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )

    candidate = google_gmail_service.GmailMessageCandidate(
        message_id="msg_1",
        content="Your verification code is 123456",
        internal_date=datetime.now(timezone.utc),
    )
    search_messages = AsyncMock(return_value=[candidate])
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    parse = AsyncMock(return_value=otp_service.OTPValue(value="123456", type=OTPType.TOTP))
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)

    result = await agent.get_otp_value_from_email(
        organization_id="org_1",
        totp_identifier="[email protected]",
        workflow_id="w_1",
        workflow_run_id="wr_1",
        created_after=datetime(2025, 1, 1, tzinfo=timezone.utc),
        expected_otp_type=OTPType.TOTP,
    )

    assert result == otp_service.OTPValue(value="123456", type=OTPType.TOTP)
    get_credentials.assert_awaited_once_with(
        organization_id="org_1",
        credential_id="goac_1",
        required_scopes=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    search_messages_args = search_messages.await_args
    assert search_messages_args is not None
    assert search_messages_args.kwargs["max_results"] == agent_functions.EMAIL_OTP_MAX_RESULTS
    assert search_messages_args.kwargs["client"] is not None
    parse.assert_awaited_once_with(
        "Your verification code is 123456",
        "org_1",
        enforced_otp_type=OTPType.TOTP,
    )
    create_otp_code.assert_awaited_once_with(
        "org_1",
        "[email protected]",
        "123456",
        "123456",
        OTPType.TOTP,
        workflow_id="w_1",
        workflow_run_id="wr_1",
        source="gmail",
    )


@pytest.mark.asyncio
async def test_get_otp_value_from_email_skips_mismatched_expected_type(monkeypatch: pytest.MonkeyPatch) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )
    candidate = google_gmail_service.GmailMessageCandidate(
        message_id="msg_code",
        content="Your sign-in code is 123456",
        internal_date=datetime.now(timezone.utc),
    )
    monkeypatch.setattr(
        otp_email.google_gmail_service,
        "search_recent_otp_messages",
        AsyncMock(return_value=[candidate]),
    )
    parse = AsyncMock(return_value=otp_service.OTPValue(value="123456", type=OTPType.TOTP))
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)
    context = EmailOTPVerificationContext()

    result = await agent.get_otp_value_from_email(
        organization_id="org_1",
        totp_identifier="[email protected]",
        expected_otp_type=OTPType.MAGIC_LINK,
        context=context,
    )

    assert result is None
    parse.assert_awaited_once_with(
        "Your sign-in code is 123456",
        "org_1",
        enforced_otp_type=OTPType.MAGIC_LINK,
    )
    create_otp_code.assert_not_awaited()
    assert context.for_source("gmail").has_seen_message("goac_1", "msg_code")


@pytest.mark.asyncio
async def test_get_otp_value_from_email_keeps_scanning_after_mismatched_candidate(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )
    now = datetime.now(timezone.utc)
    candidates = [
        google_gmail_service.GmailMessageCandidate(
            message_id="msg_code",
            content="Your sign-in code is 123456",
            internal_date=now,
        ),
        google_gmail_service.GmailMessageCandidate(
            message_id="msg_link",
            content="Use this link to sign in: https://auth.example.test/magic",
            internal_date=now,
        ),
    ]
    monkeypatch.setattr(
        otp_email.google_gmail_service,
        "search_recent_otp_messages",
        AsyncMock(return_value=candidates),
    )
    magic_link = otp_service.OTPValue(value="https://auth.example.test/magic", type=OTPType.MAGIC_LINK)
    parse = AsyncMock(side_effect=[otp_service.OTPValue(value="123456", type=OTPType.TOTP), magic_link])
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)
    context = EmailOTPVerificationContext()

    result = await agent.get_otp_value_from_email(
        organization_id="org_1",
        totp_identifier="[email protected]",
        expected_otp_type=OTPType.MAGIC_LINK,
        context=context,
    )

    assert result == magic_link
    assert parse.await_count == 2
    assert parse.await_args_list[0].args == ("Your sign-in code is 123456", "org_1")
    assert parse.await_args_list[0].kwargs == {"enforced_otp_type": OTPType.MAGIC_LINK}
    create_otp_code.assert_awaited_once()
    assert create_otp_code.await_args is not None
    assert create_otp_code.await_args.args[2] == magic_link.value
    assert context.for_source("gmail").has_seen_message("goac_1", "msg_code")


@pytest.mark.asyncio
async def test_get_otp_value_from_email_uses_first_parseable_gmail_candidate(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )

    candidates = [
        google_gmail_service.GmailMessageCandidate(
            message_id="msg_unrelated",
            content="Security alert with no code",
            internal_date=datetime.now(timezone.utc),
        ),
        google_gmail_service.GmailMessageCandidate(
            message_id="msg_code",
            content="Your verification code is 654321",
            internal_date=datetime.now(timezone.utc),
        ),
    ]
    monkeypatch.setattr(
        otp_email.google_gmail_service,
        "search_recent_otp_messages",
        AsyncMock(return_value=candidates),
    )
    parse = AsyncMock(side_effect=[None, otp_service.OTPValue(value="654321", type=OTPType.TOTP)])
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)

    result = await agent.get_otp_value_from_email(
        organization_id="org_1",
        totp_identifier="[email protected]",
        workflow_id="w_1",
        workflow_run_id="wr_1",
    )

    assert result == otp_service.OTPValue(value="654321", type=OTPType.TOTP)
    assert [call.args[0] for call in parse.await_args_list] == [
        "Security alert with no code",
        "Your verification code is 654321",
    ]
    create_otp_code.assert_awaited_once_with(
        "org_1",
        "[email protected]",
        "654321",
        "654321",
        OTPType.TOTP,
        workflow_id="w_1",
        workflow_run_id="wr_1",
        source="gmail",
    )


@pytest.mark.asyncio
async def test_get_otp_value_from_email_retries_gmail_candidate_after_parser_error(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )

    candidate = google_gmail_service.GmailMessageCandidate(
        message_id="msg_retry",
        content="Your verification code is 112233",
        internal_date=datetime.now(timezone.utc),
    )
    search_messages = AsyncMock(return_value=[candidate])
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    parse = AsyncMock(
        side_effect=[RuntimeError("temporary parser outage"), otp_service.OTPValue(value="112233", type=OTPType.TOTP)]
    )
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)

    context = EmailOTPVerificationContext()
    with structlog.testing.capture_logs() as logs:
        first_result = await agent.get_otp_value_from_email(
            organization_id="org_1",
            totp_identifier="[email protected]",
            workflow_id="w_1",
            workflow_run_id="wr_1",
            context=context,
        )
        context.for_source("gmail").last_searched_at_by_credential["goac_1"] = datetime(2025, 1, 1, tzinfo=timezone.utc)
        second_result = await agent.get_otp_value_from_email(
            organization_id="org_1",
            totp_identifier="[email protected]",
            workflow_id="w_1",
            workflow_run_id="wr_1",
            context=context,
        )

    assert first_result is None
    assert second_result == otp_service.OTPValue(value="112233", type=OTPType.TOTP)
    assert [call.args[0] for call in parse.await_args_list] == [candidate.content, candidate.content]
    create_otp_code.assert_awaited_once()
    assert any(
        record.get("event") == "Failed to parse email OTP candidate" and record.get("source") == "gmail"
        for record in logs
    )


@pytest.mark.asyncio
async def test_get_otp_value_from_email_rechecks_unseen_gmail_candidate_after_credit_skip(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    monkeypatch.setattr(
        agent,
        "get_google_workspace_credentials",
        AsyncMock(return_value=SimpleNamespace(token=object())),
    )
    create_otp_code = AsyncMock()
    monkeypatch.setattr(
        agent_functions.app, "DATABASE", SimpleNamespace(otp=SimpleNamespace(create_otp_code=create_otp_code))
    )
    candidates = [
        google_gmail_service.GmailMessageCandidate(
            message_id=f"msg_credit_skipped_{index}",
            content=f"Long relayed authentication message {index}.",
            internal_date=datetime.now(timezone.utc),
        )
        for index in range(2)
    ]
    monkeypatch.setattr(
        otp_email.google_gmail_service,
        "search_recent_otp_messages",
        AsyncMock(return_value=candidates),
    )
    parsed = otp_service.OTPValue(value="placeholder", type=OTPType.TOTP)
    parse = AsyncMock(side_effect=[otp_service.InsufficientCreditsForOTPParse, parsed])
    monkeypatch.setattr(otp_service, "parse_otp_login", parse)
    context = EmailOTPVerificationContext()

    with structlog.testing.capture_logs() as logs:
        first_result = await agent.get_otp_value_from_email(
            organization_id="org_1",
            totp_identifier="[email protected]",
            context=context,
        )
        context.for_source("gmail").last_searched_at_by_credential["goac_1"] = datetime(2025, 1, 1, tzinfo=timezone.utc)
        second_result = await agent.get_otp_value_from_email(
            organization_id="org_1",
            totp_identifier="[email protected]",
            context=context,
        )

    assert first_result is None
    assert second_result == parsed
    assert parse.await_args_list[0].args[0] == candidates[0].content
    assert parse.await_args_list[1].args[0] == candidates[1].content
    create_otp_code.assert_awaited_once()
    gmail_context = context.for_source("gmail")
    assert gmail_context.has_seen_message("goac_1", candidates[0].message_id)
    assert gmail_context.has_seen_message("goac_1", candidates[1].message_id)
    assert all(
        record.get("event") != "Failed to parse email OTP candidate" or record.get("source") != "gmail"
        for record in logs
    )


@pytest.mark.asyncio
async def test_get_otp_value_from_email_skips_credentials_without_gmail_scope(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_SHEETS_SCOPES),
    )
    monkeypatch.setattr(
        otp_email.google_oauth_service,
        "get_credentials_for_org",
        AsyncMock(return_value=[credential]),
    )
    get_credentials = AsyncMock(return_value=SimpleNamespace(token="AT"))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", get_credentials)

    result = await agent.get_otp_value_from_email(
        organization_id="org_1",
        totp_identifier="[email protected]",
    )

    assert result is None
    get_credentials.assert_not_awaited()


@pytest.mark.asyncio
async def test_get_otp_value_from_email_throttles_gmail_searches_within_polling_context(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(
        id="goac_1",
        scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES),
    )
    list_credentials = AsyncMock(return_value=[credential])
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", list_credentials)
    get_credentials = AsyncMock(return_value=SimpleNamespace(token="AT"))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", get_credentials)
    search_messages = AsyncMock(return_value=[])
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()

    with structlog.testing.capture_logs() as logs:
        for _ in range(2):
            result = await agent.get_otp_value_from_email(
                organization_id="org_1",
                totp_identifier="[email protected]",
                context=context,
            )
            assert result is None

    list_credentials.assert_awaited_once_with("org_1")
    get_credentials.assert_awaited_once()
    search_messages.assert_awaited_once()
    assert all(log.get("event") != "Unexpected email OTP lookup failure" for log in logs)


@pytest.mark.asyncio
async def test_failed_gmail_search_is_recorded_then_cleared_by_a_successful_one(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(id="goac_1", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    search_messages = AsyncMock(
        side_effect=[google_gmail_service.GmailAPIError(status=503, code=None, message="unavailable"), []]
    )
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)

    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )
    assert source_context.failed_credential_ids == {"goac_1"}

    source_context.last_searched_at_by_credential.clear()
    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )
    assert source_context.failed_credential_ids == set()
    assert source_context.completed_credential_ids == {"goac_1"}


@pytest.mark.asyncio
async def test_refreshed_credential_list_drops_a_failure_for_a_disconnected_inbox(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(id="goac_current", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", AsyncMock(return_value=[]))
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)
    source_context.failed_credential_ids.add("goac_disconnected")

    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )

    assert source_context.credential_ids == ["goac_current"]
    assert source_context.failed_credential_ids == set()


@pytest.mark.asyncio
async def test_unparseable_gmail_candidate_is_counted_without_being_marked_seen(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(id="goac_1", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    candidate = google_gmail_service.GmailMessageCandidate(
        message_id="msg_unreadable",
        content="Your verification code is 123456",
        internal_date=datetime.now(timezone.utc),
    )
    monkeypatch.setattr(
        otp_email.google_gmail_service, "search_recent_otp_messages", AsyncMock(return_value=[candidate])
    )
    monkeypatch.setattr(otp_service, "parse_otp_login", AsyncMock(side_effect=RuntimeError("parser down")))
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)

    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )

    assert source_context.unreadable_message_keys == {("goac_1", "msg_unreadable")}
    assert not source_context.has_seen_message("goac_1", "msg_unreadable")


@pytest.mark.asyncio
async def test_unusable_gmail_token_counts_as_a_failed_search(monkeypatch: pytest.MonkeyPatch) -> None:
    agent = AgentFunction()
    credential = SimpleNamespace(id="goac_1", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=None))
    search_messages = AsyncMock(return_value=[])
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", search_messages)
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)

    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )

    search_messages.assert_not_awaited()
    assert source_context.failed_credential_ids == {"goac_1"}
    assert source_context.completed_credential_ids == set()


@pytest.mark.asyncio
async def test_parse_cancelled_by_the_budget_still_marks_the_message_it_reached(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    import asyncio

    agent = AgentFunction()
    credential = SimpleNamespace(id="goac_1", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    candidate = google_gmail_service.GmailMessageCandidate(
        message_id="msg_in_flight",
        content="Your verification code is 123456",
        internal_date=datetime.now(timezone.utc),
    )
    monkeypatch.setattr(
        otp_email.google_gmail_service, "search_recent_otp_messages", AsyncMock(return_value=[candidate])
    )
    monkeypatch.setattr(otp_service, "parse_otp_login", AsyncMock(side_effect=asyncio.CancelledError()))
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)

    with pytest.raises(asyncio.CancelledError):
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )

    assert source_context.unreadable_message_keys == {("goac_1", "msg_in_flight")}
    assert not source_context.has_seen_message("goac_1", "msg_in_flight")


@pytest.mark.asyncio
async def test_a_credential_list_refresh_that_never_returns_leaves_the_list_marked_stale(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    import asyncio

    agent = AgentFunction()
    monkeypatch.setattr(agent, "get_google_workspace_credentials", AsyncMock(return_value=SimpleNamespace(token="AT")))
    monkeypatch.setattr(
        agent_functions,
        "build_email_otp_sources",
        lambda current_agent: [otp_email.GmailOTPSource(current_agent.get_google_workspace_credentials)],
    )

    context = EmailOTPVerificationContext()
    source_context = context.for_source(otp_email.GmailOTPSource.name)
    source_context.credential_ids = []
    source_context.credential_ids_loaded_at = datetime(2020, 1, 1, tzinfo=timezone.utc)

    monkeypatch.setattr(
        otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(side_effect=asyncio.CancelledError())
    )
    with pytest.raises(asyncio.CancelledError):
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
    assert source_context.credential_list_refresh_failed is True

    credential = SimpleNamespace(id="goac_1", scopes_granted=list(google_oauth_service.GOOGLE_GMAIL_SCOPES))
    monkeypatch.setattr(otp_email.google_oauth_service, "get_credentials_for_org", AsyncMock(return_value=[credential]))
    monkeypatch.setattr(otp_email.google_gmail_service, "search_recent_otp_messages", AsyncMock(return_value=[]))
    assert (
        await agent.get_otp_value_from_email(
            organization_id="org_1", totp_identifier="[email protected]", context=context
        )
        is None
    )
    assert source_context.credential_list_refresh_failed is False