#!/usr/bin/env node /** hxcs 生产文章流程 + 审核时间线验证 */ import { chromium } from "playwright"; import { execSync, execFileSync } from "node:child_process"; const BASE = process.env.ZQ_BASE_URL || "https://hxcs.feifan.net"; const USER = process.env.ZQ_USER || "a"; const PASS = process.env.ZQ_PASS || "111111"; const DRAFT_ID = process.env.ZQ_DRAFT_ARTICLEID || "60754"; const R = []; const ok = (n, d = "") => { R.push({ ok: true, n, d }); console.log("✓", n, d); }; const bad = (n, d = "") => { R.push({ ok: false, n, d }); console.error("✗", n, d); }; function queryRemoteDb(query) { const password = process.env.HXCS_MYSQL_PASS; if (!password) throw new Error("缺少 HXCS_MYSQL_PASS;请先通过试运行私密配置启动验证。"); return execFileSync( process.env.ZQ_MYSQL57 || "/usr/local/mysql-5.7.25-macos10.14-x86_64/bin/mysql", ["-h" + (process.env.HXCS_MYSQL_HOST || "10.0.42.6"), "-P" + (process.env.HXCS_MYSQL_PORT || "3306"), "-u" + (process.env.HXCS_MYSQL_USER || "root"), "-NBe", query], { encoding: "utf8", env: { ...process.env, MYSQL_PWD: password }, stdio: ["ignore", "pipe", "ignore"] } ); } async function login(page) { await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`, { waitUntil: "domcontentloaded", timeout: 45000 }); await page.fill("#uname", USER); await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly")); await page.fill("#upwd", PASS); await page.click(".submit"); await page.waitForTimeout(3500); await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 }); if (!(await page.content()).includes("moudulesData")) throw new Error("登录失败"); ok("后台登录", USER); } async function openListFrame(page) { const artUrl = await page.evaluate(() => { const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/); if (!raw) return null; const data = JSON.parse(raw[1]); const walk = (items) => { for (const it of items || []) { if ((it.name || "").includes("文章")) return it.url; const u = walk(it.menuList); if (u) return u; } return null; }; for (const m of data) { const u = walk(m.menuList); if (u) return u; } return null; }); if (!artUrl) throw new Error("未找到文章菜单"); await page.goto(`${BASE}${artUrl}`, { waitUntil: "networkidle", timeout: 60000 }); await page.waitForTimeout(2000); const frame = page.frames().find((f) => f.url().includes("apps?token")); if (!frame) throw new Error("未找到文章列表 iframe"); return frame; } async function testApplyPopup(page) { const listFrame = await openListFrame(page); let applyUrl = await listFrame.evaluate((id) => { const html = document.body.innerHTML; const re = new RegExp(`articleid:\\s*"${id}"[\\s\\S]{0,1500}?url:"([^&]+)"`, "i"); const m = html.match(re); if (m) return m[1].startsWith("/") ? m[1] : `/apps?token=${m[1]}`; return null; }, DRAFT_ID); if (!applyUrl) { for (let i = 0; i < 10; i++) { const btn = listFrame.locator('button:has-text("提交")').first(); if (await btn.count()) { const onclick = await btn.getAttribute("onclick"); applyUrl = onclick?.match(/url:"([^"]+)"/)?.[1]; if (applyUrl) break; } const next = listFrame.locator(".btn-next:not(.disabled)").first(); if (!(await next.count())) break; await next.click().catch(() => {}); await page.waitForTimeout(2000); } } if (!applyUrl) { bad("申请弹窗 URL", `articleid=${DRAFT_ID} 未找到`); return; } ok("申请弹窗 URL", applyUrl.slice(0, 70)); await page.goto(`${BASE}${applyUrl}`, { waitUntil: "networkidle", timeout: 60000 }); await page.waitForTimeout(2500); const body = await page.content(); if (body.includes("编辑器加载中") || body.includes("space-editor") || body.includes("ck-editor")) { ok("申请页内容", body.includes("编辑器加载中") ? "加载提示可见" : "编辑器可见"); } else if (body.includes("404")) { bad("申请页内容", "404"); } else { bad("申请页内容", "无编辑器/加载提示"); } } async function testFlowTime(page) { const applyid = queryRemoteDb("SELECT applyid FROM sp_gzhx_apps.apply ORDER BY applyid DESC LIMIT 1;").trim(); const auditid = queryRemoteDb(`SELECT auditid FROM sp_gzhx_apps.audit WHERE applyid=${Number(applyid)} LIMIT 1;`).trim(); if (!applyid || !auditid) { bad("流程时间线 DB", "无 apply/audit"); return; } ok("DB apply/audit", `apply=${applyid} audit=${auditid}`); await page.goto(`${BASE}/app/flow/time/?auditid=${auditid}&applyid=${applyid}`, { waitUntil: "networkidle", timeout: 60000, }); await page.waitForTimeout(2000); const html = await page.content(); if (html.includes("登录信息已过期")) { bad("流程时间线", "会话过期"); return; } if (html.includes("404") && html.includes("页面不存在")) { bad("流程时间线", "404 页面"); return; } if (html.includes("process-path") || html.includes("el-timeline") || html.includes("流程图")) { ok("流程时间线", "时间线/流程图组件已渲染"); } else { bad("流程时间线", html.slice(0, 200).replace(/\s+/g, " ")); } } async function main() { console.log("=== hxcs 流程验证 ===", new Date().toISOString()); const disp = execSync(`curl -s "${BASE}/apps/space/js/space.display.js"`, { encoding: "utf8" }); if (disp.includes("getSpaceIndex")) ok("space.display.js", "含 getSpaceIndex"); else bad("space.display.js", "缺 getSpaceIndex"); const browser = await chromium.launch({ headless: true }); const page = await browser.newPage(); try { await login(page); await testApplyPopup(page); await testFlowTime(page); } catch (e) { bad("验证异常", e.message); } await browser.close(); const pass = R.filter((x) => x.ok).length; console.log(`\n=== ${pass}/${R.length} 通过 ===`); R.filter((x) => !x.ok).forEach((x) => console.log(` ✗ ${x.n}: ${x.d}`)); process.exit(R.some((x) => !x.ok) ? 1 : 0); } main().catch((e) => { console.error(e); process.exit(1); });