162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from yuxi.channels.adapters.twitch.session import (
|
||
|
|
check_group_policy,
|
||
|
|
resolve_agent_id_for_channel,
|
||
|
|
resolve_channel_name,
|
||
|
|
resolve_route,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveChannelName:
|
||
|
|
def test_strips_hash_prefix(self):
|
||
|
|
assert resolve_channel_name("#test_channel") == "test_channel"
|
||
|
|
|
||
|
|
def test_no_hash_unchanged(self):
|
||
|
|
assert resolve_channel_name("test_channel") == "test_channel"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveRoute:
|
||
|
|
def test_resolves_route_format(self):
|
||
|
|
route = resolve_route("agent_1", "test_channel")
|
||
|
|
assert route == "agent:agent_1:twitch:channel:test_channel"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveAgentIdForChannel:
|
||
|
|
def test_channel_specific_agent(self):
|
||
|
|
config = {
|
||
|
|
"channels_config": {
|
||
|
|
"test_channel": {"agent_id": "agent_42"},
|
||
|
|
"other_channel": {"agent_id": "agent_99"},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result = resolve_agent_id_for_channel(config, "test_channel", "default_agent")
|
||
|
|
assert result == "agent_42"
|
||
|
|
|
||
|
|
def test_falls_back_to_default(self):
|
||
|
|
config = {"channels_config": {}}
|
||
|
|
result = resolve_agent_id_for_channel(config, "test_channel", "default_agent")
|
||
|
|
assert result == "default_agent"
|
||
|
|
|
||
|
|
def test_no_config_no_default_returns_empty(self):
|
||
|
|
config = {}
|
||
|
|
result = resolve_agent_id_for_channel(config, "test_channel")
|
||
|
|
assert result == ""
|
||
|
|
|
||
|
|
|
||
|
|
class TestCheckGroupPolicyOpen:
|
||
|
|
def test_open_policy_always_true(self):
|
||
|
|
config = {"group_policy": "open"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is True
|
||
|
|
|
||
|
|
def test_open_policy_with_mention(self):
|
||
|
|
config = {"group_policy": "open"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "@bot hello") is True
|
||
|
|
|
||
|
|
|
||
|
|
class TestCheckGroupPolicyDisabled:
|
||
|
|
def test_disabled_policy_always_false(self):
|
||
|
|
config = {"group_policy": "disabled"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
|
||
|
|
class TestCheckGroupPolicyMentionOnly:
|
||
|
|
def test_mention_only_with_mention(self):
|
||
|
|
config = {"group_policy": "mention_only", "bot_username": "mybot"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello @mybot") is True
|
||
|
|
|
||
|
|
def test_mention_only_without_mention(self):
|
||
|
|
config = {"group_policy": "mention_only", "bot_username": "mybot"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
def test_mention_only_no_bot_username(self):
|
||
|
|
config = {"group_policy": "mention_only", "bot_username": ""}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello @mybot") is False
|
||
|
|
|
||
|
|
def test_mention_only_require_mention_false(self):
|
||
|
|
config = {
|
||
|
|
"group_policy": "mention_only",
|
||
|
|
"bot_username": "mybot",
|
||
|
|
"require_mention": False,
|
||
|
|
}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is True
|
||
|
|
|
||
|
|
def test_mention_only_case_insensitive(self):
|
||
|
|
config = {"group_policy": "mention_only", "bot_username": "MyBot"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello @mybot") is True
|
||
|
|
|
||
|
|
|
||
|
|
class TestCheckGroupPolicyAllowlist:
|
||
|
|
def test_allowlist_user_in_list(self):
|
||
|
|
config = {
|
||
|
|
"group_policy": "allowlist",
|
||
|
|
"group_allow_from": ["twitch:user1", "twitch:user2"],
|
||
|
|
}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is True
|
||
|
|
|
||
|
|
def test_allowlist_user_not_in_list(self):
|
||
|
|
config = {
|
||
|
|
"group_policy": "allowlist",
|
||
|
|
"group_allow_from": ["twitch:user2"],
|
||
|
|
}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
def test_allowlist_per_channel_config(self):
|
||
|
|
config = {
|
||
|
|
"group_policy": "allowlist",
|
||
|
|
"group_allow_from": [],
|
||
|
|
"channels_config": {
|
||
|
|
"test": {"allow_from": ["twitch:user1"]},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is True
|
||
|
|
|
||
|
|
def test_allowlist_per_channel_not_matched(self):
|
||
|
|
config = {
|
||
|
|
"group_policy": "allowlist",
|
||
|
|
"group_allow_from": [],
|
||
|
|
"channels_config": {
|
||
|
|
"test": {"allow_from": ["twitch:user2"]},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
def test_allowlist_empty(self):
|
||
|
|
config = {"group_policy": "allowlist", "group_allow_from": []}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
|
||
|
|
class TestCheckGroupPolicyUnknown:
|
||
|
|
def test_unknown_policy_returns_false(self):
|
||
|
|
config = {"group_policy": "some_unknown_policy"}
|
||
|
|
assert check_group_policy(config, "#test", "user1", "hello") is False
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveRouteWithHashPrefix:
|
||
|
|
def test_strips_hash_prefix_in_route(self):
|
||
|
|
route = resolve_route("agent_1", "#test_channel")
|
||
|
|
assert route == "agent:agent_1:twitch:channel:test_channel"
|
||
|
|
|
||
|
|
def test_no_hash_preserved(self):
|
||
|
|
route = resolve_route("agent_1", "test_channel")
|
||
|
|
assert route == "agent:agent_1:twitch:channel:test_channel"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveAgentIdWithHashPrefix:
|
||
|
|
def test_strips_hash_prefix_when_resolving(self):
|
||
|
|
config = {
|
||
|
|
"channels_config": {
|
||
|
|
"test_channel": {"agent_id": "agent_42"},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result = resolve_agent_id_for_channel(config, "#test_channel", "default_agent")
|
||
|
|
assert result == "agent_42"
|
||
|
|
|
||
|
|
def test_no_hash_still_works(self):
|
||
|
|
config = {
|
||
|
|
"channels_config": {
|
||
|
|
"test_channel": {"agent_id": "agent_42"},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result = resolve_agent_id_for_channel(config, "test_channel", "default_agent")
|
||
|
|
assert result == "agent_42"
|