from __future__ import annotations

import pytest

from src.services.agent_market_scorecard import score_market_capabilities


def test_market_scorecard_ranks_candidates_against_openclaw_baseline():
    report = score_market_capabilities({
        "baseline_candidate_id": "openclaw_incumbent",
        "scoring_version": "test",
        "dimensions": {
            "durable_execution": 0.5,
            "human_in_loop": 0.5,
        },
        "candidates": [
            {
                "candidate_id": "openclaw_incumbent",
                "display_name": "OpenClaw",
                "evaluation_priority": "baseline",
                "capabilities": {
                    "durable_execution": 1,
                    "human_in_loop": 3,
                },
            },
            {
                "candidate_id": "langgraph_incident_kernel",
                "display_name": "LangGraph",
                "evaluation_priority": "must_test",
                "capabilities": {
                    "durable_execution": 3,
                    "human_in_loop": 3,
                },
            },
        ],
    }).to_dict()

    winner = report["candidates"][0]

    assert winner["candidate_id"] == "langgraph_incident_kernel"
    assert winner["beats_baseline_capability"] is True
    assert winner["replay_priority"] == "p0_replay"
    assert report["candidates_above_baseline"] == ["langgraph_incident_kernel"]


def test_market_scorecard_requires_weights_to_sum_to_one():
    with pytest.raises(ValueError, match="dimension weights"):
        score_market_capabilities({
            "dimensions": {"durable_execution": 0.4},
            "candidates": [
                {
                    "candidate_id": "openclaw_incumbent",
                    "capabilities": {"durable_execution": 1},
                }
            ],
        })
