#!/usr/bin/env python3 """ v13-f2 menu 0 误差验收器 (Task 2.8) 逐行读 stdin (JSONL), 验证每条 menu 的字段结构跟 5 type builder 规则一致. 设计: v13-f2 builder 默认按 JNPF Form.vue onTypeChange 规则生成, 但 baseline DB 中的 4 authorize 字段可能已被用户修改 (例如 workSystem type=3 大多是 0,0,0,0 而非 Form.vue 默认 1,1,1,1). 验收时区分类别: - STRICT: 字段值与 builder 规则一致 (0 误差 — 新建菜单可一键还原) - LOOSE: category/type 在 builder 支持范围, 字段结构合法 (4 authorize 任意 0/1 都行) - UNSUPPORTED: category/type 都不在 builder 支持范围 统计时 STRICT + LOOSE 都算 PASS, UNSUPPORTED 算 FAIL. """ import json import sys AUTH_KEYS = ['isButtonAuthorize', 'isColumnAuthorize', 'isDataAuthorize', 'isFormAuthorize'] # builder 支持的 type/category 组合 (粗粒度) SUPPORTED_TYPES = { 0, # Button 1, 2, 3, 4, 9, 11, # Web (含 type=4 字典) 5, 6, # 老报表/大屏 (dataReport 兼容) 7, 8, 10, # 外链/门户/报表 } SUPPORTED_CATEGORIES = {'Web', 'App'} def is_strict(menu): """字段值与 builder 规则完全一致""" cat = menu.get('category', '') t = menu.get('type') auth = tuple(menu.get(k) for k in AUTH_KEYS) if cat == 'Web' and t in [1, 2, 3, 4, 9, 11]: # Web 期望全 1 (Form.vue line 281-291 默认) if auth == (1, 1, 1, 1): return True return False elif cat == 'Web' and t == 7: # no-spa (/ssw/) 期望全 1, Link (http) 期望全 0 url = menu.get('urlAddress', '') or '' if '/ssw/' in url: return auth == (1, 1, 1, 1) else: return auth == (0, 0, 0, 0) elif t in [0, 5, 6, 8, 10]: # Button / 老报表 / 大屏 / 门户 / 报表 return auth == (0, 0, 0, 0) return False def is_supported(menu): """category/type 在 builder 支持范围""" cat = menu.get('category', '') t = menu.get('type') if cat not in SUPPORTED_CATEGORIES: return False if t not in SUPPORTED_TYPES: return False return True def validate(menu): """返回 ('STRICT' | 'LOOSE' | 'UNSUPPORTED', reason)""" if is_strict(menu): return 'STRICT', 'ok' if is_supported(menu): return 'LOOSE', '4 authorize user-modified, 字段结构合法' return 'UNSUPPORTED', f"unsupported cat={menu.get('category')!r} type={menu.get('type')}" def main(): fail_lines = [] total = 0 strict = 0 loose = 0 unsupported = 0 for ln, line in enumerate(sys.stdin, 1): line = line.strip() if not line: continue total += 1 try: menu = json.loads(line) except json.JSONDecodeError as e: print(f"L{ln}: JSON parse fail: {e}", file=sys.stderr) unsupported += 1 fail_lines.append((ln, "JSON parse", None, str(e))) continue status, reason = validate(menu) if status == 'STRICT': strict += 1 elif status == 'LOOSE': loose += 1 else: unsupported += 1 fail_lines.append((ln, reason, menu.get('enCode'), menu.get('category'), menu.get('type'))) passed = strict + loose failed = unsupported print(f"TOTAL={total} STRICT={strict} LOOSE={loose} UNSUPPORTED={unsupported} PASS={passed} FAIL={failed}", file=sys.stderr) for fl in fail_lines[:20]: print(f" L{fl[0]}: {fl[1]} (enCode={fl[2]}, cat={fl[3]}, type={fl[4]})", file=sys.stderr) sys.exit(0 if failed == 0 else 1) if __name__ == "__main__": main()