share-workflow.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SHARE-WF01:先启动 pnpm dev:web-share:test;API 由本测试拦截,不访问真实分享或账号。
  2. import assert from "node:assert/strict";
  3. import { readFile, mkdir } from "node:fs/promises";
  4. import { resolve } from "node:path";
  5. import { chromium } from "playwright-core";
  6. const fixtures = JSON.parse(
  7. await readFile(resolve("packages/services/test/fixtures/workflow-displays.json"), "utf8"),
  8. );
  9. const baseUrl = process.env.SHARE_E2E_BASE_URL || "http://localhost:5173";
  10. const artifacts = process.env.SHARE_E2E_ARTIFACTS || "/tmp/share-workflow-e2e";
  11. await mkdir(artifacts, { recursive: true });
  12. const names = [
  13. "CreateWorkflow",
  14. "GetWorkflowRun",
  15. "ListWorkflowRuns",
  16. "EvalWorkflowSnippet",
  17. "ListSavedWorkflows",
  18. "ListModels",
  19. "ResumeWorkflowRun",
  20. ];
  21. const base = { turnId: "share-turn-1", productTurnId: "share-product-turn-1", createdAt: 1 };
  22. const rows = [
  23. {
  24. ...base,
  25. rowId: 1,
  26. createdAtSeq: 1,
  27. kind: "turnHeader",
  28. origin: "userInput",
  29. state: "completedSuccess",
  30. startedAt: 1,
  31. },
  32. {
  33. ...base,
  34. rowId: 2,
  35. createdAtSeq: 2,
  36. kind: "userInput",
  37. origin: "realUser",
  38. text: "SHARE-WF01 workflow snapshot",
  39. },
  40. ...fixtures.map((display, index) => ({
  41. ...base,
  42. rowId: index + 3,
  43. createdAtSeq: index + 3,
  44. kind: "toolCall",
  45. toolCallId: `wf-${index}`,
  46. toolName: names[index],
  47. status: "success",
  48. inputText: "{}",
  49. input: { name: "调研工作流" },
  50. output: { text: "Workflow output preserved" },
  51. display,
  52. })),
  53. {
  54. ...base,
  55. rowId: 10,
  56. createdAtSeq: 10,
  57. kind: "toolCall",
  58. toolCallId: "wf-failed",
  59. toolName: "CreateWorkflow",
  60. status: "success",
  61. inputText: "{}",
  62. display: {
  63. kind: "create_workflow",
  64. ok: false,
  65. errorCount: 1,
  66. diagnostics: [{ line: 2, column: 1, code: 100, message: "WF_COMPILE_DIAGNOSTIC" }],
  67. },
  68. },
  69. {
  70. ...base,
  71. rowId: 11,
  72. createdAtSeq: 11,
  73. kind: "toolCall",
  74. toolCallId: "wf-legacy",
  75. toolName: "CreateWorkflow",
  76. status: "success",
  77. inputText: "{}",
  78. output: { text: "WF_LEGACY_OUTPUT" },
  79. },
  80. ];
  81. // 构造符合真实图语义的最小静态阶段,其他完整字段在两端契约测试中覆盖。
  82. rows[2].display.diagnostics = [];
  83. rows[2].display.causalityGraph.handoffs = [];
  84. rows[2].display.causalityGraph.phaseEdges = [];
  85. delete rows[2].display.causalityGraph.phases[0].alongside;
  86. const preview = {
  87. schema_version: 1,
  88. share: {
  89. title: "Workflow share",
  90. access_mode: "public_readonly",
  91. created_at: 1,
  92. expires_at: 9999999999999,
  93. },
  94. rows,
  95. artifacts: [],
  96. integrity: { projection_sha256: "a".repeat(64), artifact_set_sha256: "b".repeat(64) },
  97. };
  98. const browser = await chromium.launch({ channel: "chrome", headless: true });
  99. try {
  100. for (const locale of ["cn/share", "share"]) {
  101. for (const width of [375, 1280]) {
  102. for (const theme of ["zai-light", "zai-dark"]) {
  103. const context = await browser.newContext({ viewport: { width, height: 900 } });
  104. await context.addInitScript((theme) => localStorage.setItem("zcode-theme", theme), theme);
  105. const page = await context.newPage();
  106. const errors = [];
  107. const unexpected = [];
  108. let previews = 0;
  109. page.on("pageerror", (error) => errors.push(error.message));
  110. await page.route("**/*", async (route) => {
  111. const request = route.request();
  112. if (new URL(request.url()).pathname.endsWith("/shares/workflow-e2e/preview")) {
  113. previews++;
  114. assert.equal(request.method(), "GET");
  115. return route.fulfill({
  116. contentType: "application/json",
  117. body: JSON.stringify({ code: 0, msg: "", data: preview }),
  118. });
  119. }
  120. if (new URL(request.url()).origin === new URL(baseUrl).origin) return route.continue();
  121. unexpected.push(`${request.method()} ${new URL(request.url()).pathname}`);
  122. return route.abort();
  123. });
  124. await page.goto(`${baseUrl}/${locale}/workflow-e2e`);
  125. await page
  126. .locator('[data-conversation-share-workflow="create_workflow"]')
  127. .first()
  128. .waitFor();
  129. for (const kind of [
  130. "create_workflow",
  131. "get_workflow_run",
  132. "list_workflow_runs",
  133. "eval_workflow_snippet",
  134. "saved_workflow_list",
  135. "resume_workflow_run",
  136. ]) {
  137. assert.ok(
  138. await page.locator(`[data-conversation-share-workflow="${kind}"]`).count(),
  139. kind,
  140. );
  141. }
  142. await page
  143. .locator('[data-conversation-share-workflow="create_workflow"]')
  144. .first()
  145. .screenshot({
  146. animations: "disabled",
  147. path: resolve(artifacts, `graph-${locale.replace("/", "-")}-${width}-${theme}.png`),
  148. });
  149. assert.ok(
  150. await page
  151. .getByTestId("workflow-card-name")
  152. .first()
  153. .evaluate((element) => element.getBoundingClientRect().width >= 80),
  154. "窄屏仍须显示工作流名称",
  155. );
  156. // 展开工具卡详情,验证诊断、结果与历史兜底都可读。
  157. for (const id of ["wf-1", "wf-2", "wf-3", "wf-4", "wf-6", "wf-failed", "wf-legacy"]) {
  158. const trigger = page.getByTestId(`tool-summary-trigger-${id}`);
  159. await trigger.click();
  160. }
  161. await page.getByText("WF_COMPILE_DIAGNOSTIC", { exact: false }).waitFor();
  162. await page.getByText("WF_LEGACY_OUTPUT", { exact: false }).waitFor();
  163. await page.getByTestId("workflow-run-summary-line").waitFor();
  164. assert.ok(await page.getByText("调研工作流", { exact: false }).count());
  165. assert.equal(await page.locator('[data-testid="workflow-card-resume"]').count(), 0);
  166. assert.equal(await page.locator('a[href^="file:"],a[href^="zcode:"]').count(), 0);
  167. assert.equal(
  168. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  169. true,
  170. );
  171. assert.deepEqual(errors, []);
  172. assert.deepEqual(unexpected, []);
  173. assert.equal(previews, 1);
  174. await page.screenshot({
  175. path: resolve(artifacts, `${locale.replace("/", "-")}-${width}-${theme}.png`),
  176. fullPage: true,
  177. });
  178. await context.close();
  179. console.log(`PASS SHARE-WF01 ${locale} ${width} ${theme}`);
  180. }
  181. }
  182. }
  183. } finally {
  184. await browser.close();
  185. }