# [DEF:backend.tests.test_report_normalizer:Module]
# @TIER: STANDARD
# @SEMANTICS: tests, reports, normalizer, fallback
# @PURPOSE: Validate unknown task type fallback and partial payload normalization behavior.
# @LAYER: Domain (Tests)
# @RELATION: TESTS -> backend.src.services.reports.normalizer
# @INVARIANT: Unknown plugin types are mapped to canonical unknown task type.

from datetime import datetime

from src.core.task_manager.models import Task, TaskStatus
from src.services.reports.normalizer import normalize_task_report


def test_unknown_type_maps_to_unknown_profile():
    task = Task(
        id="unknown-1",
        plugin_id="custom-unmapped-plugin",
        status=TaskStatus.FAILED,
        started_at=datetime.utcnow(),
        finished_at=datetime.utcnow(),
        params={},
        result={"error_message": "Unexpected plugin payload"},
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "unknown"
    assert report.summary
    assert report.error_context is not None


def test_partial_payload_keeps_report_visible_with_placeholders():
    task = Task(
        id="partial-1",
        plugin_id="superset-backup",
        status=TaskStatus.SUCCESS,
        started_at=datetime.utcnow(),
        finished_at=datetime.utcnow(),
        params={},
        result=None,
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "backup"
    assert report.details is not None
    assert "result" in report.details


def test_clean_release_plugin_maps_to_clean_release_task_type():
    task = Task(
        id="clean-release-1",
        plugin_id="clean-release-compliance",
        status=TaskStatus.SUCCESS,
        started_at=datetime.utcnow(),
        finished_at=datetime.utcnow(),
        params={"run_id": "run-1"},
        result={"summary": "Clean release compliance passed", "run_id": "run-1"},
    )

    report = normalize_task_report(task)

    assert report.task_type.value == "clean_release"
    assert report.summary == "Clean release compliance passed"


# [/DEF:backend.tests.test_report_normalizer:Module]