remote-abort-api-compat.mjs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ML08:真实 Web 入口移除静态 API,复用受控 HTTP/WS 验证配对到 bootstrap。
  2. // 启动:pnpm --filter @zcode/web exec vite --host 127.0.0.1 --port 5178 --strictPort
  3. // 运行:node packages/web/test/e2e/manual-review/pending/remote-abort-api-compat.mjs
  4. // 此用例保持 pending,不替代真机浏览器或生产 relay 验收。
  5. import assert from "node:assert/strict";
  6. import { mkdir, writeFile } from "node:fs/promises";
  7. import { resolve } from "node:path";
  8. import { chromium } from "playwright-core";
  9. import { setup, assertEventually } from "./remote-login-v2.helpers.mjs";
  10. const origin = process.env.REMOTE_LOGIN_E2E_ORIGIN || "http://127.0.0.1:5178";
  11. const artifacts = resolve(".tmp/remote-abort-api-compat-e2e");
  12. await mkdir(artifacts, { recursive: true });
  13. const browser = await chromium.launch({ channel: "chrome", headless: true });
  14. const results = [];
  15. try {
  16. for (const missing of [["any"], ["timeout"], ["any", "timeout"]])
  17. for (const entry of ["qr", "list"])
  18. for (const width of [390, 1024])
  19. for (const theme of ["zai-light", "zai-dark"]) {
  20. const locale = theme === "zai-light" ? "zh-CN" : "en-US";
  21. const h = await setup(browser, {
  22. width,
  23. theme,
  24. locale,
  25. signedIn: true,
  26. relayError: null,
  27. });
  28. const label = `${missing.join("-")}-${entry}-${width}-${theme}`;
  29. try {
  30. await h.context.addInitScript((names) => {
  31. for (const name of names)
  32. Object.defineProperty(AbortSignal, name, { configurable: true, value: undefined });
  33. // 同时防止修复误用另一项较新的实例方法。
  34. Object.defineProperty(AbortSignal.prototype, "throwIfAborted", { value: undefined });
  35. }, missing);
  36. const path =
  37. entry === "qr"
  38. ? "/remote/v4?sid=e2e-device&mid=e2e-mid&name=E2E_COMPAT&app_version=3.16.1"
  39. : "/remote/v4?app_version=3.16.1";
  40. await h.page.goto(`${origin}${path}`);
  41. if (entry === "list") {
  42. const device = h.page.getByRole("button", { name: /Alice’s MacBook/ });
  43. await device.waitFor();
  44. assert.equal(h.traffic.tokens, 0);
  45. assert.equal(h.traffic.sockets, 0);
  46. await h.page.screenshot({ path: resolve(artifacts, `${label}-list.png`) });
  47. await device.click();
  48. }
  49. await assertEventually(() => h.traffic.bootstraps === 1);
  50. assert.equal(h.traffic.lists, entry === "list" ? 1 : 0);
  51. assert.equal(h.traffic.tokens, 1);
  52. assert.equal(h.traffic.sockets, 1);
  53. assert.equal(new URL(h.page.url()).searchParams.get("sid"), "e2e-device");
  54. assert.deepEqual(
  55. await h.page.evaluate(
  56. (names) => names.map((name) => typeof AbortSignal[name]),
  57. missing,
  58. ),
  59. missing.map(() => "undefined"),
  60. );
  61. assert.deepEqual(h.errors, []);
  62. await h.page.screenshot({ path: resolve(artifacts, `${label}-paired.png`) });
  63. results.push({
  64. case: "ML08",
  65. missing,
  66. entry,
  67. width,
  68. theme,
  69. locale,
  70. traffic: h.traffic,
  71. result: "passed",
  72. });
  73. } catch (error) {
  74. await h.page.screenshot({ path: resolve(artifacts, `${label}-failed.png`) });
  75. results.push({
  76. case: "ML08",
  77. missing,
  78. entry,
  79. width,
  80. theme,
  81. traffic: h.traffic,
  82. errors: h.errors,
  83. result: "failed",
  84. });
  85. throw error;
  86. } finally {
  87. await h.context.close();
  88. }
  89. }
  90. } finally {
  91. await writeFile(resolve(artifacts, "results.json"), JSON.stringify(results, null, 2));
  92. await browser.close();
  93. }
  94. console.log(`Passed ${results.length} compatibility checks; evidence: ${artifacts}`);