ForcePilot/backend/test/unit/channels/test_telegram_streaming_new.py
Kris 3264900bc9 test: 新增多渠道单元测试用例并配置测试环境变量
新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块
同时在测试配置中添加了测试用的OpenAI API密钥环境变量
2026-05-12 00:56:47 +08:00

59 lines
1.9 KiB
Python

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