from app.services.human_handoff_service import ( MESSAGE_SCOPE_EXTERNAL, MESSAGE_SCOPE_HANDOFF, 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) def test_private_subject_contains_customer_name_and_external_subject(): subject = build_handoff_subject( sender_name="Parth Dixit", external_sender="parthdixit091@gmail.com", external_subject="Invoice 42", ) assert subject == ( "Parth Dixit · handoff · parthdixit091@gmail.com · Invoice 42" ) def test_private_subject_removes_header_line_breaks(): subject = build_handoff_subject( sender_name="Urgent\r\nBcc: attacker@example.com", external_sender="customer@example.com", external_subject="Help", ) assert "\r" not in subject assert "\n" not in subject def test_human_address_is_replaced_before_external_relay(): value = "Regards,\nOwner " assert redact_human_address( value, human_email="private.owner@example.com", agent_email="agent@gentic.email", ) == "Regards,\nOwner " def test_lowest_priority_matching_rule_wins(): rules = [ { "name": "Backup", "priority": 20, "labels": ["All mail"], "email_types": ["all"], "instructions": "", }, { "name": "Primary", "priority": 1, "labels": ["All mail"], "email_types": ["all"], "instructions": "", }, ] selected = select_matching_handoff_rule( rules, email={"subject": "Hello", "body_text": "Need help"}, ) assert selected["name"] == "Primary"