from __future__ import annotations

import pytest

from src.services.agent_market_candidate_adapter import (
    build_contract_probe_result,
    get_market_candidate_spec,
)


def test_contract_probe_result_is_fail_closed_and_contract_compliant():
    result = build_contract_probe_result(
        {
            "schema_version": "agent_replay_candidate_input_v1",
            "run_id": "run",
            "incident_id": "INC-1",
            "incident_context": {
                "severity": "P1",
                "alertname": "PodCrashLooping",
            },
            "source_metadata": {},
        },
        candidate_id="nemo_nemotron_fabric",
    )

    assert result["schema_version"] == "agent_candidate_replay_result_v1"
    assert result["candidate_id"] == "nemo_nemotron_fabric"
    assert result["candidate_role"] == "agent_fabric_tool_model_evaluator"
    assert result["blocked_by_policy"] is True
    assert result["fallback_used"] is True
    assert result["requires_human_approval"] is True
    assert result["cost_usd"] == 0
    assert result["metadata"]["not_replacement_evidence"] is True


def test_contract_probe_rejects_label_leak_before_adapter_execution():
    with pytest.raises(ValueError, match="evaluation label"):
        build_contract_probe_result(
            {
                "run_id": "run",
                "incident_id": "INC-1",
                "incident_context": {
                    "verification_result": "success",
                },
            },
            candidate_id="openai_agents_sdk_coordinator",
        )


def test_unknown_candidate_id_is_rejected():
    with pytest.raises(ValueError, match="unknown market candidate_id"):
        get_market_candidate_spec("unknown_candidate")
