from __future__ import annotations import sys from pathlib import Path from typing import Any, Dict, List # Ensure backend package imports resolve when pytest runs from repo root. BACKEND_DIR = Path(__file__).resolve().parents[1] if str(BACKEND_DIR) not in sys.path: sys.path.insert(0, str(BACKEND_DIR)) from app.masters_ai.core import MastersAICore from app.masters_ai.index import DocChunk, SearchHit from app.pots_ai.core import PotsAICore def test_masters_no_key_fallback_is_compact(tmp_path: Path, monkeypatch) -> None: data_dir = tmp_path / "masters_data" data_dir.mkdir(parents=True, exist_ok=True) core = MastersAICore(data_dir=str(data_dir), openai_api_key="") long_text = ("securefax " * 2000).strip() hits: List[SearchHit] = [ SearchHit( chunk=DocChunk( id=f"c-{i}", doc="MST_SecureFAX.pdf", location=f"p.{i+1}", text=long_text, ), score=0.92, ) for i in range(20) ] monkeypatch.setattr(core.index, "health", lambda: {"status": "ready", "ready": True}) monkeypatch.setattr(core.index, "search", lambda q, top_k=8: list(hits)) out = core.handle_message("Which documents mention SecureFAX?", {}, audience="auto") assistant = str(out.get("assistant") or "") assert "OPENAI_API_KEY is not set" in assistant assert len(assistant) < 5000, "fallback response should be compact for UI readability" assert "Traceback" not in assistant def test_pots_no_key_fallback_is_compact(tmp_path: Path, monkeypatch) -> None: data_dir = tmp_path / "pots_data" data_dir.mkdir(parents=True, exist_ok=True) core = PotsAICore(data_dir=str(data_dir), openai_api_key="") long_excerpt = ("provider evidence " * 2200).strip() monkeypatch.setattr(core.index, "health", lambda: {"status": "ready", "ready": True, "chunks_count": 20, "files_count": 5}) monkeypatch.setattr( core, "_search_expanded", lambda queries, k=10: [ { "id": f"S{i+1}", "doc": "OO-AirDial-Datasheet.pdf", "page": i + 1, "text": long_excerpt, "score": 0.61 - (i * 0.01), } for i in range(8) ], ) out = core.handle_message("Compare OOMA vs MetTel for alarm lines", {}, audience="auto") assistant = str(out.get("assistant") or "") assert "full synthesis model" in assistant.lower() assert len(assistant) < 6000, "fallback response should be compact for UI readability" assert "Traceback" not in assistant