supermemory/packages/cartesia-sdk-python/tests/test_empty_profile.py
2026-09-16 17:49:20 +02:00

129 lines
3.8 KiB
Python

from __future__ import annotations
import sys
import types
import unittest
from types import SimpleNamespace
from unittest.mock import AsyncMock
def _install_test_stubs() -> None:
if "loguru" not in sys.modules:
loguru_module = types.ModuleType("loguru")
class _Logger:
def info(self, *_args, **_kwargs):
return None
def warning(self, *_args, **_kwargs):
return None
def error(self, *_args, **_kwargs):
return None
loguru_module.logger = _Logger()
sys.modules["loguru"] = loguru_module
if "pydantic" not in sys.modules:
pydantic_module = types.ModuleType("pydantic")
class BaseModel:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def Field(*, default=None, **_kwargs):
return default
pydantic_module.BaseModel = BaseModel
pydantic_module.Field = Field
sys.modules["pydantic"] = pydantic_module
_install_test_stubs()
from supermemory_cartesia.agent import SupermemoryCartesiaAgent
class _MockSupermemoryClient:
def __init__(self, response):
self.profile = AsyncMock(return_value=response)
class TestSupermemoryCartesiaNullProfile(unittest.IsolatedAsyncioTestCase):
async def test_retrieve_memories_handles_null_profile(self) -> None:
agent = SupermemoryCartesiaAgent(
agent=SimpleNamespace(),
api_key="mock_key",
container_tag="user-123",
custom_id="conversation-456",
)
response = SimpleNamespace(profile=None, search_results=None)
agent._supermemory_client = _MockSupermemoryClient(response)
result = await agent._retrieve_memories("Hello world")
self.assertEqual(
result,
{
"profile": {"static": [], "dynamic": []},
"search_results": [],
},
)
def test_query_mode_keeps_search_fact_also_present_in_profile(self) -> None:
fact = "User likes machine learning projects"
agent = SupermemoryCartesiaAgent(
agent=SimpleNamespace(),
api_key="mock_key",
container_tag="user-123",
custom_id="conversation-456",
config=SupermemoryCartesiaAgent.MemoryConfig(mode="query"),
)
context = agent._build_memory_message(
{
"profile": {"static": [fact], "dynamic": []},
"search_results": [SimpleNamespace(memory=fact)],
}
)
self.assertIsNotNone(context)
self.assertIn(fact, context)
async def test_process_does_not_retry_agent_after_output(self) -> None:
class UserTurnEnded:
pass
class FailingAfterOutputAgent:
def __init__(self) -> None:
self.calls = 0
async def process(self, _env, _event, **_kwargs):
self.calls += 1
yield "partial"
raise RuntimeError("agent failed after output")
wrapped_agent = FailingAfterOutputAgent()
agent = SupermemoryCartesiaAgent(
agent=wrapped_agent,
api_key="mock_key",
container_tag="user-123",
custom_id="conversation-456",
add_memory="never",
)
event = UserTurnEnded()
agent._enrich_event_with_memories = AsyncMock(return_value=(event, None))
outputs = []
with self.assertRaisesRegex(RuntimeError, "agent failed after output"):
async for output in agent.process(None, event):
outputs.append(output)
self.assertEqual(outputs, ["partial"])
self.assertEqual(wrapped_agent.calls, 1)
if __name__ == "__main__":
unittest.main()