from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def read(path: str) -> str: return (ROOT / path).read_text(encoding="utf-8") def test_task_detail_guard_handles_polluted_opportunity_customer_after_detach(): source = read("app/admin_ui/pages/tasks.py") assert "opportunity_customer_mismatch_process_hint" in source assert '"process_identity_unsafe"' in source assert '"identity_unsafe"' in source assert 'if identity_ctx.get("identity_unsafe") and action_code in DOCUMENT_TASK_ACTIONS:' in source def test_identity_mismatch_audit_can_apply_safe_detach_with_audit_trail(): source = read("scripts/audit_task_identity_mismatch.py") assert "load_dotenv(PROJECT_ROOT / \".env\")" in source assert "parser.add_argument(\"--apply\"" in source assert "local_customer_id = NULL" in source assert "identity_detached" in source assert "requires_fiscal_customer_review" in source def test_identity_repair_script_is_conservative_after_false_positive_cleanup(): source = read("scripts/audit_task_identity_mismatch.py") assert "is_unsafe_fiscal_link" in source assert 'if not row.get("linked_customer_id")' in source assert "without a fiscal link there is nothing to detach" in source assert "overlaps(hint, fiscal)" in source assert "domains_match" in source def test_system_audit_treats_db_deadlocks_as_warn(): source = read("scripts/audit_system_health.py") assert "is_transient_db_lock_error" in source assert "DeadlockDetected" in source assert "DB contagem temporariamente bloqueada" in source assert "safe_count_table" in source