"""Tests for src/askme/evaluation/retrieval.py.""" from __future__ import annotations from pathlib import Path from unittest.mock import AsyncMock, MagicMock, patch from typer.testing import CliRunner from askme.evaluation.retrieval import _hit, app, run_retrieval_eval runner = CliRunner() def test_hit_is_case_insensitive_substring(): """ Scenario: an expected term is found regardless of case Given a retrieved context mentioning "CellChat" Then _hit matches the expected term "cellchat" """ assert _hit("... uses CellChat to infer communication ...", "cellchat") is True assert _hit("... only scanpy preprocessing here ...", "cellbender") is False def test_run_retrieval_eval_scores_term_recall(): """ Scenario: hit-rate reflects which expected terms were retrieved Given two questions where retrieval surfaces one expected term but not the other When run_retrieval_eval scores them Then hit_rate is 0.5 and each result carries its hit/miss """ testset = [ {"question": "ambient RNA?", "expected": "cellbender"}, {"question": "velocity?", "expected": "scvelo"}, ] async def fake_aquery(question, param=None): return "cellbender removes ambient RNA" if "ambient" in question else "unrelated context" rag = MagicMock() rag.initialize_storages = AsyncMock() rag.finalize_storages = AsyncMock() rag.aquery = AsyncMock(side_effect=fake_aquery) with patch("askme.evaluation.retrieval.create_rag", return_value=rag): scores = run_retrieval_eval(testset) assert scores["n"] == 2 assert scores["hit_rate"] == 0.5 by_term = {r["expected"]: r["hit"] for r in scores["results"]} assert by_term["cellbender"] is True assert by_term["scvelo"] is False def test_run_retrieval_eval_requests_context_only(): """ Scenario: retrieval eval never triggers answer generation When run_retrieval_eval queries the RAG Then every aquery is issued with only_need_context=True """ testset = [{"question": "q", "expected": "x"}] rag = MagicMock() rag.initialize_storages = AsyncMock() rag.finalize_storages = AsyncMock() rag.aquery = AsyncMock(return_value="x") with patch("askme.evaluation.retrieval.create_rag", return_value=rag): run_retrieval_eval(testset) param = rag.aquery.call_args.kwargs["param"] assert param.only_need_context is True def test_eval_retrieval_cli_writes_report(tmp_path: Path): """ Scenario: the CLI runs the eval and writes a JSON report When askme-eval-retrieval runs with a small testset Then it exits 0 and the report holds the hit_rate """ import json report = tmp_path / "retrieval_report.json" testset = tmp_path / "ts.json" testset.write_text(json.dumps([{"question": "ambient RNA?", "expected": "cellbender"}])) fake = {"hit_rate": 1.0, "n": 1, "results": [ {"question": "ambient RNA?", "expected": "cellbender", "hit": True} ]} with patch("askme.evaluation.retrieval.run_retrieval_eval", return_value=fake): result = runner.invoke( app, ["--testset", str(testset), "--output", str(report)] ) assert result.exit_code == 0 assert json.loads(report.read_text())["hit_rate"] == 1.0 def test_eval_retrieval_cli_defaults_to_data_dir_testset(tmp_path: Path, monkeypatch): """ Scenario: the private retrieval testset lives in the data dir, not in code Given settings.data_dir holds retrieval_testset.json When askme-eval-retrieval runs without --testset Then that file's questions are evaluated """ import json ts = [{"question": "q", "expected": "t"}] (tmp_path / "retrieval_testset.json").write_text(json.dumps(ts)) monkeypatch.setattr("askme.evaluation.retrieval.settings.data_dir", str(tmp_path)) fake = {"hit_rate": 1.0, "n": 1, "results": []} with patch("askme.evaluation.retrieval.run_retrieval_eval", return_value=fake) as run: result = runner.invoke(app, ["--output", str(tmp_path / "r.json")]) assert result.exit_code == 0 assert run.call_args.args[0] == ts def test_eval_retrieval_cli_missing_testset_points_to_example(tmp_path: Path, monkeypatch): """ Scenario: a fresh clone has no retrieval testset yet Given no retrieval_testset.json in settings.data_dir When askme-eval-retrieval runs without --testset Then it exits non-zero and points at eval/retrieval_testset.example.json """ monkeypatch.setattr("askme.evaluation.retrieval.settings.data_dir", str(tmp_path)) result = runner.invoke(app, ["--output", str(tmp_path / "r.json")]) assert result.exit_code != 0 assert "retrieval_testset.example.json" in result.output