from __future__ import annotations import time import pytest from yuxi.channels.adapters.telegram import streaming class TestStreamingFixes: def test_ttl_constant_exists(self): assert streaming._STREAM_TTL_SECONDS == 300 @pytest.fixture(autouse=True) def _clean_storage(self): streaming._STREAM_STORAGE.clear() yield streaming._STREAM_STORAGE.clear() def test_cleanup_removes_expired_entries(self): streaming._STREAM_STORAGE["test:1"] = { "message_id": 1, "last_edit": time.monotonic() - 1000, "counter": 0, "created_at": time.monotonic() - 1000, } streaming._cleanup_expired(time.monotonic()) assert "test:1" not in streaming._STREAM_STORAGE def test_cleanup_keeps_fresh_entries(self): now = time.monotonic() streaming._STREAM_STORAGE["test:2"] = { "message_id": 2, "last_edit": now, "counter": 0, "created_at": now, } streaming._cleanup_expired(now) assert "test:2" in streaming._STREAM_STORAGE def test_cleanup_handles_multiple_entries(self): now = time.monotonic() streaming._STREAM_STORAGE["test:old1"] = {"created_at": now - 1000} streaming._STREAM_STORAGE["test:old2"] = {"created_at": now - 500} streaming._STREAM_STORAGE["test:new"] = {"created_at": now} streaming._cleanup_expired(now) assert "test:old1" not in streaming._STREAM_STORAGE assert "test:old2" not in streaming._STREAM_STORAGE assert "test:new" in streaming._STREAM_STORAGE def test_cleanup_removes_entries_without_created_at(self): now = time.monotonic() streaming._STREAM_STORAGE["test:no_created"] = { "message_id": 1, "last_edit": now, "counter": 0, } streaming._cleanup_expired(now) assert "test:no_created" not in streaming._STREAM_STORAGE