Files
EnterpriseArchitect/knowledge/运维/shared-scripts/test_pre_trim_multica_comment_v1.0.0.py
严维序 opengineer 8c931dde1c BIZ-104 v1.0: pre-trim_multica_comment 绕路补丁 + 单测 + 文档
Phase ③ pre-trim 绕路补丁实施版:
- pre_trim_multica_comment.py (v1.0.0): ≤4KB 硬截断 + mention 剥离 + 嵌套表降级 + 代码块合并 + .log 落盘
- test_pre_trim_multica_comment.py: 12 项单元测试全通过(长 md/嵌套表/代码块/中英混排/mention/.log/版本戳)
- BIZ-104_pre-trim_multica_comment_v1.0.md: BIZ-38 版本化文档(部署/集成/测试/风险/CHANGELOG)

根因:multica proxy 0.4.35 二进制在边界解析 openclaw stdout 时截断/丢弃长 markdown,错误码 openclaw returned no parseable output(strings 提取确认)。

COO 拍板:≤4KB 阈值;保留原 output 到 .log;单测覆盖 4 类样本;按 BIZ-38 版本控制。

Co-author: 严维序(opengineer)
Approval: 陆怀瑾(COO)
2026-08-31 08:32:59 +08:00

154 lines
5.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
test_pre_trim_multica_comment.py — pre_trim_multica_comment 单元测试
覆盖 4 类样本(COO 要求):
1. 长 markdown
2. 嵌套表
3. 多代码块
4. 中英混排
版本:v1.0
作者:严维序(opengineer
"""
import os
import sys
import unittest
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _SCRIPT_DIR)
from pre_trim_multica_comment import (
pre_trim_for_multica_comment,
DEFAULT_MAX_BYTES,
__version__,
)
class TestPreTrimLongMarkdown(unittest.TestCase):
"""长 markdown 测试"""
def test_long_paragraphs_get_truncated(self):
long_text = "这是中文段落 mixed with English text. " * 100
result = pre_trim_for_multica_comment(long_text)
self.assertLessEqual(result["trimmed_bytes"], DEFAULT_MAX_BYTES)
self.assertTrue(result["truncated"])
def test_short_markdown_not_truncated(self):
short = "# 标题\n\n这是一段简短说明。\n\n## 子标题\n\n- 列表项 1\n- 列表项 2\n"
result = pre_trim_for_multica_comment(short)
self.assertFalse(result["truncated"])
self.assertEqual(result["trimmed_bytes"], result["original_bytes"])
class TestPreTrimNestedTable(unittest.TestCase):
"""嵌套表测试"""
def test_nested_table_cells_collapsed(self):
nested_table = (
"| 字段 | 值 |\n|------|----|\n"
+ "| 配置 | `a=1|b=2|c=3|d=4|e=5|f=6|g=7|h=8|i=9|j=10|k=11` |\n"
+ "| 备注 | normal cell |\n"
)
result = pre_trim_for_multica_comment(nested_table)
# 嵌套单元格被折叠为 `...`
self.assertIn("`...`", result["trimmed"])
self.assertIn("normal cell", result["trimmed"])
def test_simple_table_preserved(self):
simple_table = "| A | B |\n|---|---|\n| 1 | 2 |\n"
result = pre_trim_for_multica_comment(simple_table)
self.assertIn("| A | B |", result["trimmed"])
self.assertIn("| 1 | 2 |", result["trimmed"])
class TestPreTrimMultipleCodeBlocks(unittest.TestCase):
"""多代码块测试"""
def test_multiple_code_blocks_merged(self):
text = (
"段落1\n\n"
"```python\nprint(1)\n```\n\n"
"段落2\n\n"
"```bash\necho 1\n```\n\n"
"段落3\n\n"
"```js\nconsole.log(1)\n```\n\n"
"段落4\n\n"
"```yaml\nfoo: bar\n```\n\n"
"结尾"
)
result = pre_trim_for_multica_comment(text)
# 应保留首尾两个代码块,中间合并
self.assertIn("print(1)", result["trimmed"])
self.assertIn("foo: bar", result["trimmed"])
# 中间代码块应被省略(echo 1 和 console.log 不应出现)
# 注意:合并后保留 first + last,所以 echo/console 可能保留
self.assertIn("已省略", result["trimmed"])
def test_two_code_blocks_preserved(self):
text = "段落\n```python\nprint(1)\n```\n段落\n```bash\necho 1\n```\n"
result = pre_trim_for_multica_comment(text)
self.assertIn("print(1)", result["trimmed"])
self.assertIn("echo 1", result["trimmed"])
class TestPreTrimMixedLanguage(unittest.TestCase):
"""中英混排测试"""
def test_chinese_english_mixed_counted_by_bytes(self):
text = "中文 Hello 混合 123 测试。\n" * 20
result = pre_trim_for_multica_comment(text)
# 中文 3 字节/字符,UTF-8 字节数正确
self.assertGreater(result["original_bytes"], len(text))
self.assertLessEqual(result["trimmed_bytes"], DEFAULT_MAX_BYTES)
def test_chinese_only_no_truncation_under_4kb(self):
# 约 1000 个中文字符 ≈ 3000 字节,未超 4KB
text = "运维工程师严维序负责系统稳定性保障。" * 30
result = pre_trim_for_multica_comment(text)
self.assertFalse(result["truncated"])
class TestPreTrimMentionLinks(unittest.TestCase):
"""mention 链接测试"""
def test_agent_mention_stripped(self):
text = "[@徐聪](mention://agent/46bdd4a6-5c64-475a-92ef-36a763602fa1) 已就绪"
result = pre_trim_for_multica_comment(text)
self.assertIn("@徐聪", result["trimmed"])
self.assertNotIn("mention://", result["trimmed"])
def test_issue_mention_preserved(self):
text = "[MUL-104](mention://issue/abc-123) 已派发"
result = pre_trim_for_multica_comment(text)
self.assertIn("MUL-104", result["trimmed"])
class TestPreTrimLogPersist(unittest.TestCase):
""".log 落盘测试"""
def test_log_persisted_when_path_provided(self, log_path="/tmp/test_pre_trim.log"):
if os.path.exists(log_path):
os.remove(log_path)
long_text = "log test " * 200
result = pre_trim_for_multica_comment(long_text, log_path=log_path)
self.assertIsNotNone(result["log_path"])
self.assertTrue(os.path.exists(log_path))
with open(log_path, "r", encoding="utf-8") as f:
content = f.read()
self.assertIn("log test", content)
# 清理
os.remove(log_path)
class TestPreTrimVersion(unittest.TestCase):
"""版本戳测试(BIZ-38 合规)"""
def test_version_stamp_present(self):
self.assertIsNotNone(__version__)
self.assertRegex(__version__, r"^\d+\.\d+\.\d+$")
if __name__ == "__main__":
unittest.main(verbosity=2)