import pytest from unittest.mock import AsyncMock @pytest.fixture def mock_col(): return AsyncMock() @pytest.fixture def svc(mock_col): from app.services.user_memory import UserMemoryService instance = UserMemoryService.__new__(UserMemoryService) instance._collection = mock_col instance._ready = True return instance @pytest.mark.asyncio async def test_load_returns_document(svc, mock_col): mock_col.find_one.return_value = {"_id": "u1", "summary": "loves beach trips"} result = await svc.load("u1") assert result["summary"] == "loves beach trips" mock_col.find_one.assert_called_once_with({"_id": "u1"}) @pytest.mark.asyncio async def test_load_returns_none_for_new_user(svc, mock_col): mock_col.find_one.return_value = None result = await svc.load("new_user") assert result is None @pytest.mark.asyncio async def test_load_returns_none_when_not_ready(svc, mock_col): svc._ready = False result = await svc.load("u1") assert result is None mock_col.find_one.assert_not_called() @pytest.mark.asyncio async def test_update_slots_sets_last_destination(svc, mock_col): await svc.update_slots("u1", entities={}, context={"destination_name": "Đà Nẵng"}) mock_col.update_one.assert_called() update_doc = mock_col.update_one.call_args_list[0][0][1] assert update_doc["$set"]["lastDestination"] == "Đà Nẵng" @pytest.mark.asyncio async def test_update_slots_noop_when_not_ready(svc, mock_col): svc._ready = False await svc.update_slots("u1", entities={}, context={"destination_name": "Đà Nẵng"}) mock_col.update_one.assert_not_called() @pytest.mark.asyncio async def test_update_slots_sets_budget(svc, mock_col): await svc.update_slots( "u1", entities={"budget": {"amount": 5000000, "currency": "VND", "level": "mid"}}, context={}, ) mock_col.update_one.assert_called() update_doc = mock_col.update_one.call_args_list[0][0][1] assert update_doc["$set"]["structured.budget_range"]["amount"] == 5000000