from __future__ import annotations import time from yuxi.channels.adapters.twitch.outbound_cache import OutboundCacheManager class TestOutboundCacheManager: def test_record_stores_entry(self): cache = OutboundCacheManager() cache.record("#channel", "Hello world") entries = cache.get_all() assert len(entries) == 1 assert entries[0]["channel"] == "#channel" assert entries[0]["content"] == "Hello world" def test_record_with_message_id(self): cache = OutboundCacheManager() cache.record("#channel", "Hello", message_id="msg_123") entries = cache.get_all() assert entries[0]["message_id"] == "msg_123" def test_record_returns_cache_key(self): cache = OutboundCacheManager() key = cache.record("#channel", "Hello", message_id="msg_123") assert key == "msg_123" def test_record_returns_auto_key_without_message_id(self): cache = OutboundCacheManager() key = cache.record("#channel", "Hello") assert ":" in key def test_multiple_records(self): cache = OutboundCacheManager() cache.record("#ch1", "msg1", message_id="id1") cache.record("#ch2", "msg2", message_id="id2") cache.record("#ch3", "msg3", message_id="id3") assert len(cache.get_all()) == 3 def test_find_by_message_id_found(self): cache = OutboundCacheManager() cache.record("#channel", "Hello", message_id="msg_123") entry = cache.find_by_message_id("msg_123") assert entry is not None assert entry["content"] == "Hello" def test_find_by_message_id_not_found(self): cache = OutboundCacheManager() cache.record("#channel", "Hello", message_id="msg_123") entry = cache.find_by_message_id("nonexistent") assert entry is None def test_find_by_message_id_without_message_id(self): cache = OutboundCacheManager() cache.record("#channel", "Hello") entry = cache.find_by_message_id("any_id") assert entry is None def test_max_size_enforcement(self): cache = OutboundCacheManager(max_size=3) for i in range(5): cache.record("#ch", f"msg{i}", message_id=f"id{i}") entries = cache.get_all() assert len(entries) == 3 def test_oldest_entry_evicted(self): cache = OutboundCacheManager(max_size=3) cache.record("#ch", "first", message_id="id1") cache.record("#ch", "second", message_id="id2") cache.record("#ch", "third", message_id="id3") cache.record("#ch", "fourth", message_id="id4") entries = cache.get_all() contents = [e["content"] for e in entries] assert "first" not in contents def test_get_all_empty(self): cache = OutboundCacheManager() assert cache.get_all() == [] def test_record_timestamp_present(self): cache = OutboundCacheManager() cache.record("#ch", "msg") entry = cache.get_all()[0] assert "timestamp" in entry assert isinstance(entry["timestamp"], float)