Files
manual_slop/tests/test_summary_cache.py
T

76 lines
2.2 KiB
Python

import os
import shutil
from pathlib import Path
from src.summary_cache import SummaryCache, get_file_hash
def test_get_file_hash():
content = "hello world"
# sha256 of "hello world"
expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
assert get_file_hash(content) == expected
def test_summary_cache():
cache_dir = Path(".test_cache")
if cache_dir.exists():
shutil.rmtree(cache_dir)
cache_file = cache_dir / "cache.json"
cache = SummaryCache(str(cache_file))
file_path = "test.py"
content = "print('hello')"
content_hash = get_file_hash(content)
summary = "**Python** - 1 lines"
# Test empty cache
assert cache.get_summary(file_path, content_hash) is None
# Test set and get
cache.set_summary(file_path, content_hash, summary)
assert cache.get_summary(file_path, content_hash) == summary
# Test cache invalidation
assert cache.get_summary(file_path, "different_hash") is None
# Test persistence
cache2 = SummaryCache(str(cache_file))
assert cache2.get_summary(file_path, content_hash) == summary
# Cleanup
if cache_dir.exists():
shutil.rmtree(cache_dir)
def test_summary_cache_lru():
cache_dir = Path(".test_cache_lru")
if cache_dir.exists():
shutil.rmtree(cache_dir)
cache_file = cache_dir / "cache.json"
# Create cache with max 2 entries
cache = SummaryCache(str(cache_file), max_entries=2)
cache.set_summary("file1.py", "hash1", "summary1")
cache.set_summary("file2.py", "hash2", "summary2")
cache.set_summary("file3.py", "hash3", "summary3") # This should evict file1.py
assert cache.get_summary("file1.py", "hash1") is None
assert cache.get_summary("file2.py", "hash2") == "summary2"
assert cache.get_summary("file3.py", "hash3") == "summary3"
# Access file2.py, then add file4.py. file3.py should be evicted
cache.get_summary("file2.py", "hash2")
cache.set_summary("file4.py", "hash4", "summary4")
assert cache.get_summary("file3.py", "hash3") is None
assert cache.get_summary("file2.py", "hash2") == "summary2"
assert cache.get_summary("file4.py", "hash4") == "summary4"
if cache_dir.exists():
shutil.rmtree(cache_dir)
if __name__ == "__main__":
test_get_file_hash()
test_summary_cache()
test_summary_cache_lru()
print("Tests passed!")