_e2e_internal_probe.cjs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Temporary internal E2E probe (NOT committed; test-closure artifact only).
  3. * Reuses tests/e2e/helpers/auth.js loginAs for admin, then verifies the
  4. * authenticated /admin dashboard and the /frontend SPA render.
  5. * Credentials are injected from local machine.json via MACHINE_JSON_PATH;
  6. * nothing sensitive is printed.
  7. */
  8. const { chromium } = require('@playwright/test');
  9. const path = require('path');
  10. const BASE = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  11. const MACHINE_JSON = process.env.MACHINE_JSON_PATH;
  12. async function main() {
  13. const summary = { base: BASE, steps: {}, env: { machineJsonSet: !!MACHINE_JSON } };
  14. const launchOpts = { headless: true, args: ['--no-sandbox'] };
  15. if (process.env.CHROME_PATH) launchOpts.executablePath = process.env.CHROME_PATH;
  16. const browser = await chromium.launch(launchOpts);
  17. try {
  18. const auth = require(path.join(__dirname, 'tests', 'e2e', 'helpers', 'auth'));
  19. // loginAs reads MACHINE_JSON_PATH at require time (already set via env).
  20. // 1) Admin login
  21. const adminAuth = await auth.loginAs(browser, 'admin');
  22. summary.steps.adminLogin = {
  23. ok: !!(adminAuth && adminAuth.token),
  24. tokenLen: adminAuth && adminAuth.token ? adminAuth.token.length : 0,
  25. error: adminAuth && adminAuth.error ? String(adminAuth.error).slice(0, 120) : null
  26. };
  27. // 2) Authenticated /admin dashboard
  28. if (adminAuth && adminAuth.token) {
  29. const ctx = await browser.newContext();
  30. await ctx.addInitScript((t) => {
  31. try { localStorage.setItem('token', t); localStorage.setItem('access_token', t); } catch (e) {}
  32. }, adminAuth.token);
  33. const page = await ctx.newPage();
  34. const resp = await page.goto(BASE + '/admin', { waitUntil: 'domcontentloaded', timeout: 30000 });
  35. await page.waitForTimeout(2500);
  36. const title = await page.title();
  37. const hasSidebar = await page.locator('.sidebar-container, .el-menu, .el-aside, .el-container').count();
  38. summary.steps.adminDashboard = {
  39. ok: resp ? resp.status() < 500 : false,
  40. status: resp ? resp.status() : null,
  41. title: String(title).slice(0, 80),
  42. hasSidebar: hasSidebar > 0,
  43. url: page.url().slice(0, 100)
  44. };
  45. await ctx.close();
  46. } else {
  47. summary.steps.adminDashboard = { ok: false, note: 'skipped: no admin token' };
  48. }
  49. // 3) /frontend SPA
  50. const fctx = await browser.newContext();
  51. const fpage = await fctx.newPage();
  52. const fresp = await fpage.goto(BASE + '/frontend', { waitUntil: 'domcontentloaded', timeout: 30000 });
  53. await fpage.waitForTimeout(2500);
  54. const ftitle = await fpage.title();
  55. const fbody = (await fpage.content()).length;
  56. summary.steps.frontend = {
  57. ok: fresp ? fresp.status() < 500 : false,
  58. status: fresp ? fresp.status() : null,
  59. title: String(ftitle).slice(0, 80),
  60. bodyLen: fbody,
  61. url: fpage.url().slice(0, 100)
  62. };
  63. await fctx.close();
  64. } catch (e) {
  65. summary.fatal = String(e && e.message ? e.message : e).slice(0, 300);
  66. } finally {
  67. await browser.close();
  68. }
  69. require('fs').writeFileSync(path.join(__dirname, '_e2e_probe_result.json'),
  70. JSON.stringify(summary, null, 2));
  71. console.log(JSON.stringify(summary, null, 2));
  72. }
  73. main().then(() => process.exit(0)).catch((e) => { console.error(String(e).slice(0,300)); process.exit(1); });