test_agent_memory.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from datetime import UTC, datetime
  2. import pytest
  3. from zbt.core.errors import AppError
  4. from zbt.domains.agent.memory import (
  5. CustomerMemoryService,
  6. InMemoryCustomerMemoryIndex,
  7. InMemoryCustomerMemoryRepository,
  8. )
  9. NOW = datetime(2026, 7, 28, 8, 0, tzinfo=UTC)
  10. def test_customer_memory_is_semantically_recalled_only_for_its_owner() -> None:
  11. service = CustomerMemoryService(
  12. InMemoryCustomerMemoryRepository(),
  13. InMemoryCustomerMemoryIndex(),
  14. lambda: NOW,
  15. )
  16. saved = service.save(
  17. owner_id="customer-a",
  18. category="PREFERENCE",
  19. content="我优先考虑给父母配置医疗险",
  20. )
  21. recalled = service.search(
  22. owner_id="customer-a",
  23. query="父母适合买什么医疗保险",
  24. limit=3,
  25. )
  26. other_customer = service.search(
  27. owner_id="customer-b",
  28. query="父母适合买什么医疗保险",
  29. limit=3,
  30. )
  31. assert recalled["items"][0]["memory_id"] == saved["memory_id"]
  32. assert recalled["items"][0]["content"] == "我优先考虑给父母配置医疗险"
  33. assert other_customer["items"] == []
  34. def test_customer_memory_rejects_sensitive_identifiers() -> None:
  35. service = CustomerMemoryService(
  36. InMemoryCustomerMemoryRepository(),
  37. InMemoryCustomerMemoryIndex(),
  38. lambda: NOW,
  39. )
  40. with pytest.raises(AppError) as error:
  41. service.save(
  42. owner_id="customer-a",
  43. category="PREFERENCE",
  44. content="请记住我的手机号是13800138000",
  45. )
  46. assert error.value.code == "CUSTOMER_MEMORY_SENSITIVE"