"""知识分类 API 测试(Qdrant 为 mock,不真实联网)""" from collections.abc import Iterator import pytest from fastapi.testclient import TestClient from app.main import app from app.models.knowledge import UNCATEGORIZED, load_taxonomy from app.services.qdrant import QdrantService @pytest.fixture def client(monkeypatch: pytest.MonkeyPatch) -> Iterator[TestClient]: """TestClient,lifespan 中的 Qdrant 集合初始化替换为空操作""" async def _noop_ensure_collections(self: QdrantService) -> None: return None monkeypatch.setattr(QdrantService, "ensure_collections", _noop_ensure_collections) with TestClient(app) as test_client: yield test_client def test_list_categories(client: TestClient) -> None: """categories 返回完整 taxonomy,且含 uncategorized""" resp = client.get("/api/v1/knowledge/categories") assert resp.status_code == 200 body = resp.json() assert body["code"] == 0 expected = load_taxonomy() data = body["data"] assert data["count"] == len(expected) names = [c["name"] for c in data["categories"]] assert names == [c.name for c in expected] assert UNCATEGORIZED in names