File size: 3,843 Bytes
af9cde9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Tests for the memory notes extraction module."""

import pytest
from unittest.mock import AsyncMock, MagicMock, patch

from reachy_mini_conversation_app.memory_notes import (
    NotesManager,
    SECTION_KEY_FACTS,
    SECTION_ENTITIES,
    SECTION_PREFERENCES,
    SECTION_THREADS,
)


class TestNotesManagerDatabase:
    """Test NotesManager database interactions (no LLM calls)."""

    def test_get_context_empty(self, tmp_path):
        """Test context generation with no notes."""
        from reachy_mini_conversation_app.database import MiniMinderDB

        db = MiniMinderDB(tmp_path / "test.db")
        manager = NotesManager(db)

        context = manager.get_context_for_prompt()
        assert context == ""

        db.close()

    def test_get_context_with_notes(self, tmp_path):
        """Test context generation with existing notes."""
        from reachy_mini_conversation_app.database import MiniMinderDB

        db = MiniMinderDB(tmp_path / "test.db")

        # Add some notes directly to database
        db.add_note(SECTION_KEY_FACTS, "Takes Topiramate at 8am daily")
        db.add_note(SECTION_PREFERENCES, "Prefers quiet voice after migraines")
        db.add_note(SECTION_ENTITIES, "Maya - daughter, lives remotely")

        manager = NotesManager(db)
        context = manager.get_context_for_prompt()

        assert "What I Remember About You" in context
        assert "Topiramate" in context
        assert "quiet voice" in context
        assert "Maya" in context

        db.close()

    def test_get_preferences(self, tmp_path):
        """Test retrieving user preferences."""
        from reachy_mini_conversation_app.database import MiniMinderDB

        db = MiniMinderDB(tmp_path / "test.db")
        db.add_note(SECTION_PREFERENCES, "Likes morning check-ins")
        db.add_note(SECTION_PREFERENCES, "Prefers British English")

        manager = NotesManager(db)
        prefs = manager.get_preferences()

        assert len(prefs) == 2
        assert "morning check-ins" in prefs[0] or "morning check-ins" in prefs[1]

        db.close()


class TestNotesManagerExtraction:
    """Test NotesManager LLM extraction (mocked)."""

    @pytest.mark.asyncio
    async def test_extract_and_save(self, tmp_path):
        """Test extraction with mocked LLM response."""
        from reachy_mini_conversation_app.database import MiniMinderDB

        db = MiniMinderDB(tmp_path / "test.db")
        manager = NotesManager(db)

        # Mock the LLM response
        mock_response = MagicMock()
        mock_response.content = """```json
{
    "key_facts": ["Takes medication at 8am"],
    "entities": ["Maya - daughter"],
    "preferences": ["Prefers quiet reminders"],
    "threads": []
}
```"""

        with patch.object(manager, "_get_llm") as mock_llm:
            mock_llm.return_value.ainvoke = AsyncMock(return_value=mock_response)

            conversation = [
                {"role": "user", "content": "I take my medication at 8am"},
                {"role": "assistant", "content": "I'll remember that."},
            ]

            result = await manager.extract_and_save(conversation, session_id="test123")

        assert "key_facts" in result
        assert len(result["key_facts"]) == 1
        assert "8am" in result["key_facts"][0]

        # Verify notes were saved to database
        notes = db.get_notes()
        assert len(notes) >= 1

        db.close()

    @pytest.mark.asyncio
    async def test_extract_empty_conversation(self, tmp_path):
        """Test extraction with empty conversation."""
        from reachy_mini_conversation_app.database import MiniMinderDB

        db = MiniMinderDB(tmp_path / "test.db")
        manager = NotesManager(db)

        result = await manager.extract_and_save([], session_id="test")
        assert result == {}

        db.close()