test_report_engine_sanitization.py
5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import unittest
from ReportEngine.ir import IRValidator
from ReportEngine.nodes.chapter_generation_node import ChapterGenerationNode
class ChapterSanitizationTestCase(unittest.TestCase):
"""Lightweight regression tests for the chapter sanitization helpers."""
def setUp(self):
self.node = ChapterGenerationNode(llm_client=None, validator=IRValidator(), storage=None)
def test_table_cell_empty_blocks_repaired(self):
chapter = {
"blocks": [
{
"type": "table",
"rows": [
{
"cells": [
{"blocks": []},
{"text": "同比变化", "blocks": None},
]
}
],
}
]
}
self.node._sanitize_chapter_blocks(chapter)
table_block = chapter["blocks"][0]
cells = table_block["rows"][0]["cells"]
self.assertEqual(len(cells), 2)
for cell in cells:
blocks = cell.get("blocks")
self.assertIsInstance(blocks, list)
self.assertGreater(len(blocks), 0)
for block in blocks:
self.assertEqual(block.get("type"), "paragraph")
def test_table_rows_scalar_values_expanded(self):
chapter = {"blocks": [{"type": "table", "rows": ["全国趋势"]}]}
self.node._sanitize_chapter_blocks(chapter)
table_block = chapter["blocks"][0]
self.assertEqual(len(table_block["rows"]), 1)
row = table_block["rows"][0]
self.assertIn("cells", row)
self.assertEqual(len(row["cells"]), 1)
cell = row["cells"][0]
self.assertIsInstance(cell.get("blocks"), list)
self.assertEqual(
cell["blocks"][0]["inlines"][0]["text"],
"全国趋势",
)
def test_engine_quote_validation(self):
validator = IRValidator()
chapter = {
"chapterId": "S1",
"title": "Engine 引用校验",
"anchor": "section-1",
"order": 1,
"blocks": [
{
"type": "engineQuote",
"engine": "insight",
"title": "Insight Agent",
"blocks": [
{
"type": "paragraph",
"inlines": [{"text": "来自 Insight Engine 的观点"}],
}
],
}
],
}
valid, errors = validator.validate_chapter(chapter)
self.assertTrue(valid, errors)
self.assertFalse(errors)
def test_engine_quote_rejects_disallowed_marks_and_blocks(self):
validator = IRValidator()
chapter = {
"chapterId": "S1",
"title": "Engine 引用校验",
"anchor": "section-1",
"order": 1,
"blocks": [
{
"type": "engineQuote",
"engine": "media",
"title": "Media Agent",
"blocks": [
{"type": "math", "latex": "x=y"},
{
"type": "paragraph",
"inlines": [
{"text": "test", "marks": [{"type": "color"}]}
],
},
],
}
],
}
valid, errors = validator.validate_chapter(chapter)
self.assertFalse(valid)
self.assertTrue(any("仅允许 paragraph" in err for err in errors))
self.assertTrue(any("仅允许 bold/italic" in err for err in errors))
def test_engine_quote_sanitization_strips_disallowed(self):
chapter = {
"blocks": [
{
"type": "engineQuote",
"engine": "query",
"blocks": [
{"type": "list", "items": [["非法"]]},
{
"type": "paragraph",
"inlines": [
{
"text": "abc",
"marks": [{"type": "bold"}, {"type": "highlight"}],
}
],
},
],
}
]
}
node = self.node
node._sanitize_chapter_blocks(chapter)
eq_block = chapter["blocks"][0]
self.assertEqual(eq_block["type"], "engineQuote")
self.assertEqual(eq_block.get("title"), "Query Agent")
inner_blocks = eq_block.get("blocks")
self.assertTrue(all(b.get("type") == "paragraph" for b in inner_blocks))
marks = inner_blocks[0]["inlines"][0].get("marks")
self.assertEqual(marks, [])
marks2 = inner_blocks[1]["inlines"][0].get("marks")
self.assertEqual(marks2, [{"type": "bold"}])
def test_engine_quote_title_must_match_engine(self):
validator = IRValidator()
chapter = {
"chapterId": "S1",
"title": "Engine 引用校验",
"anchor": "section-1",
"order": 1,
"blocks": [
{
"type": "engineQuote",
"engine": "query",
"title": "Media Agent",
"blocks": [
{
"type": "paragraph",
"inlines": [{"text": "错误标题"}],
}
],
}
],
}
valid, errors = validator.validate_chapter(chapter)
self.assertFalse(valid)
self.assertTrue(any("title 必须与engine一致" in err for err in errors))
if __name__ == "__main__":
unittest.main()