// P1 chapter cases: every manual scenario JSON runs through the same action // interpreter the screenshot capture tool uses, with per-action step evidence. import fs from "node:fs"; import path from "node:path"; import { createActionRunner } from "../../../web/tools/manual/lib/actions.mjs"; import { canvasHash, pageState, root } from "../runtime.mjs"; const SCENARIO_DIR = path.join(root, "web", "manual", "scenarios"); // Chapters that import/export large fixtures or drive slow workers need a // longer ceiling than the 10-minute default. const TIMEOUT_OVERRIDES = { ch07: 900000, ch09: 900000, ch10: 900000, ch12: 900000 }; function group(actions) { const groups = []; let waits = []; for (const action of actions) { if (action.type === "wait") { waits.push(action); continue; } groups.push({ action, waits }); waits = []; } if (waits.length) groups.push({ action: null, waits }); return groups; } function describe(action) { if (!action) return "等待渲染"; switch (action.type) { case "goto": return `打开 ${action.url}`; case "click": return `点击 ${action.selector ?? `${action.role}:${action.name}`}`; case "fill": return `填写 ${action.selector}`; case "select": return `选择 ${action.selector}`; case "selectIndex": return `选择 ${action.selector}[${action.index}]`; case "press": return `按键 ${action.key}`; case "shot": return `截图 ${action.name}`; case "save": return "保存 FCStd 到 OPFS"; case "seed": return `注入样例 ${action.name ?? path.basename(action.file)}`; case "eval": return "执行页面脚本"; case "drag": return `拖拽 ${action.selector}`; case "clickCanvas": return `点击画布 ${action.selector}`; case "waitFor": return `等待 ${action.selector ?? action.text}`; case "waitOptions": return `等待选项 ${action.selector}`; default: return `${action.type} ${action.selector ?? action.contains ?? ""}`.trim(); } } function expected(action) { if (!action) return "页面稳定"; if (action.type === "assertText" || action.type === "assertAnyText") return `文本包含「${action.contains}」`; if (action.type === "assertValue") return `值为 ${action.value}`; if (action.type === "assertVisible") return "元素可见"; if (action.type === "shot") return "截图非空(>1KB)"; if (action.type === "save") return "OPFS 出现新的 .FCStd"; return "动作成功执行"; } function loadScenarios() { return fs.readdirSync(SCENARIO_DIR).filter(name => name.endsWith(".json")).sort().map(name => { const scenario = JSON.parse(fs.readFileSync(path.join(SCENARIO_DIR, name), "utf8")); return { file: name, scenario }; }); } export const chapterCases = loadScenarios().map(({ file, scenario }) => ({ id: `CH-${scenario.chapter}-${scenario.workflow}`, title: `${scenario.title ?? scenario.workflow}(${scenario.chapter})`, priority: "P1", executor: "scenario", module: null, source: `web/manual/scenarios/${file}`, timeoutMs: TIMEOUT_OVERRIDES[scenario.chapter] ?? 600000, async run({ page, origin, step, ctx, caseDir }) { const runner = createActionRunner(page, { origin, shotDir: path.join(caseDir, "shots"), shotPrefix: `${scenario.chapter}/${scenario.workflow}/`, }); let index = 0; for (const { action, waits } of group(scenario.actions)) { index += 1; const label = action ? `${index}. ${describe(action)}` : `${index}. ${describe(null)}`; await step(label, expected(action), async () => { for (const wait of waits) await page.waitForTimeout(wait.ms ?? 500); if (action) await runner.run(action); return { type: action?.type ?? "wait", waits: waits.length }; }); } await step("章末后置断言", "无 pageerror、状态栏无 Error、画布可渲染", async () => { if (ctx.session.pageErrors.length) throw new Error(`page errors: ${JSON.stringify(ctx.session.pageErrors.slice(0, 3))}`); const state = await pageState(page); if (state.status && /^\s*Error:/.test(state.status)) throw new Error(`status bar reports ${state.status}`); const canvas = await canvasHash(page); if (canvas && canvas.bytes < 1024) throw new Error(`canvas screenshot too small: ${canvas.bytes}`); return { status: state.status, canvas: canvas ? { bytes: canvas.bytes, sha256: canvas.sha256 } : null }; }); }, }));