verify-hxcs-prod-flow.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env node
  2. /** hxcs 生产文章流程 + 审核时间线验证 */
  3. import { chromium } from "playwright";
  4. import { execSync, execFileSync } from "node:child_process";
  5. const BASE = process.env.ZQ_BASE_URL || "https://hxcs.feifan.net";
  6. const USER = process.env.ZQ_USER || "a";
  7. const PASS = process.env.ZQ_PASS || "111111";
  8. const DRAFT_ID = process.env.ZQ_DRAFT_ARTICLEID || "60754";
  9. const R = [];
  10. const ok = (n, d = "") => { R.push({ ok: true, n, d }); console.log("✓", n, d); };
  11. const bad = (n, d = "") => { R.push({ ok: false, n, d }); console.error("✗", n, d); };
  12. function queryRemoteDb(query) {
  13. const password = process.env.HXCS_MYSQL_PASS;
  14. if (!password) throw new Error("缺少 HXCS_MYSQL_PASS;请先通过试运行私密配置启动验证。");
  15. return execFileSync(
  16. process.env.ZQ_MYSQL57 || "/usr/local/mysql-5.7.25-macos10.14-x86_64/bin/mysql",
  17. ["-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],
  18. { encoding: "utf8", env: { ...process.env, MYSQL_PWD: password }, stdio: ["ignore", "pipe", "ignore"] }
  19. );
  20. }
  21. async function login(page) {
  22. await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`, { waitUntil: "domcontentloaded", timeout: 45000 });
  23. await page.fill("#uname", USER);
  24. await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly"));
  25. await page.fill("#upwd", PASS);
  26. await page.click(".submit");
  27. await page.waitForTimeout(3500);
  28. await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 });
  29. if (!(await page.content()).includes("moudulesData")) throw new Error("登录失败");
  30. ok("后台登录", USER);
  31. }
  32. async function openListFrame(page) {
  33. const artUrl = await page.evaluate(() => {
  34. const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/);
  35. if (!raw) return null;
  36. const data = JSON.parse(raw[1]);
  37. const walk = (items) => {
  38. for (const it of items || []) {
  39. if ((it.name || "").includes("文章")) return it.url;
  40. const u = walk(it.menuList);
  41. if (u) return u;
  42. }
  43. return null;
  44. };
  45. for (const m of data) {
  46. const u = walk(m.menuList);
  47. if (u) return u;
  48. }
  49. return null;
  50. });
  51. if (!artUrl) throw new Error("未找到文章菜单");
  52. await page.goto(`${BASE}${artUrl}`, { waitUntil: "networkidle", timeout: 60000 });
  53. await page.waitForTimeout(2000);
  54. const frame = page.frames().find((f) => f.url().includes("apps?token"));
  55. if (!frame) throw new Error("未找到文章列表 iframe");
  56. return frame;
  57. }
  58. async function testApplyPopup(page) {
  59. const listFrame = await openListFrame(page);
  60. let applyUrl = await listFrame.evaluate((id) => {
  61. const html = document.body.innerHTML;
  62. const re = new RegExp(`articleid:\\s*"${id}"[\\s\\S]{0,1500}?url:"([^&]+)"`, "i");
  63. const m = html.match(re);
  64. if (m) return m[1].startsWith("/") ? m[1] : `/apps?token=${m[1]}`;
  65. return null;
  66. }, DRAFT_ID);
  67. if (!applyUrl) {
  68. for (let i = 0; i < 10; i++) {
  69. const btn = listFrame.locator('button:has-text("提交")').first();
  70. if (await btn.count()) {
  71. const onclick = await btn.getAttribute("onclick");
  72. applyUrl = onclick?.match(/url:"([^"]+)"/)?.[1];
  73. if (applyUrl) break;
  74. }
  75. const next = listFrame.locator(".btn-next:not(.disabled)").first();
  76. if (!(await next.count())) break;
  77. await next.click().catch(() => {});
  78. await page.waitForTimeout(2000);
  79. }
  80. }
  81. if (!applyUrl) { bad("申请弹窗 URL", `articleid=${DRAFT_ID} 未找到`); return; }
  82. ok("申请弹窗 URL", applyUrl.slice(0, 70));
  83. await page.goto(`${BASE}${applyUrl}`, { waitUntil: "networkidle", timeout: 60000 });
  84. await page.waitForTimeout(2500);
  85. const body = await page.content();
  86. if (body.includes("编辑器加载中") || body.includes("space-editor") || body.includes("ck-editor")) {
  87. ok("申请页内容", body.includes("编辑器加载中") ? "加载提示可见" : "编辑器可见");
  88. } else if (body.includes("404")) {
  89. bad("申请页内容", "404");
  90. } else {
  91. bad("申请页内容", "无编辑器/加载提示");
  92. }
  93. }
  94. async function testFlowTime(page) {
  95. const applyid = queryRemoteDb("SELECT applyid FROM sp_gzhx_apps.apply ORDER BY applyid DESC LIMIT 1;").trim();
  96. const auditid = queryRemoteDb(`SELECT auditid FROM sp_gzhx_apps.audit WHERE applyid=${Number(applyid)} LIMIT 1;`).trim();
  97. if (!applyid || !auditid) { bad("流程时间线 DB", "无 apply/audit"); return; }
  98. ok("DB apply/audit", `apply=${applyid} audit=${auditid}`);
  99. await page.goto(`${BASE}/app/flow/time/?auditid=${auditid}&applyid=${applyid}`, {
  100. waitUntil: "networkidle", timeout: 60000,
  101. });
  102. await page.waitForTimeout(2000);
  103. const html = await page.content();
  104. if (html.includes("登录信息已过期")) { bad("流程时间线", "会话过期"); return; }
  105. if (html.includes("404") && html.includes("页面不存在")) { bad("流程时间线", "404 页面"); return; }
  106. if (html.includes("process-path") || html.includes("el-timeline") || html.includes("流程图")) {
  107. ok("流程时间线", "时间线/流程图组件已渲染");
  108. } else {
  109. bad("流程时间线", html.slice(0, 200).replace(/\s+/g, " "));
  110. }
  111. }
  112. async function main() {
  113. console.log("=== hxcs 流程验证 ===", new Date().toISOString());
  114. const disp = execSync(`curl -s "${BASE}/apps/space/js/space.display.js"`, { encoding: "utf8" });
  115. if (disp.includes("getSpaceIndex")) ok("space.display.js", "含 getSpaceIndex");
  116. else bad("space.display.js", "缺 getSpaceIndex");
  117. const browser = await chromium.launch({ headless: true });
  118. const page = await browser.newPage();
  119. try {
  120. await login(page);
  121. await testApplyPopup(page);
  122. await testFlowTime(page);
  123. } catch (e) {
  124. bad("验证异常", e.message);
  125. }
  126. await browser.close();
  127. const pass = R.filter((x) => x.ok).length;
  128. console.log(`\n=== ${pass}/${R.length} 通过 ===`);
  129. R.filter((x) => !x.ok).forEach((x) => console.log(` ✗ ${x.n}: ${x.d}`));
  130. process.exit(R.some((x) => !x.ok) ? 1 : 0);
  131. }
  132. main().catch((e) => { console.error(e); process.exit(1); });