agentic.email/backend/tests/test_handoff_mirror.py
2026-10-06 02:44:54 +05:30

85 lines
3 KiB
Python

from app.services.handoff_email_templates import (
build_mirror_handoff_html,
build_mirror_handoff_text,
external_reply_subject,
format_sender_line,
handoff_email_subject,
)
from app.services.html_sanitizer import sanitize_email_html
from app.services.mime_body_extractor import extract_mirror_content
from app.services.thread_service import threading_fields_from_parent
def test_mirror_template_is_minimal():
html = build_mirror_handoff_html(
sender_line="091rabbytes@gmail.com",
external_subject="lets try one more time",
original_html="<p>one last attempt. i am tired</p>",
agent_email="suyash@gentic.email",
)
assert "LATEST MESSAGE" not in html
assert "Why you" not in html
assert "gentic-handoff-original" in html
assert "one last attempt. i am tired" in html
assert "✦ Human Handoff" in html
assert "091rabbytes@gmail.com" in html
assert "lets try one more time" in html
def test_mirror_plain_text_template():
text = build_mirror_handoff_text(
sender_line="091rabbytes@gmail.com",
external_subject="lets try one more time",
original_body_text="one last attempt. i am tired",
agent_email="suyash@gentic.email",
)
assert "one last attempt. i am tired" in text
assert "LATEST MESSAGE" not in text
assert "Reply normally" in text
def test_format_sender_with_name():
assert (
format_sender_line({"name": "Rahul Sharma", "address": "rahul@example.com"})
== "Rahul Sharma <rahul@example.com>"
)
def test_external_threading_headers_from_customer_message():
parent = {
"_id": "mongo-id",
"message_id": "<a@gmail.com>",
"references": [],
}
threading = threading_fields_from_parent(parent)
assert threading["in_reply_to"] == "<a@gmail.com>"
assert threading["references"] == ["<a@gmail.com>"]
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 = '<p>Hello</p><script>alert(1)</script><img src="x" onerror="alert(1)">'
clean = sanitize_email_html(dirty)
assert "<script" not in clean.lower()
assert "onerror" not in clean.lower()
assert "Hello" in clean
def test_extract_html_from_multipart():
raw = (
b"From: sender@example.com\r\n"
b"To: agent@gentic.email\r\n"
b"Subject: Invoice approval\r\n"
b'MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="b"\r\n\r\n'
b"--b\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPlain fallback\r\n"
b"--b\r\nContent-Type: text/html; charset=utf-8\r\n\r\n"
b"<h2>Invoice</h2><table><tr><td>Amount</td></tr></table>\r\n"
b"--b--\r\n"
)
extracted = extract_mirror_content(raw)
assert "Invoice" in (extracted.body_html or "")
assert "<table>" in (extracted.body_html or "")