Files
PlantAI-rag-new/sdk/app/test_humanizer.py
jerryandClaude Fable 5 cd7e98e7c7 feat(capabilities): S4 SDK+UI 能力端点+Humanizer+设置面板(含 S3 发布修复)
- sdk/services/capabilities.py: MANIFEST+DEFAULTS+sanitize+QA 注册表合并
- sdk/services/humanizer.py: 9 类中文 AI 痕迹规则(块级保护引用/表格/代码/图片)
- sdk/routers/capabilities.py: GET /api/capabilities + POST install 透传
- sdk/schemas.py+sessions.py: SessionMeta.capabilities + PATCH meta 白名单 merge
- sdk/routers/qa.py: humanize 会话开关注入 SSE;chat.js: body 加 session_id
- sse_relay.py: humanize_enabled 参数链(修 _pump_events 可见性编译错误)
- api.py(QA): /capabilities/registry loopback(BFF 合并)
- ui: settings store+SettingsPanel+AppShell 接线+SessionPanel 展开态设置入口+labels
- 测试: humanizer 8 用例+capabilities_svc 6 用例;SDK 146 全绿;UI vitest 172 全绿

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-02 16:50:54 +08:00

64 lines
2.2 KiB
Python

"""test_humanizer.py —— Humanizer 单测(S4)。
覆盖:规则命中 / 块级保护(引用/表格/代码/图片逐字符一致)/ 幂等 / 空串不抛。
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from app.services.humanizer import humanize_answer # noqa: E402
class TestRules:
def test_strikes_ai_phrases(self):
text = "值得注意的是,水稻SPL基因家族在产量调控中发挥作用。此外,它影响穗发育。"
out, stats = humanize_answer(text)
assert "值得注意的是" not in out
assert stats["rules_applied"] >= 1
def test_idempotent(self):
text = "此外,该基因影响发育。"
out1, _ = humanize_answer(text)
out2, _ = humanize_answer(out1)
assert out1 == out2
def test_empty_ok(self):
assert humanize_answer("") == ("", {"rules_applied": 0})
assert humanize_answer(None)[0] is None
class TestBlockProtection:
def test_citation_spans_preserved(self):
text = "该基因调控分蘖 [PMID: 12345678]。此外,另一研究 [DOI: 10.1000/x] 表明……"
out, _ = humanize_answer(text)
assert "[PMID: 12345678]" in out
assert "[DOI: 10.1000/x]" in out
def test_code_fence_preserved(self):
code = "```python\nprint('值得注意的是')\n```"
text = f"正文。{code}"
out, _ = humanize_answer(text)
assert code in out
assert "值得注意的是" in out # 代码块内的不被替换
def test_table_preserved(self):
table = "| 基因 | 功能 |\n|---|---|\n| OsSPL14 | 分蘖 |"
text = f"值得注意的是,见下表。\n{table}"
out, _ = humanize_answer(text)
assert table in out
def test_fig_marker_preserved(self):
marker = "[[FIG:PMC12345]]"
text = f"正文。\n{marker}"
out, _ = humanize_answer(text)
assert marker in out
def test_image_md_preserved(self):
img = "![](/api/figures/proxy?pmcid=PMC7820984&url=https%3A%2F%2Fexample)"
text = f"正文 {img}"
out, _ = humanize_answer(text)
assert img in out