File size: 7,071 Bytes
e9bfb87
 
 
884a06d
 
 
e9bfb87
884a06d
 
 
 
 
 
 
e9bfb87
 
 
884a06d
e9bfb87
 
884a06d
 
 
 
 
e9bfb87
884a06d
 
 
 
 
 
e9bfb87
884a06d
 
 
 
 
 
 
 
 
 
 
e9bfb87
884a06d
 
 
 
 
 
 
 
e9bfb87
884a06d
 
 
 
e9bfb87
884a06d
 
e9bfb87
884a06d
 
 
 
 
 
 
 
 
 
e9bfb87
884a06d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9bfb87
884a06d
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Additional comprehensive tests for memory compressor to increase coverage.
"""

from datetime import datetime, timedelta

import pytest

from orka.memory.compressor import MemoryCompressor


class DummySummarizer:
    async def summarize(self, text: str) -> str:
        return f"SUMMARY: {text[:50]}..."


class TestMemoryCompressor:
    """Test memory compression functionality aligned with current API."""

    def test_compressor_initialization(self):
        """Test compressor initialization with defaults and custom params."""
        compressor_default = MemoryCompressor()
        assert compressor_default.max_entries == 1000
        assert compressor_default.importance_threshold == 0.3
        assert isinstance(compressor_default.time_window, timedelta)

        compressor_custom = MemoryCompressor(
            max_entries=5, importance_threshold=0.9, time_window=timedelta(hours=1)
        )
        assert compressor_custom.max_entries == 5
        assert compressor_custom.importance_threshold == 0.9
        assert compressor_custom.time_window == timedelta(hours=1)

    def test_should_compress_thresholds(self):
        """Validate the compression decision logic by size and importance."""
        compressor = MemoryCompressor(max_entries=3, importance_threshold=0.5)
        now = datetime.now()
        entries = [
            {"content": "a", "importance": 0.4, "timestamp": now},
            {"content": "b", "importance": 0.3, "timestamp": now},
            {"content": "c", "importance": 0.2, "timestamp": now},
            {"content": "d", "importance": 0.1, "timestamp": now},
        ]
        assert compressor.should_compress(entries) is True

        high_importance_entries = [
            {"content": "a", "importance": 0.9, "timestamp": now},
            {"content": "b", "importance": 0.8, "timestamp": now},
            {"content": "c", "importance": 0.7, "timestamp": now},
            {"content": "d", "importance": 0.6, "timestamp": now},
        ]
        # Too many entries but high mean importance → no compression
        assert compressor.should_compress(high_importance_entries) is False

        small_entries = [
            {"content": "a", "importance": 0.0, "timestamp": now},
        ]
        assert compressor.should_compress(small_entries) is False

    @pytest.mark.asyncio
    async def test_compress_no_entries(self):
        compressor = MemoryCompressor()
        result = await compressor.compress([], DummySummarizer())
        assert result == []

    @pytest.mark.asyncio
    async def test_compress_not_needed(self):
        compressor = MemoryCompressor(max_entries=10)
        now = datetime.now()
        entries = [
            {"content": "a", "importance": 0.9, "timestamp": now},
            {"content": "b", "importance": 0.8, "timestamp": now},
        ]
        result = await compressor.compress(entries, DummySummarizer())
        assert result == entries

    @pytest.mark.asyncio
    async def test_compress_summarizes_old_entries(self):
        # Force condition: many old entries beyond time window
        compressor = MemoryCompressor(
            max_entries=2, importance_threshold=0.9, time_window=timedelta(hours=1)
        )
        now = datetime.now()
        old_time = now - timedelta(days=2)
        entries = [
            {"content": "old1", "importance": 0.2, "timestamp": old_time},
            {"content": "old2", "importance": 0.2, "timestamp": old_time},
            {"content": "recent1", "importance": 0.2, "timestamp": now},
        ]
        result = await compressor.compress(entries, DummySummarizer())
        # Expect recent entries plus one summary
        assert any(e.get("is_summary") for e in result)
        assert any(e.get("timestamp") == now for e in result if not e.get("is_summary"))
        # Summary entry should have metadata and category
        summary_entries = [e for e in result if e.get("is_summary")]
        assert (
            summary_entries and summary_entries[0]["metadata"]["is_summary"] is True
            if isinstance(summary_entries[0].get("metadata"), dict)
            else True
        )
        assert summary_entries[0]["category"] == "summary"

    @pytest.mark.asyncio
    async def test_compress_when_no_old_but_over_max(self):
        # When over max_entries but all timestamps are recent, compressor should split by size
        compressor = MemoryCompressor(
            max_entries=2, importance_threshold=0.4, time_window=timedelta(days=30)
        )
        now = datetime.now()
        entries = [{"content": f"c{i}", "importance": 0.1, "timestamp": now} for i in range(4)]
        result = await compressor.compress(entries, DummySummarizer())
        assert any(e.get("is_summary") for e in result)
        assert len(result) <= 3  # 2 recent + 1 summary

    @pytest.mark.asyncio
    async def test_compress_invalid_summarizer_raises(self):
        compressor = MemoryCompressor(max_entries=1, importance_threshold=1.0)
        now = datetime.now()
        entries = [{"content": "x", "importance": 0.0, "timestamp": now} for _ in range(5)]

        class BadSummarizer:
            pass

        with pytest.raises(ValueError):
            await compressor.compress(entries, BadSummarizer())

    @pytest.mark.asyncio
    async def test_create_summary_uses_summarize(self):
        # Force compression: many entries, low importance, old timestamps
        compressor = MemoryCompressor(max_entries=2, importance_threshold=0.5, time_window=timedelta(hours=1))
        old_time = datetime.now() - timedelta(days=10)
        entries = [
            {"content": "a", "importance": 0.1, "timestamp": old_time},
            {"content": "b", "importance": 0.1, "timestamp": old_time},
            {"content": "c", "importance": 0.1, "timestamp": old_time},
            {"content": "d", "importance": 0.1, "timestamp": old_time},
        ]
        # Verify compression is triggered
        assert compressor.should_compress(entries) is True
        result = await compressor.compress(entries, DummySummarizer())
        # Should have at least one summary entry
        assert any(e.get("is_summary") for e in result), f"No summary found in result: {result}"

    @pytest.mark.asyncio
    async def test_create_summary_with_generate(self):
        class GenSummarizer:
            async def generate(self, prompt: str) -> str:
                return "SUMMARY VIA GENERATE"

        compressor = MemoryCompressor(max_entries=2, importance_threshold=0.5, time_window=timedelta(hours=1))
        old_time = datetime.now() - timedelta(days=10)
        entries = [
            {"content": "a", "importance": 0.1, "timestamp": old_time},
            {"content": "b", "importance": 0.1, "timestamp": old_time},
            {"content": "c", "importance": 0.1, "timestamp": old_time},
        ]
        assert compressor.should_compress(entries) is True
        result = await compressor.compress(entries, GenSummarizer())
        assert any(e.get("is_summary") for e in result), f"No summary found in result: {result}"