human hand off fix

This commit is contained in:
debu-2 2026-10-06 07:53:23 +05:30
parent 0981262612
commit 249fb79aaf
3 changed files with 161 additions and 23 deletions

View file

@ -1,6 +1,10 @@
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,
@ -20,6 +24,80 @@ def test_human_handoff_runtime_is_enabled():
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",
'<div>sender: C</div><div class="gmail_quote">sender: A</div>',
)
assert _body_has_quote_trail("sender: E", None) is False
def test_mirror_from_uses_one_address_not_both():
name, address = _mirror_from_identity(
{
"direction": "inbound",
"sender": {
"name": "Suyash Dixit",
"address": "suyashdixit091@gmail.com",
},
},
"Suyash Dixit",
"suyash@gentic.email",
)
assert name == "Suyash Dixit"
assert address == "suyashdixit091@gmail.com"
assert "gentic.email" not in address
agent_name, agent_address = _mirror_from_identity(
{"direction": "outbound", "sender": {"address": "suyash@gentic.email"}},
"Suyash Dixit",
"suyash@gentic.email",
)
assert agent_name == "Suyash Dixit"
assert agent_address == "suyash@gentic.email"
assert "gmail.com" not in agent_address
def test_private_subject_contains_customer_name_and_external_subject():
subject = build_handoff_subject(
sender_name="Parth Dixit",