Spaces:
Paused
Paused
File size: 9,554 Bytes
c0e9d84 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | """Tests for the ChatKey normalization model and the chat_system fix.
The production failure these cover:
ValueError: invalid literal for int() with base 10: 'dm:6802929470:305'
at pyUltroid/agent/_agent.py:628 in chat_system
a DM session key was handed to ``int()`` and aborted the listener. These pin
the new behavior: string session keys and integer chat ids are distinct
slots, malformed keys are rejected at the boundary, and the formatter
registry dispatches by object identity.
"""
from __future__ import annotations
import pytest
from pyUltroid.agent._chatkey import (
CATEGORY_GROUP,
CATEGORY_SESSION,
CATEGORY_USER,
ChatKeyCatalog,
ChatKey,
ChatKeyCategory,
DmSessionKey,
ChatKeyRegistry,
InvalidKeyFormat,
ChatKeyNormalizer,
NumericChatKey,
OpaqueStringKey,
build_chat_key,
normalize_chat_id,
)
from pyUltroid.agent._agent import UltroidAgent
# ββ the production case ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_dm_session_key_does_not_crash():
"""The exact value that raised ValueError in production."""
key = build_chat_key("dm:6802929470:305")
assert isinstance(key, DmSessionKey)
assert key.value == "dm:6802929470:305"
assert key.is_session()
assert key.user_id == 6802929470
assert key.anchor == 305
def test_numeric_and_string_keys_do_not_collide():
"""The collision behind the original bug."""
dm = build_chat_key("dm:6802929470:305").value
num = build_chat_key(6802929470).value
assert dm != num
def test_chat_system_holds_distinct_slots():
"""set/get with a dm key and an int must not clobber each other."""
agent = UltroidAgent()
agent.set_chat_system("dm:6802929470:305", "session prompt")
agent.set_chat_system(8402929470, "group prompt")
assert agent.chat_system("dm:6802929470:305") == "session prompt"
assert agent.chat_system(8402929470) == "group prompt"
# ββ normalization ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.mark.parametrize(
"raw,expected",
[
(12345, "12345"),
(-1, "-1"),
("12345", "12345"),
(True, None), # bool rejected
(None, None), # None rejected
],
)
def test_normalize_chat_id(raw, expected):
if expected is None:
with pytest.raises(InvalidKeyFormat):
normalize_chat_id(raw)
else:
assert normalize_chat_id(raw) == expected
def test_a_chatkey_passes_through_idempotent():
original = build_chat_key("dm:1:2")
again = build_chat_key(original)
assert again is original
def test_a_class_is_rejected_not_coerced():
with pytest.raises(InvalidKeyFormat):
build_chat_key(int) # a type, not a value
def test_an_int_is_numeric():
key = build_chat_key(42)
assert isinstance(key, NumericChatKey)
assert key.is_group()
assert int(key) == 42
def test_a_plain_string_is_opaque():
key = build_chat_key("a-funky-name")
assert isinstance(key, OpaqueStringKey)
assert key.is_user()
def test_malformed_dm_key_rejected():
with pytest.raises(InvalidKeyFormat):
build_chat_key("dm:garbage:1")
def test_dm_key_round_trips():
original = "dm:6802929470:305"
rebuilt = build_chat_key(original).value
assert rebuilt == original
# ββ ChatKey value semantics ββββββββββββββββββββββββββββββββββββββββββββββββ
def test_keys_are_equal_by_canonical_form():
assert build_chat_key(42) == build_chat_key(42)
assert build_chat_key("dm:1:2") == build_chat_key("dm:1:2")
assert build_chat_key(42) != build_chat_key("dm:1:2")
def test_keys_are_hashable_and_usable_as_dict_keys():
table = {build_chat_key("dm:1:2"): "x"}
assert table[build_chat_key("dm:1:2")] == "x"
def test_repr_is_informative():
key = build_chat_key("dm:1:2")
text = repr(key)
assert "dm-session" in text
assert "dm:1:2" in text
# ββ ChatKeyCategory enum βββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_category_enum_round_trips():
assert ChatKeyCategory.of(CATEGORY_SESSION) is ChatKeyCategory.SESSION
assert ChatKeyCategory.SESSION.value == CATEGORY_SESSION
def test_unknown_category_raises():
with pytest.raises(InvalidKeyFormat):
ChatKeyCategory.of("nope")
# ββ formatter registry βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_registry_round_trips_all_categories():
from pyUltroid.agent._chatkey import DEFAULT_CHATKEY_REGISTRY
for raw in (42, "dm:1:2", "opaque-string"):
key = build_chat_key(raw)
assert DEFAULT_CHATKEY_REGISTRY.canonical(key) == key.value
def test_registry_probe_returns_first_claimant():
from pyUltroid.agent._chatkey import DEFAULT_CHATKEY_REGISTRY
session_fmt = DEFAULT_CHATKEY_REGISTRY.probe("dm:1:2")
assert session_fmt is not None
assert session_fmt.category == CATEGORY_SESSION
numeric_fmt = DEFAULT_CHATKEY_REGISTRY.probe("42")
assert numeric_fmt is not None
assert numeric_fmt.category == CATEGORY_GROUP
opaque_fmt = DEFAULT_CHATKEY_REGISTRY.probe("weird")
assert opaque_fmt is not None
assert opaque_fmt.category == CATEGORY_USER
def test_registry_rejects_malformed_dm_prefix():
from pyUltroid.agent._chatkey import DEFAULT_CHATKEY_REGISTRY
with pytest.raises(InvalidKeyFormat):
DEFAULT_CHATKEY_REGISTRY.probe("dm:notanumber:1")
def test_registry_rejects_unknown_category():
from pyUltroid.agent._chatkey import DEFAULT_CHATKEY_REGISTRY
with pytest.raises(InvalidKeyFormat):
DEFAULT_CHATKEY_REGISTRY.for_category("does-not-exist")
def test_registry_rejects_unbound_key_type():
"""A ChatKey subclass with no formatter raises, it does not guess."""
from pyUltroid.agent._chatkey import DEFAULT_CHATKEY_REGISTRY
class WeirdKey(ChatKey):
__slots__ = ()
def __init__(self) -> None:
super().__init__("w", "w", "weird")
with pytest.raises(InvalidKeyFormat):
DEFAULT_CHATKEY_REGISTRY.canonical(WeirdKey()) # type: ignore[arg-type]
# ββ catalog ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_catalog_lists_all_supported_shapes():
names = ChatKeyCatalog.keys()
assert CATEGORY_GROUP in names
assert CATEGORY_SESSION in names
assert CATEGORY_USER in names
def test_catalog_session_formatter_resolves_production_key():
session_fmt = ChatKeyCatalog[CATEGORY_SESSION]
key = session_fmt.parse("dm:6802929470:305")
assert isinstance(key, DmSessionKey)
assert key.user_id == 6802929470
assert key.anchor == 305
def test_catalog_numeric_formatter_handles_negative():
numeric_fmt = ChatKeyCatalog[CATEGORY_GROUP]
key = numeric_fmt.parse("-1")
assert isinstance(key, NumericChatKey)
assert int(key) == -1
# ββ ChatKeyNormalizer isolation β§βββββββββββββββββββββββββββββββββββββββββββββββ
def test_normalizer_can_use_a_restricted_registry():
"""A custom registry changes probe behavior but not int dispatch.
``int`` never goes through ``probe`` - it is handled by the explicit type
branch - so a registry lacking the numeric formatter still parses bare
integers. What the registry *does* control is which string shapes get
claimed before falling through to the catch-all: with only the session
formatter wired, a plain string is not claimed by probe and becomes an
:class:`OpaqueStringKey`, and a malformed ``dm:`` string raises.
"""
from pyUltroid.agent._chatkey import _SessionFormatter
registry = ChatKeyRegistry([_SessionFormatter()])
normalizer = ChatKeyNormalizer(registry=registry)
# int is claimed by the type branch, not the registry.
assert isinstance(normalizer.normalize(42), NumericChatKey)
# A plain string is not claimed by the session-only registry, so it falls
# through to the catch-all rather than being misread as a session.
assert isinstance(normalizer.normalize("weird"), OpaqueStringKey)
# A malformed session string still raises even with a custom registry.
with pytest.raises(InvalidKeyFormat):
normalizer.normalize("dm:garbage:1")
# ββ chat_system persistence format β§ββββββββββββββββββββββββββββββββββββββββ
def test_chat_system_survives_str_round_trip_through_dict():
"""The in-memory and persisted forms agree on canonical strings."""
agent = UltroidAgent()
agent.set_chat_system("dm:6802929470:305", "persisted")
agent.set_chat_system(7, "also persisted")
serialized = agent._chat_systems
assert serialized == {
"dm:6802929470:305": "persisted",
"7": "also persisted",
}
assert "6802929470" not in serialized
|