import pytest from app.services.human_handoff_service import ( MESSAGE_SCOPE_EXTERNAL, MESSAGE_SCOPE_HANDOFF, _body_has_quote_trail, _mirror_from_identity, build_handoff_subject, maybe_trigger_handoff, process_human_reply, redact_human_address, resolve_handoff_from_inbound, select_matching_handoff_rule, ) def test_scope_constants_are_distinct(): assert MESSAGE_SCOPE_EXTERNAL != MESSAGE_SCOPE_HANDOFF def test_human_handoff_runtime_is_enabled(): assert callable(maybe_trigger_handoff) assert callable(process_human_reply) assert callable(resolve_handoff_from_inbound) @pytest.mark.asyncio async def test_mirror_body_appends_external_history_when_missing(monkeypatch): from types import SimpleNamespace from unittest.mock import AsyncMock from app.services.human_handoff_service import _mirror_body_with_trail parent = { "_id": "parent", "sender": {"name": "Parth Dixit", "address": "parthdixit091@gmail.com"}, "body_text": "sender: C\n\nOn earlier Suyash Dixit wrote:\n> responder: B", "body_html": None, "sent_at": None, } monkeypatch.setattr( "app.services.human_handoff_service._latest_thread_message", AsyncMock(return_value=parent), ) monkeypatch.setattr( "app.services.human_handoff_service.load_mirror_payload", AsyncMock(return_value=SimpleNamespace(body_text=parent["body_text"], body_html=None)), ) text, html = await _mirror_body_with_trail( SimpleNamespace(), account_id="account-1", handoff={"external_thread_id": "thread"}, source={"_id": "child", "new_reply_content": "responder: D", "new_reply_html": None}, payload=SimpleNamespace(body_text="responder: D", body_html=None), ) assert text.startswith("responder: D") assert "parthdixit091@gmail.com" in text assert "> sender: C" in text assert "responder: B" in text assert html and "gmail_quote" in html def test_quote_trail_is_detected_in_forwarded_history(): assert _body_has_quote_trail( "responder: B\n\nOn Tue, Oct 6, 2026 at 7:44 AM Suyash Dixit wrote:\n> sender: A", None, ) assert _body_has_quote_trail( "sender: C", '