Files
Lark-Snooper/tests/test_client.py
Benature 81b6ff7a39 feat: 图片下载与数据目录本地化
- 新增 download_images.py: 批量下载消息图片到 downloads/images/
- 数据库移至 downloads/messages.db,SnifferStorage 默认使用项目目录
- 图片默认保存到 downloads/images/,支持 --out-dir 自定义
- .gitignore 排除 downloads/ 避免提交二进制文件
- 补充腾讯会议号过滤 prompt
- 补充 Obsidian 文档 is_read frontmatter
2026-11-15 20:07:24 +08:00

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": {}}) == ""