"""Unit tests for DoX quality assessment helpers (STATE-WP-0077)."""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))

from quality_assessment import (  # noqa: E402
    assessment_from_mapping,
    badge,
    debt_for_intake_meta,
    debt_for_workplan_meta,
    intake_has_doc_ok,
    is_ok,
    progress_event_body,
)


def test_badge_ok_failed():
    assert badge("DoR", "Ok") == "DoR-Ok"
    assert badge("DoC", "Failed") == "DoC-Failed"
    assert badge("DoD", "DoD-Ok") == "DoD-Ok"


def test_assessment_from_mapping():
    assert assessment_from_mapping({"quality_dor": "DoR-Ok"}, "DoR") == "DoR-Ok"
    assert assessment_from_mapping({"quality_dor": "Ok"}, "DoR") == "DoR-Ok"
    assert assessment_from_mapping({}, "DoR") is None
    assert is_ok(assessment_from_mapping({"quality_dod": "Failed"}, "DoD")) is False


def test_debt_ready_without_dor():
    items = debt_for_workplan_meta(
        {"id": "STATE-WP-0001", "status": "ready"},
        path="workplans/x.md",
    )
    assert len(items) == 1
    assert items[0].missing == "DoR-Ok"


def test_debt_finished_without_dod():
    items = debt_for_workplan_meta(
        {"id": "STATE-WP-0001", "status": "finished", "quality_dod": "DoD-Ok"},
        path="workplans/x.md",
    )
    assert items == []


def test_debt_intake_routed():
    items = debt_for_intake_meta(
        {"id": "CUST-IN-0001", "status": "routed"},
        path="notes.md",
    )
    assert len(items) == 1
    assert items[0].missing == "DoC-Ok"


def test_intake_has_doc_ok_from_note():
    assert intake_has_doc_ok({"notes": [{"content": "Assessed DoC-Ok today"}]})
    assert not intake_has_doc_ok({"notes": [{"content": "still open"}]})


def test_progress_event_body():
    body = progress_event_body(
        policy="DoR",
        outcome="Ok",
        record_kind="workplan",
        record_id="STATE-WP-0077",
        assessed_by="grok",
    )
    assert body["event_type"] == "quality_assessment"
    assert body["detail"]["badge"] == "DoR-Ok"
