Files
meshcore-ha-agent/tests/test_adapter.py
T
Dejvino 5d60a91964 Allowlist pubkey prefixes only; ignore public and hashtag channels
- is_allowed matches pubkey_prefix only; sender names are never trusted
  (channel senders must be known contacts of the companion for the
  MeshCore integration to include their pubkey_prefix)
- channel messages on channel index 0 (public) and hashtag channels
  ('#name') are ignored unconditionally, before the allowlist check
- config flow validates allowlist entries as 6-64 hex characters
  (invalid_allowlist error) and UI strings/README updated
2026-12-01 19:45:36 +01:00

58 lines
1.9 KiB
Python

"""Tests for the HA-independent parts of the adapter (truncation, targets)."""
from custom_components.meshcore_agent import target_from_event, truncate
from custom_components.meshcore_agent.config_flow import _validate, is_hex_prefix
from custom_components.meshcore_agent.core.store import Reminder
def test_truncate_short_untouched():
assert truncate("hello", 200) == "hello"
def test_truncate_long():
text = "a" * 300
out = truncate(text, 200)
assert len(out) == 200
assert out.endswith("...")
def test_truncate_prefers_newline():
text = "line one\n" + "b" * 300
out = truncate(text, 200)
assert out == "line one..."
def test_target_direct():
event = {"message_type": "direct", "pubkey_prefix": "abc123def456"}
assert target_from_event(event) == Reminder(
at="", kind="direct", target="abc123def456"
)
def test_target_channel():
event = {"message_type": "channel", "channel_idx": 2}
assert target_from_event(event) == Reminder(at="", kind="channel", target="2")
def test_target_missing():
assert target_from_event({"message_type": "direct"}) is None
assert target_from_event({"message_type": "channel"}) is None
def test_hex_prefix_validation():
assert is_hex_prefix("aabbcc")
assert is_hex_prefix("DEF456ABC012")
assert is_hex_prefix("a" * 64)
assert not is_hex_prefix("groupchat") # names are never accepted
assert not is_hex_prefix("abc") # too short
assert not is_hex_prefix("aabbzz") # not hex
assert not is_hex_prefix("")
def test_config_validation():
assert _validate({"allowlist": "aabbcc, def456abc012", "max_reply": "200"}) is None
assert _validate({"allowlist": "", "max_reply": 200}) is None
assert _validate({"allowlist": "myname", "max_reply": 200}) == "invalid_allowlist"
assert _validate({"allowlist": "aabbcc", "max_reply": 5}) is None # max_reply checked separately
assert _validate({"allowlist": "aabbcc"}) == "invalid_max_reply"