68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
|
|
import json
|
||
|
|
|
||
|
|
from larkx.proto import proto_pb2 as P
|
||
|
|
from snooper_client import extract_card_text
|
||
|
|
from snooper_storage import SnifferStorage as Storage
|
||
|
|
|
||
|
|
|
||
|
|
def test_card_text_extracts_json_fields_and_rich_text():
|
||
|
|
rich_property = P.TextProperty(content="合成富文本卡片正文").SerializeToString()
|
||
|
|
data = {
|
||
|
|
"jsonCard": json.dumps(
|
||
|
|
{
|
||
|
|
"title": "合成卡片标题",
|
||
|
|
"elements": [
|
||
|
|
{"tag": "text", "text": "合成卡片内容"},
|
||
|
|
{"tag": "action", "url": "https://example.com/card"},
|
||
|
|
],
|
||
|
|
}
|
||
|
|
),
|
||
|
|
"richtext": {
|
||
|
|
"elementIds": ["1"],
|
||
|
|
"elements": {
|
||
|
|
"dictionary": {
|
||
|
|
"1": {"tag": 1, "property": rich_property},
|
||
|
|
}
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
text = extract_card_text(data)
|
||
|
|
|
||
|
|
assert "合成卡片标题" in text
|
||
|
|
assert "合成卡片内容" in text
|
||
|
|
assert "https://example.com/card" in text
|
||
|
|
assert "合成富文本卡片正文" in text
|
||
|
|
|
||
|
|
|
||
|
|
def test_backfill_card_text_updates_existing_rows_once():
|
||
|
|
storage = Storage(url="sqlite:///:memory:")
|
||
|
|
data = {
|
||
|
|
"jsonCard": json.dumps({"title": "合成卡片标题", "text": "合成卡片正文"})
|
||
|
|
}
|
||
|
|
storage.save_message(
|
||
|
|
{
|
||
|
|
"msg_id": "card-1",
|
||
|
|
"chat_id": "chat-a",
|
||
|
|
"chat_type": 2,
|
||
|
|
"from_id": "sender-a",
|
||
|
|
"msg_type": 14,
|
||
|
|
"msg_type_name": "CARD",
|
||
|
|
"create_time": 1_792_000_000,
|
||
|
|
"content": "[卡片]",
|
||
|
|
"content_data": data,
|
||
|
|
},
|
||
|
|
direction="in",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert storage.backfill_card_text() == 1
|
||
|
|
assert storage.backfill_card_text() == 0
|
||
|
|
row = storage.get_messages("chat-a")[0]
|
||
|
|
assert "合成卡片标题" in row.content
|
||
|
|
assert "合成卡片正文" in row.content
|
||
|
|
assert row.content.count("合成卡片正文") == 1
|
||
|
|
|
||
|
|
|
||
|
|
def test_card_text_ignores_placeholder_only_rich_text():
|
||
|
|
assert extract_card_text({"richtext": {}}) == ""
|