feat(qqpush): 浏览器长图渲染公告

This commit is contained in:
2026-07-30 21:21:49 +08:00
parent f8e9544fd6
commit d2187e5545
3 changed files with 165 additions and 12 deletions

View File

@@ -0,0 +1,44 @@
import importlib.util
import sys
import types
from pathlib import Path
def load_article_renderer(monkeypatch):
"""加载纯 HTML 构建逻辑,避免测试中启动 Chromium。"""
pyppeteer = types.ModuleType("pyppeteer")
pyppeteer.launch = None
monkeypatch.setitem(sys.modules, "pyppeteer", pyppeteer)
path = Path(__file__).resolve().parents[1] / "danding_bot" / "plugins" / "danding_qqpush" / "article_render.py"
spec = importlib.util.spec_from_file_location("danding_qqpush_article_render", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_article_document_preserves_layout_and_removes_script(monkeypatch):
module = load_article_renderer(monkeypatch)
document = module.build_article_document(
"<p>第一段</p><script>alert('xss')</script><p><strong>第二段</strong></p>",
"公告 <标题>",
"2026-07-30 20:27",
)
assert "<p>第一段</p>" in document
assert "<strong>第二段</strong>" in document
assert "<script>" not in document
assert "公告 &lt;标题&gt;" in document
assert "发布时间2026-07-30 20:27" in document
def test_article_document_uses_safe_text_fallback_without_bleach(monkeypatch):
module = load_article_renderer(monkeypatch)
monkeypatch.setattr(module, "bleach", None)
document = module.build_article_document("<p>第一段</p><script>alert('xss')</script><p>第二段</p>", "标题", None)
assert "<script>" not in document
assert "alert" not in document
assert "第一段<br>第二段" in document