import os from app.rag import rerank def test_rerank_disabled_is_stable(monkeypatch): monkeypatch.delenv("RAG_RERANKER_MODEL", raising=False) rows = [{"id": "a", "text": "第一"}, {"id": "b", "text": "第二"}] assert rerank.rerank(rows, "问题") == rows metric = rerank.last_metric() assert metric["fallback"] is True assert metric["reranked_count"] == 0 def test_rerank_local_model_missing_falls_back(monkeypatch, tmp_path): monkeypatch.setenv("RAG_RERANKER_MODEL", str(tmp_path / "missing")) rows = [{"id": "a", "text": "第一"}, {"id": "b", "text": "第二"}] assert rerank.rerank(rows, "问题") == rows assert rerank.last_metric()["fallback"] is True