178 lines
6.1 KiB
Python
178 lines
6.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from yuxi.channels.adapters.nextcloudtalk.accounts import (
|
||
|
|
NcAccountConfig,
|
||
|
|
list_nc_accounts,
|
||
|
|
resolve_default_account_id,
|
||
|
|
resolve_merged_config,
|
||
|
|
resolve_nc_account,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class TestNcAccountConfig:
|
||
|
|
def test_default_values(self):
|
||
|
|
cfg = NcAccountConfig("test-id")
|
||
|
|
assert cfg.account_id == "test-id"
|
||
|
|
assert cfg.server_url == ""
|
||
|
|
assert cfg.bot_user == ""
|
||
|
|
assert cfg.app_password == ""
|
||
|
|
assert cfg.display_name == "ForcePilot Bot"
|
||
|
|
assert cfg.dm_policy == "pairing"
|
||
|
|
assert cfg.group_policy == "allowlist"
|
||
|
|
|
||
|
|
def test_config_values(self):
|
||
|
|
cfg = NcAccountConfig("test-id", {
|
||
|
|
"server_url": "https://nc.example.com",
|
||
|
|
"bot_user": "mybot",
|
||
|
|
"app_password": "secret",
|
||
|
|
"display_name": "My Bot",
|
||
|
|
"dm_policy": "open",
|
||
|
|
"group_policy": "open",
|
||
|
|
})
|
||
|
|
assert cfg.server_url == "https://nc.example.com"
|
||
|
|
assert cfg.bot_user == "mybot"
|
||
|
|
assert cfg.app_password == "secret"
|
||
|
|
assert cfg.display_name == "My Bot"
|
||
|
|
assert cfg.dm_policy == "open"
|
||
|
|
assert cfg.group_policy == "open"
|
||
|
|
|
||
|
|
def test_camel_case_aliases(self):
|
||
|
|
cfg = NcAccountConfig("test-id", {
|
||
|
|
"baseUrl": "https://nc.example.com",
|
||
|
|
"botUser": "mybot",
|
||
|
|
"appPassword": "secret",
|
||
|
|
"displayName": "My Bot",
|
||
|
|
"dmPolicy": "open",
|
||
|
|
"groupPolicy": "open",
|
||
|
|
})
|
||
|
|
assert cfg.server_url == "https://nc.example.com"
|
||
|
|
assert cfg.bot_user == "mybot"
|
||
|
|
assert cfg.app_password == "secret"
|
||
|
|
assert cfg.display_name == "My Bot"
|
||
|
|
assert cfg.dm_policy == "open"
|
||
|
|
assert cfg.group_policy == "open"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveNcAccount:
|
||
|
|
def test_default_with_no_accounts(self):
|
||
|
|
config = {
|
||
|
|
"server_url": "https://nc.example.com",
|
||
|
|
"bot_user": "bot",
|
||
|
|
"app_password": "secret",
|
||
|
|
}
|
||
|
|
account = resolve_nc_account("default", config)
|
||
|
|
assert account is not None
|
||
|
|
assert account.account_id == "default"
|
||
|
|
assert account.server_url == "https://nc.example.com"
|
||
|
|
|
||
|
|
def test_nonexistent_account_returns_none(self):
|
||
|
|
config = {"accounts": {"other": {"server_url": "https://other.com"}}}
|
||
|
|
account = resolve_nc_account("missing", config)
|
||
|
|
assert account is None
|
||
|
|
|
||
|
|
def test_default_account_with_accounts_list(self):
|
||
|
|
config = {
|
||
|
|
"accounts": {
|
||
|
|
"default": {"server_url": "https://default.example.com"},
|
||
|
|
"secondary": {"server_url": "https://secondary.example.com"},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
account = resolve_nc_account("default", config)
|
||
|
|
assert account is not None
|
||
|
|
assert account.account_id == "default"
|
||
|
|
|
||
|
|
def test_multi_account_resolution(self):
|
||
|
|
config = {
|
||
|
|
"server_url": "https://base.example.com",
|
||
|
|
"bot_user": "base_bot",
|
||
|
|
"accounts": {
|
||
|
|
"acct1": {
|
||
|
|
"server_url": "https://acct1.example.com",
|
||
|
|
"bot_user": "acct1_bot",
|
||
|
|
"app_password": "pass1",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
account = resolve_nc_account("acct1", config)
|
||
|
|
assert account is not None
|
||
|
|
assert account.account_id == "acct1"
|
||
|
|
assert account.server_url == "https://acct1.example.com"
|
||
|
|
assert account.bot_user == "acct1_bot"
|
||
|
|
|
||
|
|
def test_account_merges_base_config(self):
|
||
|
|
config = {
|
||
|
|
"server_url": "https://base.example.com",
|
||
|
|
"display_name": "Base Bot",
|
||
|
|
"dm_policy": "pairing",
|
||
|
|
"accounts": {
|
||
|
|
"acct1": {
|
||
|
|
"server_url": "https://acct1.example.com",
|
||
|
|
"bot_user": "acct1_bot",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
account = resolve_nc_account("acct1", config)
|
||
|
|
assert account is not None
|
||
|
|
assert account.display_name == "Base Bot"
|
||
|
|
assert account.dm_policy == "pairing"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveMergedConfig:
|
||
|
|
def test_shallow_merge(self):
|
||
|
|
base = {"a": 1, "b": 2}
|
||
|
|
override = {"b": 20, "c": 30}
|
||
|
|
channel_config = {
|
||
|
|
"a": 1, "b": 2,
|
||
|
|
"accounts": {"acct1": override},
|
||
|
|
}
|
||
|
|
merged = resolve_merged_config("acct1", channel_config)
|
||
|
|
assert merged["a"] == 1
|
||
|
|
assert merged["b"] == 20
|
||
|
|
assert merged["c"] == 30
|
||
|
|
|
||
|
|
def test_deep_merge(self):
|
||
|
|
channel_config = {
|
||
|
|
"rooms": {"room-1": {"enabled": True}},
|
||
|
|
"accounts": {
|
||
|
|
"acct1": {"rooms": {"room-1": {"systemPrompt": "hello"}}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
merged = resolve_merged_config("acct1", channel_config)
|
||
|
|
assert merged["rooms"]["room-1"]["enabled"] is True
|
||
|
|
assert merged["rooms"]["room-1"]["systemPrompt"] == "hello"
|
||
|
|
|
||
|
|
def test_excludes_accounts_key(self):
|
||
|
|
channel_config = {
|
||
|
|
"server_url": "https://base.example.com",
|
||
|
|
"accounts": {"acct1": {"server_url": "https://acct1.example.com"}},
|
||
|
|
}
|
||
|
|
merged = resolve_merged_config("acct1", channel_config)
|
||
|
|
assert "accounts" not in merged
|
||
|
|
assert merged["server_url"] == "https://acct1.example.com"
|
||
|
|
|
||
|
|
|
||
|
|
class TestListNcAccounts:
|
||
|
|
def test_empty_accounts_returns_default(self):
|
||
|
|
result = list_nc_accounts({})
|
||
|
|
assert result == ["default"]
|
||
|
|
|
||
|
|
def test_with_accounts(self):
|
||
|
|
config = {"accounts": {"acct1": {}, "acct2": {}}}
|
||
|
|
result = list_nc_accounts(config)
|
||
|
|
assert "default" in result
|
||
|
|
assert "acct1" in result
|
||
|
|
assert "acct2" in result
|
||
|
|
assert result[0] == "default"
|
||
|
|
|
||
|
|
def test_default_not_duplicated(self):
|
||
|
|
config = {"accounts": {"default": {}, "acct1": {}}}
|
||
|
|
result = list_nc_accounts(config)
|
||
|
|
assert len(result) == 2
|
||
|
|
assert result[0] == "default"
|
||
|
|
assert result[1] == "acct1"
|
||
|
|
|
||
|
|
|
||
|
|
class TestResolveDefaultAccountId:
|
||
|
|
def test_always_returns_default(self):
|
||
|
|
assert resolve_default_account_id({}) == "default"
|
||
|
|
assert resolve_default_account_id({"accounts": {"a": {}}}) == "default"
|