test_knowledge_retrieval.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from datetime import UTC, datetime
  2. from zbt.domains.knowledge.index import InMemoryKnowledgeIndex
  3. from zbt.domains.knowledge.repository import InMemoryKnowledgeRepository
  4. from zbt.domains.knowledge.service import KnowledgeService
  5. def test_search_only_returns_published_knowledge() -> None:
  6. repository = InMemoryKnowledgeRepository()
  7. index = InMemoryKnowledgeIndex()
  8. service = KnowledgeService(
  9. repository,
  10. lambda: datetime(2026, 7, 27, 3, 0, tzinfo=UTC),
  11. index,
  12. )
  13. published = service.create_document(
  14. title="安心医疗险保险条款",
  15. document_type="INSURANCE_TERMS",
  16. source_name="安心医疗险条款.pdf",
  17. product_code="MED-BASIC",
  18. content="疾病医疗等待期为30天,意外伤害不受等待期限制。",
  19. created_by="admin-01",
  20. )
  21. draft = service.create_document(
  22. title="尚未发布的内部说明",
  23. document_type="FAQ",
  24. source_name="内部说明.md",
  25. product_code=None,
  26. content="内部测试内容声称等待期为10天,但该内容尚未发布。",
  27. created_by="admin-01",
  28. )
  29. service.index_document(published["document_id"])
  30. service.test_search(published["document_id"], "医疗险等待期是多少天?")
  31. service.publish_document(published["document_id"])
  32. service.index_document(draft["document_id"])
  33. result = service.search("医疗险等待期是多少天?")
  34. assert result["total"] == 1
  35. assert result["items"][0]["document_id"] == published["document_id"]
  36. assert result["items"][0]["title"] == "安心医疗险保险条款"
  37. assert result["items"][0]["source_name"] == "安心医疗险条款.pdf"
  38. assert result["items"][0]["document_version"] == 1
  39. assert "30天" in result["items"][0]["content"]