File size: 15,532 Bytes
8ede856 | 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | from types import SimpleNamespace
import pytest
from astrbot.core.message.components import Image, Plain, Reply
from astrbot.core.utils.quoted_message_parser import (
extract_quoted_message_images,
extract_quoted_message_text,
)
class _DummyAPI:
def __init__(
self,
responses: dict[tuple[str, str], dict],
param_responses: dict[tuple[str, tuple[tuple[str, str], ...]], dict]
| None = None,
):
self._responses = responses
self._param_responses = param_responses or {}
async def call_action(self, action: str, **params):
param_key = (action, tuple(sorted((k, str(v)) for k, v in params.items())))
if param_key in self._param_responses:
return self._param_responses[param_key]
msg_id = params.get("message_id")
if msg_id is None:
msg_id = params.get("id")
key = (action, str(msg_id))
if key not in self._responses:
raise RuntimeError(f"no mock response for {key}")
return self._responses[key]
class _FailIfCalledAPI:
async def call_action(self, action: str, **params):
raise AssertionError(
f"call_action should not be called, got action={action}, params={params}"
)
def _make_event(
reply: Reply,
responses: dict[tuple[str, str], dict] | None = None,
param_responses: dict[tuple[str, tuple[tuple[str, str], ...]], dict] | None = None,
):
if responses is None:
responses = {}
if param_responses is None:
param_responses = {}
return SimpleNamespace(
message_obj=SimpleNamespace(message=[reply]),
bot=SimpleNamespace(api=_DummyAPI(responses, param_responses)),
get_group_id=lambda: "",
)
@pytest.mark.asyncio
async def test_extract_quoted_message_text_from_reply_chain():
reply = Reply(id="1", chain=[Plain(text="quoted content")], message_str="")
event = _make_event(reply)
text = await extract_quoted_message_text(event)
assert text == "quoted content"
@pytest.mark.asyncio
async def test_extract_quoted_message_text_no_reply_component():
event = SimpleNamespace(
message_obj=SimpleNamespace(message=[Plain(text="unquoted message")]),
bot=SimpleNamespace(api=_DummyAPI({}, {})),
get_group_id=lambda: "",
)
text = await extract_quoted_message_text(event)
assert text is None
@pytest.mark.asyncio
async def test_extract_quoted_message_images_no_reply_component():
event = SimpleNamespace(
message_obj=SimpleNamespace(message=[Plain(text="unquoted message")]),
bot=SimpleNamespace(api=_FailIfCalledAPI()),
get_group_id=lambda: "",
)
images = await extract_quoted_message_images(event)
assert images == []
@pytest.mark.asyncio
@pytest.mark.parametrize("reply_id", [None, ""])
async def test_extract_quoted_message_text_reply_without_id_does_not_call_get_msg(
reply_id: str | None,
):
reply = Reply(
id="placeholder", chain=[Plain(text="quoted content")], message_str=""
)
object.__setattr__(reply, "id", reply_id)
event = SimpleNamespace(
message_obj=SimpleNamespace(message=[reply]),
bot=SimpleNamespace(api=_FailIfCalledAPI()),
get_group_id=lambda: "",
)
text = await extract_quoted_message_text(event)
assert text == "quoted content"
@pytest.mark.asyncio
async def test_extract_quoted_message_text_fallback_get_msg_and_forward():
reply = Reply(id="100", chain=None, message_str="")
event = _make_event(
reply,
responses={
(
"get_msg",
"100",
): {
"data": {
"message": [
{"type": "text", "data": {"text": "parent"}},
{"type": "forward", "data": {"id": "fwd_1"}},
]
}
},
(
"get_forward_msg",
"fwd_1",
): {
"data": {
"messages": [
{
"sender": {"nickname": "Alice"},
"message": [{"type": "text", "data": {"text": "hello"}}],
},
{
"sender": {"nickname": "Bob"},
"message": [
{"type": "image", "data": {"url": "http://img"}},
{"type": "text", "data": {"text": "world"}},
],
},
]
}
},
},
)
text = await extract_quoted_message_text(event)
assert text is not None
assert "parent" in text
assert "Alice: hello" in text
assert "Bob: [Image]world" in text
@pytest.mark.parametrize(
"placeholder_text",
[
"[Forward Message]",
"[转发消息]",
"[合并转发]",
"Alice: [Forward Message]",
"(Alice): [转发消息]",
"[Forward Message]\n[转发消息]",
"Alice: [Forward Message]\n(Bob): [合并转发]",
"[转发消息]\n\n[合并转发]",
],
)
@pytest.mark.asyncio
async def test_extract_quoted_message_text_forward_placeholder_variants_trigger_fallback(
placeholder_text: str,
):
reply = Reply(id="400", chain=[Plain(text=placeholder_text)], message_str="")
event = _make_event(
reply,
responses={
("get_msg", "400"): {
"data": {
"message": [
{"type": "text", "data": {"text": "Bob: "}},
{"type": "image", "data": {}},
{"type": "text", "data": {"text": "world"}},
]
}
}
},
)
text = await extract_quoted_message_text(event)
assert "Bob: [Image]world" in text
@pytest.mark.asyncio
async def test_extract_quoted_message_text_mixed_placeholder_does_not_trigger_fallback():
reply = Reply(
id="402",
chain=[Plain(text="Alice: [Forward Message]\nreal text")],
message_str="",
)
event = SimpleNamespace(
message_obj=SimpleNamespace(message=[reply]),
bot=SimpleNamespace(api=_FailIfCalledAPI()),
get_group_id=lambda: "",
)
text = await extract_quoted_message_text(event)
assert text is not None
assert "[Forward Message]" in text
assert "real text" in text
@pytest.mark.asyncio
async def test_extract_quoted_message_text_forward_placeholder_fallback_failure():
reply = Reply(id="401", chain=[Plain(text="[Forward Message]")], message_str="")
event = _make_event(reply, responses={})
text = await extract_quoted_message_text(event)
assert text == "[Forward Message]"
@pytest.mark.asyncio
async def test_extract_quoted_message_text_multimsg_malformed_config_does_not_raise():
reply = Reply(id="402", chain=None, message_str="")
event = _make_event(
reply,
responses={
("get_msg", "402"): {
"data": {
"message": [
{
"type": "json",
"data": {
"data": (
'{"app":"com.tencent.multimsg",'
'"config":"oops","meta":{}}'
)
},
},
{"type": "text", "data": {"text": "still works"}},
]
}
}
},
)
text = await extract_quoted_message_text(event)
assert text == "still works"
@pytest.mark.asyncio
async def test_extract_quoted_message_images_from_reply_chain():
reply = Reply(
id="1",
chain=[
Plain(text="quoted"),
Image(file="https://img.example.com/a.jpg"),
],
message_str="",
)
event = _make_event(reply)
images = await extract_quoted_message_images(event)
assert images == ["https://img.example.com/a.jpg"]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_fallback_get_msg_direct_url():
reply = Reply(id="200", chain=None, message_str="")
event = _make_event(
reply,
responses={
("get_msg", "200"): {
"data": {
"message": [
{
"type": "image",
"data": {"url": "https://img.example.com/direct.jpg"},
}
]
}
}
},
)
images = await extract_quoted_message_images(event)
assert images == ["https://img.example.com/direct.jpg"]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_data_image_ref_normalized_to_base64():
data_image_ref = "data:image/png;base64,abcd1234=="
reply = Reply(id="201", chain=None, message_str="")
event = _make_event(
reply,
responses={
("get_msg", "201"): {
"data": {
"message": [
{"type": "image", "data": {"url": data_image_ref}},
]
}
}
},
)
images = await extract_quoted_message_images(event)
assert images == ["base64://abcd1234=="]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_file_url_with_query_string():
url_with_query = "https://img.example.com/direct.jpg?token=abc123#frag"
reply = Reply(id="205", chain=None, message_str="")
event = _make_event(
reply,
responses={
("get_msg", "205"): {
"data": {
"message": [
{
"type": "file",
"data": {
"url": url_with_query,
"name": "direct.jpg",
},
}
]
}
}
},
)
images = await extract_quoted_message_images(event)
assert images == [url_with_query]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_non_image_local_path_is_ignored(tmp_path):
non_image_file = tmp_path / "secret.txt"
non_image_file.write_text("not an image", encoding="utf-8")
reply = Reply(
id="placeholder", chain=[Image(file=str(non_image_file))], message_str=""
)
object.__setattr__(reply, "id", None)
event = SimpleNamespace(
message_obj=SimpleNamespace(message=[reply]),
bot=SimpleNamespace(api=_FailIfCalledAPI()),
get_group_id=lambda: "",
)
images = await extract_quoted_message_images(event)
assert images == []
@pytest.mark.asyncio
async def test_extract_quoted_message_images_chain_placeholder_triggers_fallback():
reply = Reply(id="210", chain=[Plain(text="[Forward Message]")], message_str="")
event = _make_event(
reply,
responses={
("get_msg", "210"): {
"data": {
"message": [
{
"type": "image",
"data": {
"url": "https://img.example.com/from-fallback.jpg"
},
}
]
}
}
},
)
images = await extract_quoted_message_images(event)
assert images == ["https://img.example.com/from-fallback.jpg"]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_fallback_resolve_file_id_with_get_image():
reply = Reply(id="300", chain=None, message_str="")
event = _make_event(
reply,
responses={
("get_msg", "300"): {
"data": {"message": [{"type": "image", "data": {"file": "abc123.jpg"}}]}
}
},
param_responses={
("get_image", (("file", "abc123.jpg"),)): {
"data": {"url": "https://img.example.com/resolved.jpg"}
}
},
)
images = await extract_quoted_message_images(event)
assert images == ["https://img.example.com/resolved.jpg"]
@pytest.mark.asyncio
async def test_extract_quoted_message_images_deduplicates_across_sources():
dup_url = "https://img.example.com/dup.jpg"
chain_only_url = "https://img.example.com/only-chain.jpg"
get_msg_only_url = "https://img.example.com/only-get-msg.jpg"
forward_only_url = "https://img.example.com/only-forward.jpg"
reply = Reply(
id="310",
chain=[Image(file=dup_url), Image(file=chain_only_url)],
message_str="",
)
event = _make_event(
reply,
responses={
("get_msg", "310"): {
"data": {
"message": [
{"type": "image", "data": {"url": dup_url}},
{"type": "image", "data": {"url": get_msg_only_url}},
{"type": "forward", "data": {"id": "999"}},
]
}
},
("get_forward_msg", "999"): {
"data": {
"messages": [
{
"sender": {"nickname": "Tester"},
"message": [
{"type": "image", "data": {"url": dup_url}},
{"type": "image", "data": {"url": forward_only_url}},
],
}
]
}
},
},
)
images = await extract_quoted_message_images(event)
assert images == [
dup_url,
chain_only_url,
get_msg_only_url,
forward_only_url,
]
@pytest.mark.asyncio
async def test_extract_quoted_message_nested_forward_id_is_resolved():
nested_image = "https://img.example.com/nested.jpg"
reply = Reply(id="320", chain=[Plain(text="[Forward Message]")], message_str="")
event = _make_event(
reply,
responses={
("get_msg", "320"): {
"data": {"message": [{"type": "forward", "data": {"id": "fwd_1"}}]}
},
("get_forward_msg", "fwd_1"): {
"data": {
"messages": [
{
"sender": {"nickname": "Alice"},
"message": [{"type": "forward", "data": {"id": "fwd_2"}}],
}
]
}
},
("get_forward_msg", "fwd_2"): {
"data": {
"messages": [
{
"sender": {"nickname": "Bob"},
"message": [
{"type": "text", "data": {"text": "deep"}},
{"type": "image", "data": {"url": nested_image}},
],
}
]
}
},
},
)
text = await extract_quoted_message_text(event)
assert text is not None
assert "Bob: deep" in text
images = await extract_quoted_message_images(event)
assert images == [nested_image]
|