from fastapi.testclient import TestClient from duck_core.api import create_app from duck_core.memory.vector_memory import VectorMemory from duck_core.model_client import ModelClient def test_health_and_status_endpoints(tmp_path, monkeypatch): monkeypatch.setenv("DUCK_DB_PATH", str(tmp_path / "duck.sqlite3")) app = create_app() client = TestClient(app) assert client.get("/health").json()["status"] == "ok" status = client.get("/v1/status").json() assert status["name"] == "DuckLM" assert status["api_host"] == "127.0.0.1" assert status["api"]["host"] == "127.0.0.1" assert status["api"]["port"] == 8000 assert status["paths"]["db_path"] == str(tmp_path / "duck.sqlite3") assert status["models"]["default_provider"] == "llama_server" assert status["models"]["roles"]["thinker"]["model"] == "local-main" assert status["services"]["duck_api"]["ok"] is True assert status["services"]["llama"]["probed"] is False assert status["services"]["vector_memory"]["probed"] is False def test_status_endpoint_can_probe_backends(tmp_path, monkeypatch): monkeypatch.setenv("DUCK_DB_PATH", str(tmp_path / "duck.sqlite3")) async def fake_ping(self): return { "thinker": { "ok": True, "base_url": "http://127.0.0.1:8081/v1", "model": "local-main", "latency_ms": 1.2, }, "critic": { "ok": False, "base_url": "http://127.0.0.1:8081/v1", "model": "local-main", "error": "offline", }, } async def fake_vector_health(self): return { "configured": True, "ok": True, "qdrant_url": "http://127.0.0.1:6333", "collection": "duck_memory", "embedding_source": "local:./models/all-MiniLM-L6-v2", "latency_ms": 2.3, } monkeypatch.setattr(ModelClient, "ping", fake_ping) monkeypatch.setattr(VectorMemory, "health", fake_vector_health) app = create_app() client = TestClient(app) status = client.get("/v1/status?probe=true").json() assert status["services"]["llama"]["probed"] is True assert status["services"]["llama"]["ok"] is False assert status["services"]["llama"]["roles"]["thinker"]["ok"] is True assert status["services"]["llama"]["roles"]["critic"]["ok"] is False assert status["services"]["vector_memory"]["probed"] is True assert status["services"]["vector_memory"]["ok"] is True def test_webchat_index_renders(tmp_path, monkeypatch): monkeypatch.setenv("DUCK_DB_PATH", str(tmp_path / "duck.sqlite3")) app = create_app() client = TestClient(app) response = client.get("/") assert response.status_code == 200 assert "DuckLM" in response.text