workflow-snippet.test.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // 单独运行:node --test <此文件>;前置为 dev Vite/Electron 已启动(默认 CDP 9229)。
  2. // 独立页面挂真实 renderer,验证上方摘要交互;不声称覆盖 CLI 审批传输。
  3. import assert from "node:assert/strict";
  4. import test from "node:test";
  5. import { fileURLToPath } from "node:url";
  6. import { chromium } from "playwright-core";
  7. test("WFS03 snippet 三态内容与展开交互", async () => {
  8. const targets = await (
  9. await fetch(`${process.env.ZCODE_E2E_CDP_URL ?? "http://localhost:9229"}/json/list`)
  10. ).json();
  11. const app = targets.find((target) => target.type === "page" && target.url.includes("localhost:"));
  12. assert.ok(app);
  13. const origin = new URL(app.url).origin;
  14. const connection = await chromium.launch({ headless: true, channel: "chrome" });
  15. const context = await connection.newContext();
  16. let page;
  17. try {
  18. const fixture = fileURLToPath(
  19. new URL("../../../../../../ui/test/fixtures/workflow-snippet.fixture.tsx", import.meta.url),
  20. );
  21. page = await context.newPage();
  22. await page.route("**/__workflow_snippet_smoke", (route) =>
  23. route.fulfill({
  24. contentType: "text/html",
  25. body: `<div id="root"></div><script type="module">import RefreshRuntime from '/@react-refresh'; RefreshRuntime.injectIntoGlobalHook(window); window.$RefreshReg$=()=>{}; window.$RefreshSig$=()=>type=>type; window.__vite_plugin_react_preamble_installed__=true; document.documentElement.classList.toggle('dark', matchMedia('(prefers-color-scheme: dark)').matches); await import('/@fs/${fixture}');</script>`,
  26. }),
  27. );
  28. for (const width of [1200, 390]) {
  29. await page.setViewportSize({ width, height: 800 });
  30. await page.emulateMedia({ colorScheme: width === 1200 ? "dark" : "light" });
  31. await page.goto(`${origin}/__workflow_snippet_smoke`);
  32. const summary = page.locator(".group\\/tool-summary");
  33. await summary.waitFor();
  34. // 2026-09-12 修订:进行中按整秒显示(0s/1s…),精确毫秒只在终态出现。
  35. assert.match(await summary.innerText(), /Running workflow snippet.*\d+s\b/s);
  36. assert.doesNotMatch(await summary.innerText(), /\d+ms/);
  37. await summary.click();
  38. await page.locator("pre").first().waitFor();
  39. assert.equal(
  40. await page
  41. .getByTestId("workflow-snippet-body")
  42. .locator("[data-language]")
  43. .first()
  44. .getAttribute("data-language"),
  45. "typescript",
  46. );
  47. assert.equal(await page.getByTestId("snippet-result").count(), 0);
  48. await page.getByTestId("complete").click();
  49. assert.equal(
  50. (await summary.innerText()).replace(/\s+/g, " ").trim(),
  51. "Workflow snippet · 427ms",
  52. );
  53. await page.getByTestId("snippet-result").waitFor();
  54. await page
  55. .getByTestId("snippet-result")
  56. .getByText(/package.json/)
  57. .first()
  58. .waitFor();
  59. assert.equal(
  60. await page
  61. .getByTestId("workflow-snippet-body")
  62. .locator("[data-language]")
  63. .first()
  64. .getAttribute("data-language"),
  65. "json",
  66. );
  67. assert.equal(await page.getByTestId("snippet-code").count(), 0);
  68. assert.equal(await page.getByTestId("snippet-logs").count(), 0);
  69. assert.equal(
  70. await page.getByTestId("workflow-snippet-body").locator("h4, summary").count(),
  71. 0,
  72. );
  73. assert.doesNotMatch(
  74. await page.getByTestId("workflow-snippet-body").innerText(),
  75. /completed in|Return value:|427ms|PASS/,
  76. );
  77. assert.match(await page.getByTestId("workflow-snippet-body").getAttribute("class"), /mb-2/);
  78. assert.match(
  79. await page.getByTestId("workflow-snippet-body").locator(".max-h-80").getAttribute("class"),
  80. /max-h-80/,
  81. );
  82. const copy = page.getByTestId("snippet-result").getByRole("button", { name: "Copy code" });
  83. assert.equal(await copy.count(), 1);
  84. if (process.env.ZCODE_E2E_SCREENSHOT_PREFIX) {
  85. await page.screenshot({
  86. animations: "disabled",
  87. path: `${process.env.ZCODE_E2E_SCREENSHOT_PREFIX}-${width}.png`,
  88. });
  89. }
  90. await page.getByTestId("fail").click();
  91. assert.match(await summary.innerText(), /Failed|Execution failed/);
  92. assert.doesNotMatch(await summary.innerText(), /427ms|TIMEOUT/);
  93. await page
  94. .getByTestId("snippet-result")
  95. .getByText(/deadline exceeded/)
  96. .first()
  97. .waitFor();
  98. assert.equal(
  99. (await page.getByTestId("workflow-snippet-body").innerText()).match(/read 2 files/g)
  100. ?.length,
  101. 1,
  102. );
  103. }
  104. } finally {
  105. await page?.close();
  106. await context.close();
  107. await connection.close();
  108. }
  109. });