103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import time
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from yuxi.channels.adapters.nostr.send_cache import SendCache, SendCacheEntry
|
||
|
|
|
||
|
|
|
||
|
|
class TestSendCache:
|
||
|
|
def test_record_new_entry(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
cache.record("event_001", "hello world", "sent")
|
||
|
|
assert len(cache) == 1
|
||
|
|
|
||
|
|
def test_record_updates_existing(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
cache.record("event_001", "hello", "sent")
|
||
|
|
cache.record("event_001", "hello updated", "delivered")
|
||
|
|
assert len(cache) == 1
|
||
|
|
entry = cache.get("event_001")
|
||
|
|
assert entry.status == "delivered"
|
||
|
|
|
||
|
|
def test_max_size_eviction(self):
|
||
|
|
cache = SendCache(max_size=3)
|
||
|
|
for i in range(5):
|
||
|
|
cache.record(f"event_{i}", f"content_{i}", "sent")
|
||
|
|
assert len(cache) == 3
|
||
|
|
assert cache.get("event_0") is None
|
||
|
|
assert cache.get("event_2") is not None
|
||
|
|
|
||
|
|
def test_get_nonexistent(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
assert cache.get("nonexistent") is None
|
||
|
|
|
||
|
|
def test_update_status(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
cache.record("event_001", "content", "sent")
|
||
|
|
cache.update_status("event_001", "delivered")
|
||
|
|
entry = cache.get("event_001")
|
||
|
|
assert entry.status == "delivered"
|
||
|
|
|
||
|
|
def test_update_status_nonexistent(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
cache.update_status("nonexistent", "delivered")
|
||
|
|
|
||
|
|
def test_list_recent(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
for i in range(15):
|
||
|
|
cache.record(f"event_{i}", f"content_{i}")
|
||
|
|
recent = cache.list_recent(limit=5)
|
||
|
|
assert len(recent) == 5
|
||
|
|
assert recent[-1].event_id == "event_14"
|
||
|
|
|
||
|
|
def test_list_recent_default_limit(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
for i in range(5):
|
||
|
|
cache.record(f"event_{i}", f"content_{i}")
|
||
|
|
recent = cache.list_recent()
|
||
|
|
assert len(recent) == 5
|
||
|
|
|
||
|
|
def test_to_dict_list(self):
|
||
|
|
cache = SendCache(max_size=10)
|
||
|
|
cache.record("event_a", "content a", "sent")
|
||
|
|
cache.record("event_b", "content b", "delivered")
|
||
|
|
entries = cache.to_dict_list()
|
||
|
|
assert len(entries) == 2
|
||
|
|
assert entries[0]["content"] == "content b"
|
||
|
|
|
||
|
|
def test_from_dict_list(self):
|
||
|
|
data = [
|
||
|
|
{"event_id": "e1", "content": "hello", "status": "sent", "timestamp": time.time()},
|
||
|
|
{"event_id": "e2", "content": "world", "status": "delivered", "timestamp": time.time()},
|
||
|
|
]
|
||
|
|
cache = SendCache.from_dict_list(data, max_size=10)
|
||
|
|
assert len(cache) == 2
|
||
|
|
assert cache.get("e1").content == "hello"
|
||
|
|
|
||
|
|
def test_from_dict_list_max_size(self):
|
||
|
|
data = [
|
||
|
|
{"event_id": f"e{i}", "content": f"c{i}", "status": "sent", "timestamp": time.time()}
|
||
|
|
for i in range(10)
|
||
|
|
]
|
||
|
|
cache = SendCache.from_dict_list(data, max_size=3)
|
||
|
|
assert len(cache) == 10
|
||
|
|
|
||
|
|
def test_from_dict_list_empty(self):
|
||
|
|
cache = SendCache.from_dict_list([], max_size=10)
|
||
|
|
assert len(cache) == 0
|
||
|
|
|
||
|
|
def test_from_dict_list_missing_fields(self):
|
||
|
|
data = [{"event_id": "e1"}]
|
||
|
|
cache = SendCache.from_dict_list(data, max_size=10)
|
||
|
|
assert len(cache) == 1
|
||
|
|
entry = cache.get("e1")
|
||
|
|
assert entry.content == ""
|
||
|
|
assert entry.status == "sent"
|
||
|
|
|
||
|
|
def test_size_property(self):
|
||
|
|
cache = SendCache(max_size=100)
|
||
|
|
for i in range(50):
|
||
|
|
cache.record(f"event_{i}", f"content_{i}")
|
||
|
|
assert len(cache) == 50
|