"""Integration-style tests for Human Handoff → Customer RFC threading.""" from bson import ObjectId from app.services.external_reply_service import ( build_external_reply_plan, internet_message_id_of, ) from app.services.mail_sender import build_message, sign_message from types import SimpleNamespace def test_handoff_bridge_first_reply_threads_against_customer_a(): customer_a_id = "" customer_a = { "_id": ObjectId(), "internet_message_id": customer_a_id, "message_id": customer_a_id, "message_scope": "EXTERNAL", "direction": "inbound", "references": [], "subject": "fresh test", } plan = build_external_reply_plan( parent_external_message=customer_a, body_text="human said B", recipients=["091rabbytes@gmail.com"], ) assert plan.threading.in_reply_to == customer_a_id assert plan.threading.references == [customer_a_id] assert plan.threading.in_reply_to != "" assert "" not in plan.threading.references def test_lowercased_stored_id_recovered_via_raw_headers(): customer_a_id = "" parent = { "_id": ObjectId(), "internet_message_id": "", "message_id": "", "raw_headers": {"message_id": customer_a_id}, "references": [], "subject": "fresh test", } plan = build_external_reply_plan( parent_external_message=parent, body_text="human said B", recipients=["customer@gmail.com"], parent_internet_id_override=internet_message_id_of(parent), ) assert plan.threading.in_reply_to == customer_a_id assert plan.threading.references == [customer_a_id] def test_signed_mime_preserves_threading_headers(): customer_a_id = "" message = build_message( sender="suyash@gentic.email", recipients=["091rabbytes@gmail.com"], cc=[], subject="Re: fresh test", body_text="human said B", body_html=None, in_reply_to=customer_a_id, references=[customer_a_id], message_id="", ) domain = SimpleNamespace( domain_name="gentic.email", dkim_selector="agmail", dkim_private_key="private-key", ) from unittest.mock import patch with patch( "app.services.mail_sender.dkim.sign", return_value=b"DKIM-Signature: test\r\n", ): signed = sign_message(message, domain) assert f"In-Reply-To: {customer_a_id}".encode() in signed assert f"References: {customer_a_id}".encode() in signed