-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_context_analysis_cache.py
More file actions
68 lines (57 loc) · 1.97 KB
/
Copy pathtest_context_analysis_cache.py
File metadata and controls
68 lines (57 loc) · 1.97 KB
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
import os
import unittest
from unittest.mock import patch
from services.context_analysis_cache import ContextAnalysisCache
class ContextAnalysisCacheTest(unittest.TestCase):
def setUp(self):
ContextAnalysisCache.clear()
def test_content_hash_cache_reuses_span_without_retaining_source_text(self):
source = "private roleplay context " * 80
calls = 0
def analyze():
nonlocal calls
calls += 1
return (12, len(source))
first, first_status = ContextAnalysisCache.image_prompt_span(
"assistant",
source,
analyze,
)
second, second_status = ContextAnalysisCache.image_prompt_span(
"assistant",
source,
analyze,
)
self.assertEqual(first, second)
self.assertEqual(first_status, "miss")
self.assertEqual(second_status, "hit")
self.assertEqual(calls, 1)
self.assertNotIn(source, repr(ContextAnalysisCache._entries))
self.assertTrue(
all(len(key) == 64 for key in ContextAnalysisCache._entries)
)
def test_content_change_invalidates_and_disabled_cache_bypasses(self):
_, first_status = ContextAnalysisCache.image_prompt_span(
"assistant",
"first content",
lambda: None,
)
_, changed_status = ContextAnalysisCache.image_prompt_span(
"assistant",
"changed content",
lambda: None,
)
with patch.dict(
os.environ,
{"CONTEXT_ANALYSIS_CACHE_ENABLED": "false"},
):
_, bypass_status = ContextAnalysisCache.image_prompt_span(
"assistant",
"first content",
lambda: None,
)
self.assertEqual(first_status, "miss")
self.assertEqual(changed_status, "miss")
self.assertEqual(bypass_status, "bypass")
if __name__ == "__main__":
unittest.main()