"""Shared builders for the knowledge-loop tests.""" from __future__ import annotations import uuid import pytest from app.domains.support.models import ( KB_PUBLISHED, KB_SOURCE_FEISHU_DOC, KB_SOURCE_MANUAL, SupportKbArticle, ) @pytest.fixture(autouse=True) def _clear_wiki_caches(): """The index and the vector cache are process-global. Without this a test that publishes an article sees the previous test's corpus, which is the classic 「单跑绿混跑红」 shape — and here it would be especially confusing because the staleness probe makes the cache *usually* correct. """ from app.domains.support.kb_service import reset_vector_cache from app.domains.support.kb_wiki import reset_index_cache reset_index_cache() reset_vector_cache() yield reset_index_cache() reset_vector_cache() async def make_article( db, *, title: str, body: str = "正文。", status: str = KB_PUBLISHED, source: str = KB_SOURCE_MANUAL, aliases: str | None = None, summary: str | None = None, embedding: list[float] | None = None, category: str = "network", ) -> SupportKbArticle: article = SupportKbArticle( id=uuid.uuid4(), title=title, body=body, category=category, status=status, source=source, search_aliases=aliases, summary=summary, embedding=embedding, embedding_model="text-embedding-v3" if embedding else None, ) db.add(article) await db.flush() return article async def make_mirror(db, *, title: str, body: str = "镜像正文。") -> SupportKbArticle: return await make_article( db, title=title, body=body, source=KB_SOURCE_FEISHU_DOC, ) async def make_person(db, *, name: str = "评审人"): from app.identity.person_models import IdentityPerson person = IdentityPerson( id=uuid.uuid4(), source_system="successfactors", hris_person_id=f"P{uuid.uuid4().hex[:8]}", display_name=name, work_email=f"u{uuid.uuid4().hex[:8]}@pingcap.com", employment_status="active", ) db.add(person) await db.flush() return person