#!/usr/bin/env node /** * Browser gate for the beginner page web/teaching/reducer-toolkit.html. * * Opens the generated page over file:// (it must be self-contained and readable * with no server) and asserts: 23 part cards, every group section present, * copyable commands and links to the console / replay / parts manual, no * template leakage, no pageerror/console/request failures, and zero horizontal * overflow at 1440×900 and 390×844. * * Prerequisite: playwright resolvable from web/node_modules. * Exit: 0 pass / 1 page problem / 2 prerequisite missing. * * Usage: node web/tools/teaching-console/check-reducer-page.mjs */ import path from "node:path"; import { pathToFileURL } from "node:url"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const REPO = path.resolve(HERE, "../../.."); const require = createRequire(path.join(REPO, "web/package.json")); const PAGE = path.join(REPO, "web/teaching/reducer-toolkit.html"); const failures = []; const note = (message) => { failures.push(message); console.error(`FAIL ${message}`); }; let chromium; try { ({ chromium } = require("playwright")); } catch { console.error("前置缺失:web/node_modules 下没有 playwright"); process.exit(2); } const VIEWPORTS = [{ width: 1440, height: 900 }, { width: 390, height: 844 }]; const browser = await chromium.launch({ headless: true, args: ["--no-sandbox", "--disable-dev-shm-usage"] }); const page = await browser.newPage({ viewport: VIEWPORTS[0] }); const pageErrors = []; const consoleErrors = []; const failedRequests = []; page.on("pageerror", (error) => pageErrors.push(String(error.message ?? error))); page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); }); page.on("requestfailed", (request) => failedRequests.push(`${request.url()} ${request.failure()?.errorText ?? ""}`)); const response = await page.goto(pathToFileURL(PAGE).href, { waitUntil: "load" }); if (!response) note("file:// 打开页面失败(无 response)"); const dom = await page.evaluate(() => { const text = document.body.innerText; const partIds = [...document.querySelectorAll("article.part[id]")].map((node) => node.id); const hrefs = [...document.querySelectorAll("a[href]")].map((node) => node.getAttribute("href")); const commands = [...document.querySelectorAll("code")].map((node) => node.textContent).join("\n"); return { partIds, groups: document.querySelectorAll("section.group").length, hrefs, hasConsole: hrefs.some((href) => /console\.html/.test(href)), hasReplay: hrefs.some((href) => /reducer-replay\.html\?doc=/.test(href)), hasManual: hrefs.some((href) => /reducer-parts-manual\.html#P\d{2}/.test(href)), hasIndex: hrefs.some((href) => /(^|\/)index\.html$/.test(href)), command: /create-part\.mjs P\d{2}/.test(commands) && /create-assembly\.mjs/.test(commands), leakage: ["[object Object]", "undefined", "NaN", "${"].filter((token) => text.includes(token)), }; }); const expectedIds = Array.from({ length: 23 }, (_, index) => `P${String(index + 1).padStart(2, "0")}`); const missing = expectedIds.filter((id) => !dom.partIds.includes(id)); if (dom.partIds.length !== 23) note(`零件卡片 ${dom.partIds.length} 张,应为 23 张`); if (missing.length) note(`缺少零件卡片:${missing.join(", ")}`); if (dom.groups < 10) note(`分组区仅 ${dom.groups} 个,应至少 10 个(7 组 + 装配/工具/排错)`); if (!dom.hasConsole) note("没有跳转教学控制台的链接"); if (!dom.hasReplay) note("没有跳转建模回放的链接"); if (!dom.hasManual) note("没有跳转图文教案(reducer-parts-manual.html#P0x)的链接"); if (!dom.hasIndex) note("没有返回 index.html 的链接"); if (!dom.command) note("缺少可复制的 create-part / create-assembly 命令"); for (const token of dom.leakage) note(`模板泄漏标记:${token}`); for (const error of pageErrors) note(`pageerror: ${error}`); for (const error of consoleErrors) note(`console: ${error}`); for (const error of failedRequests) note(`request: ${error}`); for (const viewport of VIEWPORTS) { await page.setViewportSize(viewport); await page.waitForTimeout(120); const overflow = await page.evaluate(() => Math.max(0, document.documentElement.scrollWidth - window.innerWidth)); if (overflow > 0) note(`${viewport.width}px: 横向溢出 ${overflow}px`); console.log(`${viewport.width}x${viewport.height}: overflow=${overflow}`); } await browser.close(); if (failures.length) { console.error(`\nreducer-toolkit.html 门禁失败:${failures.length} 项`); process.exit(1); } console.log(`\nreducer-toolkit.html 门禁通过:23 张零件卡片 / ${dom.groups} 个分组区 / 双视口 0 溢出`);