share-content.mjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SHARE-WF02:fixture 只拦截网络,不访问真实账号、分享或模型。
  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 blocks = JSON.parse(
  7. await readFile(resolve("packages/services/test/fixtures/share-content-blocks.json"), "utf8"),
  8. );
  9. for (const artifact of blocks[0].payload.artifacts) {
  10. if (artifact.kind === "file" || artifact.kind === "markdown")
  11. artifact.preview_text = `# VERIFIED_DOCUMENT_${artifact.resource_id.endsWith("1") ? "1" : "2"}\n\n实际交付文档正文。`;
  12. }
  13. const baseUrl = process.env.SHARE_E2E_BASE_URL || "http://127.0.0.1:5186";
  14. const artifactDir = process.env.SHARE_E2E_ARTIFACTS || "/tmp/share-content-e2e";
  15. await mkdir(artifactDir, { recursive: true });
  16. const base = { turnId: "share-turn-1", productTurnId: "share-product-turn-1", createdAt: 1 };
  17. const rows = [
  18. {
  19. ...base,
  20. rowId: 1,
  21. createdAtSeq: 1,
  22. kind: "turnHeader",
  23. origin: "userInput",
  24. state: "completedSuccess",
  25. startedAt: 1,
  26. },
  27. {
  28. ...base,
  29. rowId: 2,
  30. createdAtSeq: 2,
  31. kind: "assistantText",
  32. state: "complete",
  33. text: "SHARE_WF02_FINAL 正文保留。http://127.0.0.1:8080",
  34. },
  35. {
  36. ...base,
  37. rowId: 3,
  38. createdAtSeq: 3,
  39. kind: "assistantText",
  40. state: "complete",
  41. text: "SHARE_WF02_FALLBACK 工作流已完成,包含进度看板和交付文档。",
  42. },
  43. ];
  44. rows.push({
  45. ...base,
  46. rowId: 4,
  47. createdAtSeq: 4,
  48. kind: "assistantText",
  49. state: "complete",
  50. text: "EMPTY_WORKFLOW_FALLBACK",
  51. });
  52. blocks.push({
  53. ...blocks[0],
  54. id: "share-content-2",
  55. fallback_row_id: 4,
  56. resource_ids: [],
  57. payload: {
  58. name: "Unavailable workflow",
  59. completion: false,
  60. unavailable: "source_unavailable",
  61. artifacts: [],
  62. },
  63. });
  64. const artifacts = [1, 2].map((index) => ({
  65. artifact_id: `share-artifact-${index}`,
  66. logical_artifact_key: `share-key-${index}`,
  67. producer_product_turn_id: base.productTurnId,
  68. artifact_version: 1,
  69. state: "current",
  70. ref: `zcode-artifact://share/share-artifact-${index}`,
  71. artifact_type: "text",
  72. display_name: `report-${index}.md`,
  73. extension: "md",
  74. mime_type: "text/markdown",
  75. size_bytes: 12,
  76. sha256: "a".repeat(64),
  77. url: `https://resources.invalid/report-${index}.md`,
  78. url_expires_at: 9999999999999,
  79. }));
  80. const preview = {
  81. schema_version: 1,
  82. share: {
  83. title: "SHARE-WF02",
  84. access_mode: "public_readonly",
  85. created_at: 1,
  86. expires_at: 9999999999999,
  87. },
  88. rows,
  89. content_blocks: blocks,
  90. artifacts,
  91. integrity: {
  92. projection_sha256: "a".repeat(64),
  93. artifact_set_sha256: "b".repeat(64),
  94. content_blocks_sha256: "c".repeat(64),
  95. },
  96. };
  97. const browser = await chromium.launch({ channel: "chrome", headless: true });
  98. try {
  99. for (const locale of ["cn/share", "share"])
  100. for (const width of [375, 1280])
  101. for (const theme of ["zai-light", "zai-dark"]) {
  102. const context = await browser.newContext({
  103. viewport: { width, height: 1000 },
  104. reducedMotion: "reduce",
  105. });
  106. await context.addInitScript((value) => localStorage.setItem("zcode-theme", value), theme);
  107. const page = await context.newPage();
  108. const errors = [];
  109. const unexpected = [];
  110. let version = 2;
  111. page.on(
  112. "pageerror",
  113. (error) => errors.length < 5 && errors.push(error.stack ?? error.message),
  114. );
  115. await page.route("**/*", async (route) => {
  116. const url = new URL(route.request().url());
  117. if (url.pathname.endsWith("/shares/content-e2e/preview"))
  118. return route.fulfill({
  119. contentType: "application/json",
  120. body: JSON.stringify({
  121. code: 0,
  122. msg: "",
  123. data: {
  124. ...preview,
  125. content_blocks: blocks.map((block) => ({ ...block, version })),
  126. },
  127. }),
  128. });
  129. if (url.hostname === "resources.invalid") {
  130. unexpected.push("CORS-dependent artifact fetch");
  131. return route.abort();
  132. }
  133. // Vite 的资源 base 固定 /cn/share/;部署的英文路径也回退到同一份 index.html。
  134. if (
  135. url.origin === new URL(baseUrl).origin &&
  136. url.pathname.startsWith("/share/") &&
  137. route.request().isNavigationRequest()
  138. ) {
  139. const response = await route.fetch({ url: `${baseUrl}/cn${url.pathname}` });
  140. return route.fulfill({ response });
  141. }
  142. if (url.origin === new URL(baseUrl).origin) return route.continue();
  143. unexpected.push(url.pathname);
  144. return route.abort();
  145. });
  146. await page.goto(`${baseUrl}/${locale}/content-e2e`);
  147. await page.locator("[data-share-content-block]").waitFor();
  148. assert.equal(await page.locator("[data-share-content-block]").count(), 1);
  149. assert.equal(await page.getByText("EMPTY_WORKFLOW_FALLBACK", { exact: true }).count(), 0);
  150. assert.match(await page.locator("body").innerText(), /SHARE_WF02_FINAL/u);
  151. assert.equal(await page.locator("[data-share-local-website]").count(), 0);
  152. for (const title of ["军棋项目进度看板", "结果表格", "性能图表", "验收指标"]) {
  153. await page.locator("button").filter({ hasText: title }).first().click();
  154. if (title === "军棋项目进度看板")
  155. await page.getByText("模块 14", { exact: true }).waitFor();
  156. if (title === "验收指标") await page.getByText("43", { exact: true }).waitFor();
  157. await page.keyboard.press("Escape");
  158. }
  159. for (const [index, title] of ["军棋项目交付报告", "军棋游戏启动说明"].entries()) {
  160. await page.locator("button").filter({ hasText: title }).first().click();
  161. await page.getByRole("heading", { name: `VERIFIED_DOCUMENT_${index + 1}` }).waitFor();
  162. await page.keyboard.press("Escape");
  163. }
  164. const overflow = await page.evaluate(
  165. () => document.documentElement.scrollWidth > innerWidth + 1,
  166. );
  167. assert.equal(overflow, false, `${locale}/${width}/${theme} overflow`);
  168. await page.screenshot({
  169. path: `${artifactDir}/${locale.replaceAll("/", "-")}-${width}-${theme}.png`,
  170. fullPage: true,
  171. });
  172. version = 99;
  173. await page.reload();
  174. await page.locator("[data-share-content-fallback]").first().waitFor();
  175. // content-visibility:auto 会跳过视口外布局,先实际滚到正文再验证可见。
  176. await page.getByText(/SHARE_WF02_FINAL/u).scrollIntoViewIfNeeded();
  177. await page.getByText(/SHARE_WF02_FINAL/u).waitFor();
  178. await page.getByText(/SHARE_WF02_FALLBACK/u).scrollIntoViewIfNeeded();
  179. await page.getByText(/SHARE_WF02_FALLBACK/u).waitFor();
  180. assert.deepEqual(errors, []);
  181. assert.deepEqual(unexpected, []);
  182. await context.close();
  183. }
  184. console.log(
  185. "SHARE-WF02: 8 browser variants passed (content, files, fallback, isolation, overflow)",
  186. );
  187. } finally {
  188. await browser.close();
  189. }