#!/usr/bin/env node /** * hxcs.feifan.net 完整功能验证(续) */ 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 results = []; const ok = (n, d = "") => { results.push({ ok: true, n, d }); console.log("✓", n, d || ""); }; const bad = (n, d = "") => { results.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(3000); await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 }); if (!(await page.content()).includes("moudulesData")) { throw new Error("登录失败或未进入 workbench"); } ok("后台登录", USER); } async function menuUrl(page, keyword) { return page.evaluate((kw) => { 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(kw)) 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; }, keyword); } async function openListFrame(page) { const artUrl = await menuUrl(page, "文章"); 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 testArticleAdd(page) { const listFrame = await openListFrame(page); const onclick = await listFrame.locator('button:has-text("录入")').first().getAttribute("onclick"); const addUrl = onclick?.match(/url:"([^"]+)"/)?.[1]; if (!addUrl) { bad("发文章-录入 URL"); return; } await page.goto(`${BASE}${addUrl}`, { waitUntil: "networkidle", timeout: 60000 }); for (let i = 0; i < 30; i++) { if ((await page.locator(".ck-editor").count()) > 0) break; await page.waitForTimeout(500); } const ck = await page.locator(".ck-editor").count(); const video = await page.locator("text=上传视频").count(); const pdf = await page.locator("text=上传PDF").count(); if (ck && video && pdf) ok("发文章编辑器", `ck=${ck} video=${video} pdf=${pdf}`); else bad("发文章编辑器", `ck=${ck} video=${video} pdf=${pdf}`); } async function testFlowSubmit(page) { await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 }); const listFrame = await openListFrame(page); // 尝试点击「草稿」Tab/链接 for (const sel of ['text=草稿', '.el-radio-button:has-text("草稿")', 'label:has-text("草稿")']) { const el = listFrame.locator(sel).first(); if (await el.count()) { await el.click().catch(() => {}); await page.waitForTimeout(2500); break; } } // 翻页找提交按钮 let applyUrl = null; for (let pageNum = 0; pageNum < 8; pageNum++) { const submitBtn = listFrame.locator('button:has-text("提交")').first(); if (await submitBtn.count()) { const onclick = await submitBtn.getAttribute("onclick"); applyUrl = onclick?.match(/url:"([^"]+)"/)?.[1]; if (applyUrl) break; } const next = listFrame.locator(".btn-next:not(.disabled), .el-pagination__next:not(.disabled)").first(); if (!(await next.count())) break; await next.click().catch(() => {}); await page.waitForTimeout(2000); } if (!applyUrl) { // 从 DB 找草稿 articleid,用 Playwright 在页面内触发 layer let draftId = null; try { draftId = queryRemoteDb("SELECT articleid FROM sp_gzhx_apps.article WHERE dcode=21 ORDER BY articleid DESC LIMIT 1;").trim(); } catch { /* ignore */ } if (draftId) { applyUrl = await listFrame.evaluate((id) => { const html = document.body.innerHTML; const re = new RegExp(`articleid:\\s*"${id}"[\\s\\S]{0,1200}?url:"([^&]+)"`, "i"); const m = html.match(re) || html.match(new RegExp(`params='\\{articleid: "${id}"\\}'[\\s\\S]{0,400}?token=([^&'\" ]+)`, "i")); if (m) return m[1].startsWith("/") ? m[1] : `/apps?token=${m[1]}`; return null; }, draftId); } } if (!applyUrl) { bad("流程-申请弹窗 URL", "列表无草稿提交按钮"); return; } ok("流程-申请弹窗 URL", applyUrl.slice(0, 60)); await page.goto(`${BASE}${applyUrl}`, { waitUntil: "networkidle", timeout: 60000 }); await page.waitForTimeout(3000); const hasFlowJs = await page.evaluate(() => [...document.scripts].some((s) => s.src.includes("flow-submit1")) ); if (hasFlowJs) ok("流程申请页 JSP", "flow-submit1"); else bad("流程申请页 JSP", "缺少 flow-submit1 版本号"); const btn = page.locator('button:has-text("发起")'); for (let i = 0; i < 24; i++) { if (!(await btn.isDisabled().catch(() => true))) break; await page.waitForTimeout(500); } if (await btn.isDisabled().catch(() => false)) { bad("流程-发起按钮", "始终禁用"); return; } ok("流程-发起按钮", "已就绪"); let submitBody = ""; let postResp = ""; page.on("request", (r) => { if (r.method() === "POST") { const b = r.postData() || ""; if (b.includes("cmsId=")) submitBody = b; } }); page.on("response", async (r) => { if (r.request().method() === "POST") { const t = await r.text().catch(() => ""); if (t.includes("发起")) postResp = t.replace(/<[^>]+>/g, " ").slice(0, 200); } }); await page.locator(".ck-editor__editable").first().click().catch(() => {}); await page.locator(".ck-editor__editable").first().type("hxcs验证-" + Date.now()).catch(() => {}); await btn.click(); await page.waitForTimeout(4000); const cmsId = submitBody.match(/cmsId=([^&]*)/)?.[1] || (await page.evaluate(() => space?.vue?.$data?.obj?.cmsId)); if (cmsId) ok("流程-POST cmsId", decodeURIComponent(String(cmsId))); else bad("流程-POST cmsId", "未写入"); if (postResp.includes("发起成功")) ok("流程-后端响应", "发起成功"); else if (postResp.includes("发起失败")) bad("流程-后端响应", postResp.trim()); else if (submitBody) ok("流程-POST", "已提交"); else bad("流程-POST", "无 POST"); } async function testGuestAccount(page) { const url = await menuUrl(page, "游客"); if (!url) { bad("游客账号菜单"); return; } const html = await page.evaluate(async (u) => (await fetch(u)).text(), url); if (html.includes("guest_account") || html.includes("游客")) ok("游客账号列表页"); else bad("游客账号列表页", "内容异常"); } async function testTemplates(page) { const url = await menuUrl(page, "网站模板") || await menuUrl(page, "模板"); if (!url) { bad("网站模板菜单"); return; } const html = await page.evaluate(async (u) => (await fetch(u)).text(), url); if (html.includes("全屏风-红色系")) ok("模板-全屏风红色系"); else bad("模板-全屏风红色系"); if (html.includes("非繁二级网站模板-红色")) ok("模板-二级红色"); else bad("模板-二级红色"); } async function testStaticAssets() { const checks = [ ["/apps/space/plug-in/ckeditor5/build/ckeditor.js", 700000], ["/apps/space/js/space.vue.subassembly.js", 85000], ["/apps/space/pdf/secure-viewer.html", 2000], ]; for (const [path, min] of checks) { const out = execSync(`curl -s -o /dev/null -w '%{http_code} %{size_download}' "${BASE}${path}"`, { encoding: "utf8" }); const [code, size] = out.trim().split(" "); if (code === "200" && Number(size) >= min) ok(`静态 ${path}`, `${size}b`); else bad(`静态 ${path}`, out.trim()); } } async function main() { console.log("=== hxcs 完整验证开始 ===", new Date().toISOString()); await testStaticAssets(); const browser = await chromium.launch({ headless: true }); const page = await browser.newPage(); const errs = []; page.on("console", (m) => { if (m.type() === "error" && !/recycleToken|favicon/.test(m.text())) errs.push(m.text()); }); try { await login(page); await testTemplates(page); await testGuestAccount(page); await testArticleAdd(page); await testFlowSubmit(page); } catch (e) { bad("验证异常", e.message); } if (errs.length) bad("Console", errs.slice(0, 2).join(" | ")); else ok("Console", "无阻断 error"); await browser.close(); const pass = results.filter((r) => r.ok).length; console.log(`\n=== 完成 ${pass}/${results.length} 通过 ===`); results.filter((r) => !r.ok).forEach((r) => console.log(` ✗ ${r.n}: ${r.d}`)); process.exit(results.some((r) => !r.ok) ? 1 : 0); } main().catch((e) => { console.error(e); process.exit(1); });