from __future__ import annotations

from pathlib import Path

from testing.ci.summary import (
    RunSummary,
    load_junit_cases,
    load_junit_summary,
    pushgateway_series_exists,
    read_pushgateway_counters,
    render_payload,
)


def test_load_junit_summary_combines_suites(tmp_path: Path) -> None:
    junit = tmp_path / "results.xml"
    junit.write_text(
        '<testsuites><testsuite tests="3" failures="1" errors="0" skipped="1"/></testsuites>'
    )

    summary = load_junit_summary([junit])

    assert summary == RunSummary(tests=3, failures=1, errors=0, skipped=1)
    payload = render_payload(suite="bstein_home", ok=2, failed=0, summary=summary)
    assert 'platform_quality_gate_runs_total{suite="bstein_home",status="ok"} 2' in payload
    assert 'bstein_home_quality_gate_tests_total{suite="bstein_home",result="skipped"} 1' in payload
    assert 'platform_quality_gate_workspace_line_coverage_percent{suite="bstein_home"} 0.000' in payload
    assert 'platform_quality_gate_source_files_total{suite="bstein_home"} 0' in payload
    assert 'platform_quality_gate_source_lines_over_500_total{suite="bstein_home"} 0' in payload


def test_load_junit_cases_and_render_test_case_metrics(tmp_path: Path) -> None:
    junit = tmp_path / "cases.xml"
    junit.write_text(
        (
            "<testsuite>"
            '<testcase classname="app.health" name="test_ok" />'
            '<testcase classname="app.health" name="test_fail"><failure/></testcase>'
            "</testsuite>"
        ),
        encoding="utf-8",
    )

    cases = load_junit_cases([junit])
    assert ("app.health::test_ok", "passed") in cases
    assert ("app.health::test_fail", "failed") in cases

    payload = render_payload(
        suite="bstein_home",
        ok=1,
        failed=0,
        summary=RunSummary(tests=2, failures=1, errors=0, skipped=0),
        test_cases=cases,
    )
    assert 'platform_quality_gate_test_case_result{suite="bstein_home"' in payload
    assert 'test="app.health::test_fail",status="failed"} 1' in payload


def test_pushgateway_counter_parser_is_label_order_insensitive() -> None:
    text = "\n".join(
        [
            'platform_quality_gate_runs_total{suite="bstein_home",status="ok",job="platform-quality-ci"} 10',
            'platform_quality_gate_runs_total{status="failed",job="platform-quality-ci",suite="bstein_home"} 2',
            'platform_quality_gate_build_info{suite="bstein_home",branch="master",build_number="274",jenkins_job="bstein-dev-home",job="platform-quality-ci"} 1',
        ]
    )

    assert read_pushgateway_counters(text, suite="bstein_home", job="platform-quality-ci") == {"ok": 10.0, "failed": 2.0}
    assert pushgateway_series_exists(
        text,
        metric="platform_quality_gate_build_info",
        labels={
            "job": "platform-quality-ci",
            "suite": "bstein_home",
            "branch": "master",
            "build_number": "274",
            "jenkins_job": "bstein-dev-home",
        },
    )
