from pathlib import Path

import pytest

from chengyue_script.dvlm.evaluation import error_case_compare
from chengyue_script.dvlm.evaluation import analyze_docvqa_checkpoint_regressions as docvqa_reg
from chengyue_script.dvlm.evaluation import analyze_short_answer_qa as short_answer_qa


def test_get_vlmeval_root_prefers_third_party(tmp_path):
    third_party = tmp_path / "third_party" / "VLMEvalKit"
    local = tmp_path / "VLMEvalKit_local"
    third_party.mkdir(parents=True)
    local.mkdir(parents=True)

    assert error_case_compare.get_vlmeval_root(tmp_path) == third_party
    assert docvqa_reg.get_vlmeval_root(tmp_path) == third_party
    assert short_answer_qa.get_vlmeval_root(tmp_path) == third_party


def test_get_vlmeval_root_falls_back_to_local(tmp_path):
    local = tmp_path / "VLMEvalKit_local"
    local.mkdir(parents=True)

    assert error_case_compare.get_vlmeval_root(tmp_path) == local
    assert docvqa_reg.get_vlmeval_root(tmp_path) == local
    assert short_answer_qa.get_vlmeval_root(tmp_path) == local


def test_find_experiment_paths_prefers_third_party(tmp_path, monkeypatch):
    exp_name = "exp_tp"
    vlm_root = tmp_path / "third_party" / "VLMEvalKit"
    eval_dir = vlm_root / "output_models" / exp_name / "eval_vlmeval" / "checkpoint-100"
    eval_dir.mkdir(parents=True)

    monkeypatch.setattr(error_case_compare, "PROJECT_ROOT", tmp_path)

    info = error_case_compare.find_experiment_paths(exp_name)

    assert info.vlmeval_dir == vlm_root / "output_models" / exp_name
    assert info.checkpoints == ["checkpoint-100"]


def test_find_experiment_paths_falls_back_to_local(tmp_path, monkeypatch):
    exp_name = "exp_local"
    vlm_root = tmp_path / "VLMEvalKit_local"
    eval_dir = vlm_root / "output_models" / exp_name / "eval_vlmeval" / "checkpoint-200"
    eval_dir.mkdir(parents=True)

    monkeypatch.setattr(error_case_compare, "PROJECT_ROOT", tmp_path)

    info = error_case_compare.find_experiment_paths(exp_name)

    assert info.vlmeval_dir == vlm_root / "output_models" / exp_name
    assert info.checkpoints == ["checkpoint-200"]
