新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块 同时在测试配置中添加了测试用的OpenAI API密钥环境变量
165 lines
5.8 KiB
Python
165 lines
5.8 KiB
Python
from __future__ import annotations
|
|
|
|
from yuxi.channels.models import ChannelAccountSnapshot, build_snapshot_from_adapter
|
|
|
|
|
|
class TestChannelAccountSnapshot:
|
|
def test_default_instantiation(self):
|
|
snap = ChannelAccountSnapshot()
|
|
assert snap.account_id == ""
|
|
assert snap.name == ""
|
|
assert snap.configured is False
|
|
assert snap.enabled is True
|
|
assert snap.linked is False
|
|
assert snap.running is False
|
|
assert snap.connected is False
|
|
assert snap.status_state == "not-configured"
|
|
assert snap.health_state == "stopped"
|
|
|
|
def test_custom_values(self):
|
|
snap = ChannelAccountSnapshot(
|
|
account_id="account-1",
|
|
name="测试账户",
|
|
configured=True,
|
|
enabled=True,
|
|
linked=True,
|
|
running=True,
|
|
connected=True,
|
|
status_state="linked",
|
|
health_state="running",
|
|
last_error="connection timeout",
|
|
reconnect_attempts=3,
|
|
dm_policy="pairing",
|
|
group_policy="allowlist",
|
|
allow_from_count=5,
|
|
busy=False,
|
|
active_runs=2,
|
|
)
|
|
assert snap.account_id == "account-1"
|
|
assert snap.name == "测试账户"
|
|
assert snap.configured is True
|
|
assert snap.linked is True
|
|
assert snap.running is True
|
|
assert snap.connected is True
|
|
assert snap.status_state == "linked"
|
|
assert snap.health_state == "running"
|
|
assert snap.last_error == "connection timeout"
|
|
assert snap.reconnect_attempts == 3
|
|
assert snap.dm_policy == "pairing"
|
|
assert snap.group_policy == "allowlist"
|
|
assert snap.allow_from_count == 5
|
|
assert snap.busy is False
|
|
assert snap.active_runs == 2
|
|
|
|
def test_timeline_fields_default_none(self):
|
|
snap = ChannelAccountSnapshot()
|
|
assert snap.last_start_at is None
|
|
assert snap.last_stop_at is None
|
|
assert snap.last_connected_at_s is None
|
|
assert snap.last_message_at is None
|
|
assert snap.last_event_at is None
|
|
assert snap.last_inbound_at is None
|
|
assert snap.last_outbound_at is None
|
|
assert snap.last_transport_activity_at is None
|
|
|
|
def test_timeline_fields_with_values(self):
|
|
snap = ChannelAccountSnapshot(
|
|
last_start_at=1000.0,
|
|
last_stop_at=2000.0,
|
|
last_connected_at_s=3000.0,
|
|
last_message_at=4000.0,
|
|
last_event_at=5000.0,
|
|
last_inbound_at=6000.0,
|
|
last_outbound_at=7000.0,
|
|
last_transport_activity_at=8000.0,
|
|
)
|
|
assert snap.last_start_at == 1000.0
|
|
assert snap.last_stop_at == 2000.0
|
|
assert snap.last_connected_at_s == 3000.0
|
|
assert snap.last_message_at == 4000.0
|
|
assert snap.last_event_at == 5000.0
|
|
assert snap.last_inbound_at == 6000.0
|
|
assert snap.last_outbound_at == 7000.0
|
|
assert snap.last_transport_activity_at == 8000.0
|
|
|
|
def test_last_disconnect_default_none(self):
|
|
snap = ChannelAccountSnapshot()
|
|
assert snap.last_disconnect is None
|
|
|
|
def test_extra_fields_allowed(self):
|
|
snap = ChannelAccountSnapshot(account_id="test", custom_field="custom_value")
|
|
assert snap.custom_field == "custom_value"
|
|
|
|
|
|
class TestBuildSnapshotFromAdapter:
|
|
def test_reads_all_fields_from_adapter(self):
|
|
class MockAdapter:
|
|
account_id = "adapter-1"
|
|
account_name = "MockAdapter"
|
|
_token_mgr = object()
|
|
_enabled = True
|
|
_linked = True
|
|
_running = True
|
|
_connected = True
|
|
_status_state = "linked"
|
|
_status = None
|
|
_last_connected_at = 1700000000.0
|
|
_last_message_at = 1700001000.0
|
|
_last_error = "test error"
|
|
_reconnect_attempts = 5
|
|
dm_policy = "pairing"
|
|
group_policy = "allowlist"
|
|
_busy = False
|
|
_active_runs = 3
|
|
|
|
snap = build_snapshot_from_adapter(MockAdapter())
|
|
assert snap.account_id == "adapter-1"
|
|
assert snap.name == "MockAdapter"
|
|
assert snap.configured is True
|
|
assert snap.linked is True
|
|
assert snap.running is True
|
|
assert snap.connected is True
|
|
assert snap.status_state == "linked"
|
|
assert snap.health_state == "stopped"
|
|
assert snap.last_connected_at_s == 1700000000.0
|
|
assert snap.last_message_at == 1700001000.0
|
|
assert snap.last_error == "test error"
|
|
assert snap.reconnect_attempts == 5
|
|
assert snap.dm_policy == "pairing"
|
|
assert snap.group_policy == "allowlist"
|
|
assert snap.busy is False
|
|
assert snap.active_runs == 3
|
|
|
|
def test_fallback_when_attributes_missing(self):
|
|
class BareAdapter:
|
|
pass
|
|
|
|
snap = build_snapshot_from_adapter(BareAdapter())
|
|
assert snap.account_id == ""
|
|
assert snap.name == ""
|
|
assert snap.configured is False
|
|
assert snap.enabled is True
|
|
assert snap.linked is False
|
|
assert snap.running is False
|
|
assert snap.connected is False
|
|
assert snap.status_state == "not-configured"
|
|
assert snap.health_state == "stopped"
|
|
assert snap.reconnect_attempts == 0
|
|
assert snap.busy is False
|
|
assert snap.active_runs == 0
|
|
assert snap.dm_policy == "allowlist"
|
|
assert snap.group_policy == "allowlist"
|
|
|
|
def test_status_enum_value_mapped_to_health_state(self):
|
|
from enum import Enum
|
|
|
|
class Status(Enum):
|
|
RUNNING = "running"
|
|
ERROR = "error"
|
|
|
|
class AdapterWithEnumStatus:
|
|
_status = Status.ERROR
|
|
_token_mgr = None
|
|
|
|
snap = build_snapshot_from_adapter(AdapterWithEnumStatus())
|
|
assert snap.health_state == "error" |