from datetime import UTC, datetime from zbt.domains.knowledge.index import InMemoryKnowledgeIndex from zbt.domains.knowledge.repository import InMemoryKnowledgeRepository from zbt.domains.knowledge.service import KnowledgeService def test_search_only_returns_published_knowledge() -> None: repository = InMemoryKnowledgeRepository() index = InMemoryKnowledgeIndex() service = KnowledgeService( repository, lambda: datetime(2026, 7, 27, 3, 0, tzinfo=UTC), index, ) published = service.create_document( title="安心医疗险保险条款", document_type="INSURANCE_TERMS", source_name="安心医疗险条款.pdf", product_code="MED-BASIC", content="疾病医疗等待期为30天,意外伤害不受等待期限制。", created_by="admin-01", ) draft = service.create_document( title="尚未发布的内部说明", document_type="FAQ", source_name="内部说明.md", product_code=None, content="内部测试内容声称等待期为10天,但该内容尚未发布。", created_by="admin-01", ) service.index_document(published["document_id"]) service.test_search(published["document_id"], "医疗险等待期是多少天?") service.publish_document(published["document_id"]) service.index_document(draft["document_id"]) result = service.search("医疗险等待期是多少天?") assert result["total"] == 1 assert result["items"][0]["document_id"] == published["document_id"] assert result["items"][0]["title"] == "安心医疗险保险条款" assert result["items"][0]["source_name"] == "安心医疗险条款.pdf" assert result["items"][0]["document_version"] == 1 assert "30天" in result["items"][0]["content"]