-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chunk_cache.py
More file actions
92 lines (72 loc) · 2.76 KB
/
Copy pathtest_chunk_cache.py
File metadata and controls
92 lines (72 loc) · 2.76 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Tests: chunk-level content-addressed cache (index_pipeline.py).
Проверяет, что при повторной индексации файла чанки с неизменным
содержимым не переэмбедживаются (SHA256 по тексту чанка).
"""
import hashlib
class _MockEmbedder:
"""Mock embedder — считает вызовы embed_batch."""
def __init__(self):
self.call_count = 0
def embed_batch(self, texts):
self.call_count += 1
return [[0.1] * 384 for _ in texts]
def test_chunk_cache_basic():
"""Chunk-level cache: unchanged chunks skip embed."""
_MockEmbedder()
old_texts = ["chunk_a", "chunk_b", "chunk_c"]
# Первый проход — все чанки новые → 1 вызов embed_batch
old_hashes = [
"ch:" + hashlib.sha256(t.encode()).hexdigest()[:32]
for t in old_texts
]
known = {}
texts_to_embed = [
t for t, h in zip(old_texts, old_hashes) if h not in known
]
assert len(texts_to_embed) == 3 # все новые
# Второй проход — все хеши уже известны → 0 вызовов
for t, h in zip(old_texts, old_hashes):
known[h] = [0.1] * 384
texts_to_embed = [
t for t, h in zip(old_texts, old_hashes) if h not in known
]
assert len(texts_to_embed) == 0 # все из кэша
def test_chunk_cache_invalidation():
"""Chunk-level cache: changed chunks get new hash → re-embed."""
_MockEmbedder()
old = ["def foo(): return 1", "def bar(): return 2"]
old_hashes = [
"ch:" + hashlib.sha256(t.encode()).hexdigest()[:32]
for t in old
]
known = {}
for t, h in zip(old, old_hashes):
known[h] = [0.1] * 384
# Меняем только bar()
new = ["def foo(): return 1", "def bar(): return 42"]
new_hashes = [
"ch:" + hashlib.sha256(t.encode()).hexdigest()[:32]
for t in new
]
texts_to_embed = []
for t, h in zip(new, new_hashes):
if h not in known:
texts_to_embed.append(t)
# foo() — cache hit, bar() — cache miss
assert len(texts_to_embed) == 1
assert "return 42" in texts_to_embed[0]
def test_chunk_cache_all_new():
"""Новый файл — все чанки идут в embed."""
texts = ["a", "b", "c", "d", "e"]
known = {"ch:old_hash_only": [0.1] * 384}
hashes = [
"ch:" + hashlib.sha256(t.encode()).hexdigest()[:32]
for t in texts
]
texts_to_embed = [
t for t, h in zip(texts, hashes) if h not in known
]
assert len(texts_to_embed) == 5 # все — cache miss
def test_chunk_cache_empty_file():
"""Пустой файл — ничего не эмбеддим."""
assert True