from app import selection_judge


def test_parse_judge_response_ok():
    raw = '[{"keyword_id": 1, "score": 0.8}, {"keyword_id": 2, "score": 0.3}]'
    assert selection_judge.parse_judge_response(raw) == {1: 0.8, 2: 0.3}


def test_parse_judge_response_codefence():
    raw = '```json\n[{"keyword_id": 5, "score": 0.5}]\n```'
    assert selection_judge.parse_judge_response(raw) == {5: 0.5}


def test_parse_judge_response_garbage_returns_empty():
    assert selection_judge.parse_judge_response("not json") == {}


def test_judge_candidates_no_key_returns_empty(monkeypatch):
    monkeypatch.setattr(selection_judge, "ANTHROPIC_API_KEY", "")
    assert selection_judge.judge_candidates([{"id": 1, "keyword": "x", "category": "economy"}]) == {}
