"""Tests for unified cortex-eval CLI (#1282)."""
from __future__ import annotations

import json
import pathlib
import tempfile
import pytest

from cortex.eval.run_eval import _build_parser, _log_run, _compare_baseline, BENCHMARKS


class TestParser:
    def test_accepts_all_benchmarks(self):
        parser = _build_parser()
        for bm in BENCHMARKS:
            args = parser.parse_args(["--benchmark", bm])
            assert args.benchmark == bm

    def test_dataset_alias(self):
        """--dataset is an alias for --benchmark."""
        parser = _build_parser()
        args = parser.parse_args(["--dataset", "locomo"])
        assert args.benchmark == "locomo"

    def test_default_mode(self):
        parser = _build_parser()
        args = parser.parse_args(["--benchmark", "locomo"])
        assert args.mode == "retrieval-only"

    def test_confidence_threshold_flag(self):
        parser = _build_parser()
        args = parser.parse_args(["--benchmark", "locomo", "--confidence-threshold", "0.5"])
        assert args.confidence_threshold == 0.5

    def test_runs_log_flag(self):
        parser = _build_parser()
        args = parser.parse_args(["--benchmark", "locomo", "--runs-log", "my_runs.jsonl"])
        assert args.runs_log == "my_runs.jsonl"

    def test_baseline_flag(self):
        parser = _build_parser()
        args = parser.parse_args(["--benchmark", "locomo", "--baseline", "base.json"])
        assert args.baseline == "base.json"


class TestLogRun:
    def test_writes_jsonl(self, tmp_path):
        log_path = str(tmp_path / "runs.jsonl")

        _log_run(
            runs_log=log_path,
            benchmark="locomo",
            metrics={"avg_token_f1": 0.75},
            config={"mode": "retrieval-only"},
            elapsed_sec=10.5,
        )

        with open(log_path) as f:
            line = f.readline()
            data = json.loads(line)

        assert data["benchmark"] == "locomo"
        assert data["metrics"]["avg_token_f1"] == 0.75
        assert data["elapsed_sec"] == 10.5
        assert "timestamp" in data

    def test_appends_multiple_runs(self, tmp_path):
        log_path = str(tmp_path / "runs.jsonl")

        for i in range(3):
            _log_run(log_path, "locomo", {"run": i}, {}, 1.0)

        with open(log_path) as f:
            lines = f.readlines()
        assert len(lines) == 3


class TestBaselineComparison:
    def test_compare_baseline(self, capsys, tmp_path):
        baseline_path = str(tmp_path / "baseline.json")
        with open(baseline_path, "w") as f:
            json.dump({
                "metrics": {
                    "exact_match_accuracy": 0.5,
                    "fuzzy_match_accuracy": 0.6,
                    "avg_token_f1": 0.55,
                }
            }, f)

        current = {
            "exact_match_accuracy": 0.6,
            "fuzzy_match_accuracy": 0.7,
            "avg_token_f1": 0.65,
        }
        _compare_baseline(baseline_path, current)

        captured = capsys.readouterr()
        assert "Baseline comparison" in captured.out
        assert "↑" in captured.out  # improvements
