import pytest
from app.services.handoff_email_templates import (
external_reply_subject,
format_sender_line,
handoff_email_subject,
)
from app.services.handoff_mirror import load_mirror_payload
from app.services.html_sanitizer import sanitize_email_html
from app.services.mime_body_extractor import (
extract_mirror_content,
extract_new_reply_mime_content,
)
from app.services.thread_service import threading_fields_from_parent
@pytest.mark.asyncio
async def test_mirror_body_is_unchanged_without_gentic_wrapper():
payload = await load_mirror_payload(
{
"body_text": "one last attempt. i am tired",
"body_html": "
one last attempt. i am tired
",
}
)
assert payload.body_text == "one last attempt. i am tired"
assert payload.body_html == (
"one last attempt. i am tired
"
)
assert "Human Handoff" not in payload.body_text
assert "gentic-handoff" not in (payload.body_html or "")
@pytest.mark.asyncio
async def test_mirror_plain_text_fallback_is_unchanged():
payload = await load_mirror_payload(
{
"body_text": "one last attempt. i am tired",
"body_html": None,
}
)
assert payload.body_text == "one last attempt. i am tired"
assert payload.body_html is None
def test_format_sender_with_name():
assert (
format_sender_line({"name": "Rahul Sharma", "address": "rahul@example.com"})
== "Rahul Sharma "
)
def test_quoted_history_uses_original_sender_not_human_owner():
from datetime import datetime, timezone
from app.services.handoff_email_templates import (
attach_quoted_history,
build_gmail_quote,
)
quoted_text, quoted_html = build_gmail_quote(
sender={"name": "Parth Dixit", "address": "parthdixit091@gmail.com"},
event_at=datetime(2026, 10, 6, 7, 29, tzinfo=timezone.utc),
body_text="sender: C",
body_html="sender: C
",
)
body_text, body_html = attach_quoted_history(
new_text="responder: D",
new_html="responder: D
",
quoted_text=quoted_text,
quoted_html=quoted_html,
)
assert body_text.startswith("responder: D")
assert "Parth Dixit " in body_text
assert "> sender: C" in body_text
assert "parthdixit091@gmail.com" in body_html
assert "Suyash AIML" not in body_text
def test_external_threading_headers_from_customer_message():
parent = {
"_id": "mongo-id",
"message_id": "",
"references": [],
}
threading = threading_fields_from_parent(parent)
assert threading["in_reply_to"] == ""
assert threading["references"] == [""]
def test_external_reply_subject_uses_customer_subject():
assert external_reply_subject("lets try one more time") == "Re: lets try one more time"
assert handoff_email_subject("lets try one more time") == "Human Handoff ยท lets try one more time"
def test_sanitize_removes_script():
dirty = 'Hello
'
clean = sanitize_email_html(dirty)
assert "