verify-hxcs-full-e2e.mjs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env node
  2. /**
  3. * hxcs.feifan.net 完整功能验证(续)
  4. */
  5. import { chromium } from "playwright";
  6. import { execSync, execFileSync } from "node:child_process";
  7. const BASE = process.env.ZQ_BASE_URL || "https://hxcs.feifan.net";
  8. const USER = process.env.ZQ_USER || "a";
  9. const PASS = process.env.ZQ_PASS || "111111";
  10. const results = [];
  11. const ok = (n, d = "") => { results.push({ ok: true, n, d }); console.log("✓", n, d || ""); };
  12. const bad = (n, d = "") => { results.push({ ok: false, n, d }); console.error("✗", n, d || ""); };
  13. function queryRemoteDb(query) {
  14. const password = process.env.HXCS_MYSQL_PASS;
  15. if (!password) throw new Error("缺少 HXCS_MYSQL_PASS;请先通过试运行私密配置启动验证。");
  16. return execFileSync(
  17. process.env.ZQ_MYSQL57 || "/usr/local/mysql-5.7.25-macos10.14-x86_64/bin/mysql",
  18. ["-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],
  19. { encoding: "utf8", env: { ...process.env, MYSQL_PWD: password }, stdio: ["ignore", "pipe", "ignore"] }
  20. );
  21. }
  22. async function login(page) {
  23. await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`, { waitUntil: "domcontentloaded", timeout: 45000 });
  24. await page.fill("#uname", USER);
  25. await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly"));
  26. await page.fill("#upwd", PASS);
  27. await page.click(".submit");
  28. await page.waitForTimeout(3000);
  29. await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 });
  30. if (!(await page.content()).includes("moudulesData")) {
  31. throw new Error("登录失败或未进入 workbench");
  32. }
  33. ok("后台登录", USER);
  34. }
  35. async function menuUrl(page, keyword) {
  36. return page.evaluate((kw) => {
  37. const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/);
  38. if (!raw) return null;
  39. const data = JSON.parse(raw[1]);
  40. const walk = (items) => {
  41. for (const it of items || []) {
  42. if ((it.name || "").includes(kw)) return it.url;
  43. const u = walk(it.menuList);
  44. if (u) return u;
  45. }
  46. return null;
  47. };
  48. for (const m of data) {
  49. const u = walk(m.menuList);
  50. if (u) return u;
  51. }
  52. return null;
  53. }, keyword);
  54. }
  55. async function openListFrame(page) {
  56. const artUrl = await menuUrl(page, "文章");
  57. if (!artUrl) throw new Error("未找到文章菜单");
  58. await page.goto(`${BASE}${artUrl}`, { waitUntil: "networkidle", timeout: 60000 });
  59. await page.waitForTimeout(2000);
  60. const frame = page.frames().find((f) => f.url().includes("apps?token"));
  61. if (!frame) throw new Error("未找到文章列表 iframe");
  62. return frame;
  63. }
  64. async function testArticleAdd(page) {
  65. const listFrame = await openListFrame(page);
  66. const onclick = await listFrame.locator('button:has-text("录入")').first().getAttribute("onclick");
  67. const addUrl = onclick?.match(/url:"([^"]+)"/)?.[1];
  68. if (!addUrl) { bad("发文章-录入 URL"); return; }
  69. await page.goto(`${BASE}${addUrl}`, { waitUntil: "networkidle", timeout: 60000 });
  70. for (let i = 0; i < 30; i++) {
  71. if ((await page.locator(".ck-editor").count()) > 0) break;
  72. await page.waitForTimeout(500);
  73. }
  74. const ck = await page.locator(".ck-editor").count();
  75. const video = await page.locator("text=上传视频").count();
  76. const pdf = await page.locator("text=上传PDF").count();
  77. if (ck && video && pdf) ok("发文章编辑器", `ck=${ck} video=${video} pdf=${pdf}`);
  78. else bad("发文章编辑器", `ck=${ck} video=${video} pdf=${pdf}`);
  79. }
  80. async function testFlowSubmit(page) {
  81. await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle", timeout: 60000 });
  82. const listFrame = await openListFrame(page);
  83. // 尝试点击「草稿」Tab/链接
  84. for (const sel of ['text=草稿', '.el-radio-button:has-text("草稿")', 'label:has-text("草稿")']) {
  85. const el = listFrame.locator(sel).first();
  86. if (await el.count()) {
  87. await el.click().catch(() => {});
  88. await page.waitForTimeout(2500);
  89. break;
  90. }
  91. }
  92. // 翻页找提交按钮
  93. let applyUrl = null;
  94. for (let pageNum = 0; pageNum < 8; pageNum++) {
  95. const submitBtn = listFrame.locator('button:has-text("提交")').first();
  96. if (await submitBtn.count()) {
  97. const onclick = await submitBtn.getAttribute("onclick");
  98. applyUrl = onclick?.match(/url:"([^"]+)"/)?.[1];
  99. if (applyUrl) break;
  100. }
  101. const next = listFrame.locator(".btn-next:not(.disabled), .el-pagination__next:not(.disabled)").first();
  102. if (!(await next.count())) break;
  103. await next.click().catch(() => {});
  104. await page.waitForTimeout(2000);
  105. }
  106. if (!applyUrl) {
  107. // 从 DB 找草稿 articleid,用 Playwright 在页面内触发 layer
  108. let draftId = null;
  109. try {
  110. draftId = queryRemoteDb("SELECT articleid FROM sp_gzhx_apps.article WHERE dcode=21 ORDER BY articleid DESC LIMIT 1;").trim();
  111. } catch { /* ignore */ }
  112. if (draftId) {
  113. applyUrl = await listFrame.evaluate((id) => {
  114. const html = document.body.innerHTML;
  115. const re = new RegExp(`articleid:\\s*&quot;${id}&quot;[\\s\\S]{0,1200}?url:&quot;([^&]+)&quot;`, "i");
  116. const m = html.match(re) || html.match(new RegExp(`params='\\{articleid: &quot;${id}&quot;\\}'[\\s\\S]{0,400}?token=([^&'\" ]+)`, "i"));
  117. if (m) return m[1].startsWith("/") ? m[1] : `/apps?token=${m[1]}`;
  118. return null;
  119. }, draftId);
  120. }
  121. }
  122. if (!applyUrl) { bad("流程-申请弹窗 URL", "列表无草稿提交按钮"); return; }
  123. ok("流程-申请弹窗 URL", applyUrl.slice(0, 60));
  124. await page.goto(`${BASE}${applyUrl}`, { waitUntil: "networkidle", timeout: 60000 });
  125. await page.waitForTimeout(3000);
  126. const hasFlowJs = await page.evaluate(() =>
  127. [...document.scripts].some((s) => s.src.includes("flow-submit1"))
  128. );
  129. if (hasFlowJs) ok("流程申请页 JSP", "flow-submit1");
  130. else bad("流程申请页 JSP", "缺少 flow-submit1 版本号");
  131. const btn = page.locator('button:has-text("发起")');
  132. for (let i = 0; i < 24; i++) {
  133. if (!(await btn.isDisabled().catch(() => true))) break;
  134. await page.waitForTimeout(500);
  135. }
  136. if (await btn.isDisabled().catch(() => false)) { bad("流程-发起按钮", "始终禁用"); return; }
  137. ok("流程-发起按钮", "已就绪");
  138. let submitBody = "";
  139. let postResp = "";
  140. page.on("request", (r) => {
  141. if (r.method() === "POST") {
  142. const b = r.postData() || "";
  143. if (b.includes("cmsId=")) submitBody = b;
  144. }
  145. });
  146. page.on("response", async (r) => {
  147. if (r.request().method() === "POST") {
  148. const t = await r.text().catch(() => "");
  149. if (t.includes("发起")) postResp = t.replace(/<[^>]+>/g, " ").slice(0, 200);
  150. }
  151. });
  152. await page.locator(".ck-editor__editable").first().click().catch(() => {});
  153. await page.locator(".ck-editor__editable").first().type("hxcs验证-" + Date.now()).catch(() => {});
  154. await btn.click();
  155. await page.waitForTimeout(4000);
  156. const cmsId = submitBody.match(/cmsId=([^&]*)/)?.[1] || (await page.evaluate(() => space?.vue?.$data?.obj?.cmsId));
  157. if (cmsId) ok("流程-POST cmsId", decodeURIComponent(String(cmsId)));
  158. else bad("流程-POST cmsId", "未写入");
  159. if (postResp.includes("发起成功")) ok("流程-后端响应", "发起成功");
  160. else if (postResp.includes("发起失败")) bad("流程-后端响应", postResp.trim());
  161. else if (submitBody) ok("流程-POST", "已提交");
  162. else bad("流程-POST", "无 POST");
  163. }
  164. async function testGuestAccount(page) {
  165. const url = await menuUrl(page, "游客");
  166. if (!url) { bad("游客账号菜单"); return; }
  167. const html = await page.evaluate(async (u) => (await fetch(u)).text(), url);
  168. if (html.includes("guest_account") || html.includes("游客")) ok("游客账号列表页");
  169. else bad("游客账号列表页", "内容异常");
  170. }
  171. async function testTemplates(page) {
  172. const url = await menuUrl(page, "网站模板") || await menuUrl(page, "模板");
  173. if (!url) { bad("网站模板菜单"); return; }
  174. const html = await page.evaluate(async (u) => (await fetch(u)).text(), url);
  175. if (html.includes("全屏风-红色系")) ok("模板-全屏风红色系");
  176. else bad("模板-全屏风红色系");
  177. if (html.includes("非繁二级网站模板-红色")) ok("模板-二级红色");
  178. else bad("模板-二级红色");
  179. }
  180. async function testStaticAssets() {
  181. const checks = [
  182. ["/apps/space/plug-in/ckeditor5/build/ckeditor.js", 700000],
  183. ["/apps/space/js/space.vue.subassembly.js", 85000],
  184. ["/apps/space/pdf/secure-viewer.html", 2000],
  185. ];
  186. for (const [path, min] of checks) {
  187. const out = execSync(`curl -s -o /dev/null -w '%{http_code} %{size_download}' "${BASE}${path}"`, { encoding: "utf8" });
  188. const [code, size] = out.trim().split(" ");
  189. if (code === "200" && Number(size) >= min) ok(`静态 ${path}`, `${size}b`);
  190. else bad(`静态 ${path}`, out.trim());
  191. }
  192. }
  193. async function main() {
  194. console.log("=== hxcs 完整验证开始 ===", new Date().toISOString());
  195. await testStaticAssets();
  196. const browser = await chromium.launch({ headless: true });
  197. const page = await browser.newPage();
  198. const errs = [];
  199. page.on("console", (m) => { if (m.type() === "error" && !/recycleToken|favicon/.test(m.text())) errs.push(m.text()); });
  200. try {
  201. await login(page);
  202. await testTemplates(page);
  203. await testGuestAccount(page);
  204. await testArticleAdd(page);
  205. await testFlowSubmit(page);
  206. } catch (e) {
  207. bad("验证异常", e.message);
  208. }
  209. if (errs.length) bad("Console", errs.slice(0, 2).join(" | "));
  210. else ok("Console", "无阻断 error");
  211. await browser.close();
  212. const pass = results.filter((r) => r.ok).length;
  213. console.log(`\n=== 完成 ${pass}/${results.length} 通过 ===`);
  214. results.filter((r) => !r.ok).forEach((r) => console.log(` ✗ ${r.n}: ${r.d}`));
  215. process.exit(results.some((r) => !r.ok) ? 1 : 0);
  216. }
  217. main().catch((e) => { console.error(e); process.exit(1); });