validate_delivery.py
10.2 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""Validate the maintainable sources and published DOCX delivery."""
from __future__ import annotations
import re
import sys
import zipfile
import json
from pathlib import Path
from docx import Document
from docx.enum.section import WD_ORIENT
from docx.oxml.ns import qn
REPO_ROOT = Path(__file__).resolve().parents[3]
DOCS_DIR = REPO_ROOT / "docs" / "order-lifecycle"
MANUAL = DOCS_DIR / "README.md"
ACCEPTANCE = DOCS_DIR / "test-acceptance.md"
TRACE = DOCS_DIR / "prd-traceability.md"
DOCX = DOCS_DIR / "精品升级订单全流程维护手册.docx"
COVERAGE_MANIFEST = REPO_ROOT / "tests" / "order-lifecycle" / "coverage.manifest.mjs"
PACKAGE_JSON = REPO_ROOT / "package.json"
ASSETS = [
"order-end-to-end.png",
"work-order-state-machine.png",
"submission-decision.png",
"cancel-refund-correction.png",
"construction-lifecycle.png",
]
SYMBOL_SOURCES = {
"createWithPay": REPO_ROOT / "tabWorkOrder" / "api.js",
"createPrerecordedOrder": REPO_ROOT / "tabWorkOrder" / "api.js",
"supplementOrder": REPO_ROOT / "tabWorkOrder" / "api.js",
"correctionOrder": REPO_ROOT / "tabWorkOrder" / "api.js",
"createPayOrder": REPO_ROOT / "tabWorkOrder" / "api.js",
"getOrderRefundStep": REPO_ROOT / "api" / "api.js",
"revokeOrder": REPO_ROOT / "pages" / "tabWorkOrder" / "api.js",
"showFlag": REPO_ROOT / "tabWorkOrder" / "api.js",
"listConstruction": REPO_ROOT / "tabWorkOrder" / "api.js",
"confirmConstruction": REPO_ROOT / "tabWorkOrder" / "api.js",
}
def require(condition: bool, message: str) -> None:
if not condition:
raise AssertionError(message)
def validate_markdown_links(path: Path, text: str) -> int:
count = 0
for target in re.findall(r"\[[^\]]+\]\(([^)]+)\)", text):
target = target.strip().split("#", 1)[0]
if not target or re.match(r"^[a-z]+://", target, flags=re.I):
continue
resolved = (path.parent / target).resolve()
require(resolved.exists(), f"broken Markdown link in {path.name}: {target}")
count += 1
return count
def validate_sources() -> dict[str, int]:
manual = MANUAL.read_text(encoding="utf-8")
acceptance = ACCEPTANCE.read_text(encoding="utf-8")
trace = TRACE.read_text(encoding="utf-8")
for heading in (
"从需求生成测试用例",
"场景覆盖模型",
"自动化测试分层",
"通用用例模板",
"测试数据预检",
):
require(heading in manual, f"README is missing test-design heading: {heading}")
require(manual.count("```mermaid") == 5, "README must contain exactly five Mermaid diagrams")
require(manual.count("```") >= 10, "Mermaid fences appear incomplete")
evidence_ids = set(re.findall(r"\bPRD-\d{8}-\d{2}\b", trace))
manual_evidence = set(re.findall(r"\bPRD-\d{8}-\d{2}\b", manual))
require(len(evidence_ids) == 58, f"expected 58 evidence IDs, found {len(evidence_ids)}")
require(manual_evidence <= evidence_ids, "README references unknown PRD evidence IDs")
require(
all(value in acceptance for value in ("已覆盖", "已执行", "受数据阻塞")),
"acceptance matrix is missing required status columns",
)
acceptance_ids = set(re.findall(r"\bAC-\d{2}\b", acceptance))
require(len(acceptance_ids) >= 24, "acceptance matrix must cover at least 24 scenarios")
mail_ids = set(re.findall(r"\bMAIL-\d{3}\b", trace))
require(len(mail_ids) == 60, f"expected 60 mail IDs, found {len(mail_ids)}")
hashes = set(re.findall(r"\b[a-f0-9]{64}\b", trace))
require(len(hashes) == 58, f"expected 58 unique SHA-256 values, found {len(hashes)}")
package = json.loads(PACKAGE_JSON.read_text(encoding="utf-8"))
required_commands = (
"test:orderLifecycle",
"test:orderLifecycle:contract",
"test:orderLifecycle:preflight",
"test:orderLifecycle:e2e",
"test:orderLifecycle:ui",
)
for command in required_commands:
require(command in package.get("scripts", {}), f"package script is missing: {command}")
coverage_source = COVERAGE_MANIFEST.read_text(encoding="utf-8")
interface_block = re.search(
r"export const REQUIRED_INTERFACES = \[(.*?)\]\s*\n",
coverage_source,
flags=re.S,
)
require(interface_block is not None, "coverage manifest interface list is missing")
critical_interfaces = set(re.findall(r"'([^']+)'", interface_block.group(1)))
require(
len(critical_interfaces) == 10,
f"expected 10 critical interfaces, found {len(critical_interfaces)}",
)
source_delivery_text = "\n".join((manual, acceptance, trace))
require(
not re.search(r"(?<!\d)1[3-9]\d{9}(?!\d)", source_delivery_text),
"delivery Markdown contains a phone-number literal",
)
require(
not re.search(
r"(?i)(?:mobile|code|sessionId|secret)\s*[:=]\s*['\"][^'\"]+['\"]",
source_delivery_text,
),
"delivery Markdown contains a credential-like literal",
)
link_count = validate_markdown_links(MANUAL, manual)
link_count += validate_markdown_links(ACCEPTANCE, acceptance)
link_count += validate_markdown_links(TRACE, trace)
for index_path in (
REPO_ROOT / "docs" / "README.md",
REPO_ROOT / "docs" / "DOMAIN-INDEX.md",
REPO_ROOT / "docs" / "order-creation" / "README.md",
REPO_ROOT / "docs" / "tabWorkOrder" / "createOrder.md",
):
link_count += validate_markdown_links(
index_path, index_path.read_text(encoding="utf-8")
)
for asset in ASSETS:
asset_path = DOCS_DIR / "assets" / asset
require(asset_path.exists(), f"missing diagram asset: {asset}")
require(asset_path.stat().st_size > 10_000, f"diagram asset too small: {asset}")
for symbol, source in SYMBOL_SOURCES.items():
require(source.exists(), f"missing source file for {symbol}: {source}")
require(symbol in source.read_text(encoding="utf-8"), f"missing code symbol: {symbol}")
return {
"mermaid": 5,
"evidence": len(evidence_ids),
"mails": len(mail_ids),
"hashes": len(hashes),
"acceptance": len(acceptance_ids),
"commands": len(required_commands),
"critical_interfaces": len(critical_interfaces),
"links": link_count,
"symbols": len(SYMBOL_SOURCES),
}
def validate_docx() -> dict[str, int]:
require(DOCX.exists() and DOCX.stat().st_size > 100_000, "DOCX is missing or incomplete")
document = Document(DOCX)
require(len(document.sections) == 2, "DOCX must have portrait body and landscape appendix")
require(document.sections[0].orientation == WD_ORIENT.PORTRAIT, "body must be portrait")
require(document.sections[1].orientation == WD_ORIENT.LANDSCAPE, "appendix must be landscape")
require(len(document.inline_shapes) == 5, "DOCX must embed five flowchart images")
require(len(document.tables) >= 20, "DOCX is missing expected status/evidence tables")
text_parts = [p.text for p in document.paragraphs]
for table in document.tables:
for row in table.rows:
text_parts.extend(cell.text for cell in row.cells)
all_text = "\n".join(text_parts)
for expected in (
"区域助手精品工单迁移与生命周期维护手册",
"createWithPay",
"施工单创建与商品级履约状态",
"测试验收:区域助手精品工单迁移自动化",
"受数据阻塞",
"邮件清单(60/60)",
"附件哈希(58/58)",
):
require(expected in all_text, f"DOCX is missing expected text: {expected}")
require("{{" not in all_text and "}}" not in all_text, "DOCX contains template placeholders")
require(
not re.search(r"(?<!\d)1[3-9]\d{9}(?!\d)", all_text),
"DOCX contains a phone-number literal",
)
require(
not re.search(
r"(?i)(?:mobile|code|sessionId|secret)\s*[:=]\s*['\"][^'\"]+['\"]",
all_text,
),
"DOCX contains a credential-like literal",
)
numbered_paragraphs = 0
for paragraph in document.paragraphs:
p_pr = paragraph._p.pPr
if p_pr is not None and p_pr.numPr is not None:
numbered_paragraphs += 1
require(numbered_paragraphs > 20, "DOCX lists must use real Word numbering")
with zipfile.ZipFile(DOCX) as archive:
document_xml = archive.read("word/document.xml").decode("utf-8")
styles_xml = archive.read("word/styles.xml").decode("utf-8")
require("<w:r><w:r>" not in document_xml, "invalid nested Word run found")
require(document_xml.count("<wp:inline") == 5, "OOXML image count mismatch")
require(document_xml.count("w:tblHeader") >= len(document.tables), "table header flags missing")
require("Microsoft YaHei" in styles_xml, "Microsoft YaHei style override missing")
alt_texts = re.findall(r'<wp:docPr[^>]+descr="([^"]+)"', document_xml)
require(len(alt_texts) == 5 and all(alt_texts), "image alt text is missing")
for style_name in ("Normal", "Heading 1", "Heading 2", "Heading 3"):
style = document.styles[style_name]
require(
style.font.name == "Microsoft YaHei"
or style._element.rPr.rFonts.get(qn("w:eastAsia")) == "Microsoft YaHei",
f"{style_name} does not use Microsoft YaHei",
)
return {
"sections": len(document.sections),
"tables": len(document.tables),
"images": len(document.inline_shapes),
"numbered_paragraphs": numbered_paragraphs,
}
def main() -> int:
try:
source_counts = validate_sources()
docx_counts = validate_docx()
except Exception as exc:
print(f"[FAIL] {exc}", file=sys.stderr)
return 1
print("[OK] maintainable source validation passed")
print(
" Mermaid={mermaid}, PRD={evidence}, mails={mails}, hashes={hashes}, "
"acceptance={acceptance}, commands={commands}, interfaces={critical_interfaces}, "
"links={links}, code symbols={symbols}".format(
**source_counts
)
)
print(
" DOCX sections={sections}, tables={tables}, images={images}, "
"numbered paragraphs={numbered_paragraphs}".format(**docx_counts)
)
return 0
if __name__ == "__main__":
raise SystemExit(main())