/** * 从核对页面的 DATA 抽出用例索引 —— docs/e2e/cases.index.json * * 索引是「用例文字」与「脚本」之间的唯一事实来源: * 批量写脚本时照着它写,HTML 侧按 tc 编号回填执行结果。 * 路由不靠猜,用前端真实路由表反查 links 字段。 */ import fs from 'node:fs' import path from 'node:path' import { DOCS_DIR, E2E_DIR, REPO_DIR } from '../lib/config.mjs' const HTML = path.join(E2E_DIR, 'index.html') const OUT = path.join(E2E_DIR, 'cases.index.json') function readRoutes() { const dir = path.join(REPO_DIR, 'web/src/router/modules') const routes = [] for (const f of fs.readdirSync(dir)) { const src = fs.readFileSync(path.join(dir, f), 'utf8') const re = /path:\s*'([^']+)'[\s\S]{0,400}?title:\s*'([^']+)'/g let m while ((m = re.exec(src))) routes.push({ path: m[1], title: m[2] }) } return routes } function extractData() { const html = fs.readFileSync(HTML, 'utf8') const m = html.match(/const DATA=(\[[\s\S]*?\n\]);/) if (!m) throw new Error('核对页面里找不到 DATA 数组') return eval(m[1]) } const routes = readRoutes() const routeSet = new Set(routes.map((r) => r.path)) /** links 里混着前端路由和后端接口,只挑真实存在的前端路由 */ function resolveRoute(links = '') { const tokens = links.split(/[,\s]+/).map((s) => s.trim()).filter(Boolean) for (const tk of tokens) { const p = tk.split(':')[0] if (routeSet.has(p)) return p } return '' } const DATA = extractData() const cases = [] for (const d of DATA) { const route = resolveRoute(d.links) for (const tc of d.tcs || []) { cases.push({ tc: tc.tc, req: d.code, reqName: d.name, fl: d.fl, sec: d.sec, grp: d.grp, module: d.module, st: d.st, note: d.note || '', links: d.links || '', route, title: tc.title, type: tc.type, priority: tc.priority, pre: tc.pre, steps: tc.steps, expect: tc.expect }) } } const dup = cases.map((c) => c.tc).filter((tc, i, a) => a.indexOf(tc) !== i) fs.writeFileSync(OUT, JSON.stringify({ generatedFrom: path.basename(HTML), total: cases.length, routes, cases }, null, 2)) console.log(`用例总数 ${cases.length}`) console.log(`重复编号 ${dup.length ? dup.join(',') : '无'}`) console.log(`已解析出前端路由的用例 ${cases.filter((c) => c.route).length}`) console.log(`未解析出路由的用例 ${cases.filter((c) => !c.route).length}`) console.log(`输出 ${path.relative(process.cwd(), OUT)}`)