- Rename project to stealergram throughout - Add pyproject.toml (replaces requirements.txt split, folds pytest.ini) - Replace all em-dashes with hyphens across all source files Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
"""
|
|
Tests for utils/cache.py - file-ID deduplication cache.
|
|
|
|
Each test gets an isolated cache file via the `isolated_cache` fixture
|
|
so tests never touch data/cache.json.
|
|
"""
|
|
|
|
import pytest
|
|
import utils.cache as cache_module
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def isolated_cache(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(cache_module, "CACHE_FILE", tmp_path / "cache.json")
|
|
|
|
|
|
def test_unseen_id_returns_false():
|
|
assert cache_module.is_seen(12345) is False
|
|
|
|
|
|
def test_mark_seen_makes_id_seen():
|
|
cache_module.mark_seen(12345)
|
|
assert cache_module.is_seen(12345) is True
|
|
|
|
|
|
def test_multiple_ids_stored_independently():
|
|
cache_module.mark_seen(1)
|
|
cache_module.mark_seen(2)
|
|
cache_module.mark_seen(3)
|
|
assert cache_module.is_seen(1)
|
|
assert cache_module.is_seen(2)
|
|
assert cache_module.is_seen(3)
|
|
assert not cache_module.is_seen(4)
|
|
|
|
|
|
def test_persists_to_disk_between_calls():
|
|
"""
|
|
is_seen() and mark_seen() each load from disk independently.
|
|
This verifies the persist-on-write / load-on-read contract
|
|
(simulating what happens across separate function calls in the bot loop).
|
|
"""
|
|
cache_module.mark_seen(999)
|
|
assert cache_module.is_seen(999) is True
|
|
|
|
|
|
def test_missing_cache_file_handled_gracefully(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(cache_module, "CACHE_FILE", tmp_path / "nonexistent.json")
|
|
assert cache_module.is_seen(42) is False
|
|
|
|
|
|
def test_mark_seen_is_idempotent():
|
|
cache_module.mark_seen(7)
|
|
cache_module.mark_seen(7)
|
|
cache_module.mark_seen(7)
|
|
assert cache_module.is_seen(7) is True
|