"""Test: generate_report unmasks slack_message before delivery.""" from __future__ import annotations from unittest.mock import MagicMock, patch import pytest @pytest.fixture(autouse=True) def _enable_masking(monkeypatch) -> None: monkeypatch.setenv("OPENSRE_MASK_ENABLED", "true") def _state_with_masking() -> dict[str, object]: return { "alert_name": "pipeline failure", "pipeline_name": "pipeline", "severity": "warning", "problem_md": "# Incident in ", "slack_message": "", "report": "", "masking_map": { "": "etl-worker-7d9f8b-xkp2q", "": "tracer-test", }, "root_cause": "etl-worker-7d9f8b-xkp2q OOMKilled in tracer-test", "evidence": {}, "context": {}, "resolved_integrations": {}, "slack_context": {}, "discord_context": {}, "validated_claims": [], "non_validated_claims": [], } def test_slack_message_is_unmasked_before_delivery() -> None: from tools.investigation.reporting import node as pub_node from tools.investigation.reporting.formatters.messages import ReportMessages masked_message = "Root cause: crashed in . Impact: customer-facing." with ( patch.object(pub_node, "build_report_context", return_value=MagicMock()), patch.object( pub_node, "build_report_messages", return_value=ReportMessages(masked_message, "tg", "wa", []), ), patch.object(pub_node, "render_report"), patch.object(pub_node, "open_in_editor"), patch.object( pub_node, "create_investigation_and_attach_url", return_value=("inv-123", "https://test/inv-123"), ), patch("integrations.slack.delivery.send_slack_report", return_value=(False, None)), patch("integrations.slack.delivery.build_action_blocks", return_value=[]), ): result = pub_node.generate_report(_state_with_masking()) # type: ignore[arg-type] assert "" not in result["slack_message"] assert "" not in result["slack_message"] assert "etl-worker-7d9f8b-xkp2q" in result["slack_message"] assert "tracer-test" in result["slack_message"] def test_empty_masking_map_is_passthrough() -> None: from tools.investigation.reporting import node as pub_node from tools.investigation.reporting.formatters.messages import ReportMessages state = _state_with_masking() state["masking_map"] = {} message_without_placeholders = "Plain report with no placeholders." with ( patch.object(pub_node, "build_report_context", return_value=MagicMock()), patch.object( pub_node, "build_report_messages", return_value=ReportMessages(message_without_placeholders, "tg", "wa", []), ), patch.object(pub_node, "render_report"), patch.object(pub_node, "open_in_editor"), patch.object( pub_node, "create_investigation_and_attach_url", return_value=("inv-123", "https://test/inv-123"), ), patch("integrations.slack.delivery.send_slack_report", return_value=(False, None)), patch("integrations.slack.delivery.build_action_blocks", return_value=[]), ): result = pub_node.generate_report(state) # type: ignore[arg-type] assert result["slack_message"] == message_without_placeholders