76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
from yuxi.channel.routing.session_key import (
|
|
build_session_key,
|
|
parse_session_key_parts,
|
|
)
|
|
|
|
|
|
class TestBuildSessionKey:
|
|
def test_default_four_part_key(self):
|
|
key = build_session_key("feishu", "acc1", "dm", "user1")
|
|
assert key == "feishu:acc1:dm:user1"
|
|
|
|
def test_includes_thread_id_when_provided(self):
|
|
key = build_session_key("feishu", "acc1", "dm", "user1", thread_id="t1")
|
|
assert key == "feishu:acc1:dm:user1:t1"
|
|
|
|
def test_extension_takes_precedence(self):
|
|
extension = MagicMock(return_value="custom:key")
|
|
key = build_session_key("feishu", "acc1", "dm", "user1", thread_id="t1", extension=extension)
|
|
assert key == "custom:key"
|
|
extension.assert_called_once_with(
|
|
channel_type="feishu",
|
|
account_id="acc1",
|
|
chat_type="dm",
|
|
peer_id="user1",
|
|
thread_id="t1",
|
|
)
|
|
|
|
def test_extension_ignored_when_returns_none(self):
|
|
extension = MagicMock(return_value=None)
|
|
key = build_session_key("feishu", "acc1", "dm", "user1", thread_id="t1", extension=extension)
|
|
assert key == "feishu:acc1:dm:user1:t1"
|
|
|
|
def test_extension_ignored_when_returns_empty_string(self):
|
|
extension = MagicMock(return_value="")
|
|
key = build_session_key("feishu", "acc1", "dm", "user1", extension=extension)
|
|
assert key == "feishu:acc1:dm:user1"
|
|
|
|
def test_no_extension_ignores_none_thread_id(self):
|
|
key = build_session_key("feishu", "acc1", "dm", "user1", thread_id=None)
|
|
assert key == "feishu:acc1:dm:user1"
|
|
|
|
|
|
class TestParseSessionKeyParts:
|
|
def test_full_key(self):
|
|
parts = parse_session_key_parts("feishu:acc1:dm:user1:t1")
|
|
assert parts == {
|
|
"channel_type": "feishu",
|
|
"account_id": "acc1",
|
|
"chat_type": "dm",
|
|
"peer_id": "user1",
|
|
"thread_id": "t1",
|
|
}
|
|
|
|
def test_empty_string(self):
|
|
parts = parse_session_key_parts("")
|
|
assert parts == {
|
|
"channel_type": None,
|
|
"account_id": None,
|
|
"chat_type": None,
|
|
"peer_id": None,
|
|
"thread_id": None,
|
|
}
|
|
|
|
def test_single_part(self):
|
|
parts = parse_session_key_parts("feishu")
|
|
assert parts["channel_type"] == "feishu"
|
|
assert parts["account_id"] is None
|
|
|
|
def test_more_than_five_parts_keep_extra_in_thread_id(self):
|
|
parts = parse_session_key_parts("a:b:c:d:e:f")
|
|
assert parts["thread_id"] == "e"
|