|
| 1 | +"""Regression tests for the v5.4.1 Human-First and brownfield gate fixes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +ROOT = Path(__file__).resolve().parents[2] |
| 9 | +sys.path.insert(0, str(ROOT / "scripts")) |
| 10 | +sys.path.insert(0, str(ROOT / "scripts" / "validators")) |
| 11 | + |
| 12 | +from quality_gate import Gate # noqa: E402 |
| 13 | +from scan_prototype_css import scan # noqa: E402 |
| 14 | +from prd_structure import _section_bodies # noqa: E402 |
| 15 | + |
| 16 | + |
| 17 | +def codes(gate: Gate) -> set[str]: |
| 18 | + return {item.code for item in gate.findings} |
| 19 | + |
| 20 | + |
| 21 | +class MemoryGate(Gate): |
| 22 | + def __init__(self, documents: dict[str, str]): |
| 23 | + super().__init__() |
| 24 | + self.documents = {str(Path(name)): value for name, value in documents.items()} |
| 25 | + |
| 26 | + def read(self, path: Path) -> str: |
| 27 | + return self.documents[str(path)] |
| 28 | + |
| 29 | + |
| 30 | +def test_h2_uses_full_h3_subtree() -> None: |
| 31 | + raw = """## 4. 端到端角色旅程 |
| 32 | +
|
| 33 | +### 4.1 企业旅程 FLOW-DEMO-001 |
| 34 | +ROLE-USER 进入页面,提交后得到可见成功结果;失败时保留输入并恢复。 |
| 35 | +
|
| 36 | +## 5. 业务流程与状态 |
| 37 | +正文。 |
| 38 | +""" |
| 39 | + sections = dict(_section_bodies(raw)) |
| 40 | + assert "ROLE-USER" in sections["4. 端到端角色旅程"] |
| 41 | + assert "FLOW-DEMO-001" in sections["4. 端到端角色旅程"] |
| 42 | + |
| 43 | + |
| 44 | +def test_todo_inside_stable_id_is_not_unknown_marker() -> None: |
| 45 | + gate = Gate() |
| 46 | + raw = """--- |
| 47 | +open_p0_unknown_ids: [] |
| 48 | +--- |
| 49 | +## 模块 |
| 50 | +REG-RPT-TODO 是待办区域,不是 TODO 占位符。 |
| 51 | +""" |
| 52 | + gate._check_unknowns(Path("prd.md"), raw, gate._frontmatter(raw), stage="specify", scope_refs=set()) |
| 53 | + assert "PRD-UNTRACKED-UNKNOWN" not in codes(gate) |
| 54 | + |
| 55 | + |
| 56 | +def test_uiact_is_allowed_without_business_ac() -> None: |
| 57 | + path = Path("prototype.html") |
| 58 | + raw = """<!doctype html><html><head><style> |
| 59 | +.btn{border:1px solid #1677ff;padding:8px 12px;background:#fff}.page{display:block}.hidden{display:none!important} |
| 60 | +</style></head><body> |
| 61 | +<section class="page" data-testid="page-VIEW-DEMO-001" data-state="default"> |
| 62 | +<div data-testid="region-REG-DEMO-001"><button class="btn" data-action="UIACT-TAB-NEXT">下一页签</button> |
| 63 | +<button class="btn" data-action="ACT-DEMO-SAVE" data-ac="AC-DEMO-001">保存</button></div></section> |
| 64 | +<script>document.addEventListener('click',e=>{const el=e.target.closest('[data-action]');if(!el)return;switch(el.dataset.action){case 'UIACT-TAB-NEXT':document.body.setAttribute('data-state','tab-next');break;case 'ACT-DEMO-SAVE':document.body.setAttribute('data-state','saved');break;}});</script> |
| 65 | +</body></html>""" |
| 66 | + gate = MemoryGate({str(path): raw}) |
| 67 | + gate.check_prototype(path, "L3") |
| 68 | + found = codes(gate) |
| 69 | + assert "PROTO-UNSTABLE-ACTION" not in found |
| 70 | + assert "PROTO-ACTION-NO-AC" not in found |
| 71 | + assert "PROTO-UNHANDLED-ACTION" not in found |
| 72 | + |
| 73 | + |
| 74 | +def test_binding_terms_are_checked_across_handoff() -> None: |
| 75 | + prd = Path("PRD.md") |
| 76 | + prototype = Path("prototype.html") |
| 77 | + prd_raw = """--- |
| 78 | +page_contract_view_ids: [VIEW-DEMO-001] |
| 79 | +binding_terms: [道路运输经营许可证] |
| 80 | +--- |
| 81 | +REQ-DEMO-001 ACT-DEMO-001 AC-DEMO-001 |
| 82 | +- id: AC-DEMO-001 |
| 83 | +""" |
| 84 | + prototype_raw = '<section data-testid="page-VIEW-DEMO-001">行业经营许可</section>' |
| 85 | + gate = MemoryGate({str(prd): prd_raw, str(prototype): prototype_raw}) |
| 86 | + gate.check_handoff(prd, [prototype], "L2") |
| 87 | + assert "HANDOFF-BINDING-TERM-MISSING" in codes(gate) |
| 88 | + |
| 89 | + |
| 90 | +def test_demo_and_local_iframe_are_blocked() -> None: |
| 91 | + path = Path("prototype.html") |
| 92 | + raw = '<section data-testid="page-VIEW-DEMO-001">验收场景<iframe src="child.html"></iframe></section>' |
| 93 | + gate = MemoryGate({str(path): raw}) |
| 94 | + gate.check_prototype(path, "L2") |
| 95 | + found = codes(gate) |
| 96 | + assert "PROTO-DEMO-SCAFFOLDING-VISIBLE" in found |
| 97 | + assert "PROTO-NESTED-PRODUCT-IFRAME" in found |
| 98 | + |
| 99 | +def test_css_scans_unstyled_controls_and_tiny_primary_text() -> None: |
| 100 | + findings = scan("""<style>.primary-btn{color:#fff}button{font-size:9px}</style><button class="btn primary">保存</button>""") |
| 101 | + kinds = {item["kind"] for item in findings} |
| 102 | + assert "unstyled-control-class" in kinds |
| 103 | + assert "unreadable-type-scale" in kinds |
| 104 | + |
| 105 | + |
| 106 | +def test_state_columns_reject_api_ids() -> None: |
| 107 | + gate = Gate() |
| 108 | + raw = """REQ-DEMO-001 AC-DEMO-001 |
| 109 | +| 当前状态 | 动作 | 下一状态 | |
| 110 | +|---|---|---| |
| 111 | +| 待处理 | 复检 | API-RISK-RECHECK 复检 | |
| 112 | +""" |
| 113 | + gate._check_testability(Path("PRD.md"), raw, {}, "L2") |
| 114 | + assert "PRD-STATE-SEMANTIC-POLLUTION" in codes(gate) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")] |
| 119 | + for test in tests: |
| 120 | + test() |
| 121 | + print(f"PASS: {len(tests)} v5.4.1 Human-First gate regressions") |
0 commit comments