workflow-file-actions.test.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // 浏览器组件回归:真实产物头部、字节读取 hook 与浏览器下载;会话 API 使用固定夹具。
  2. import assert from "node:assert/strict";
  3. import { before, after, test } from "node:test";
  4. import { fileURLToPath } from "node:url";
  5. import { mkdir, readFile } from "node:fs/promises";
  6. import { join } from "node:path";
  7. import { createServer } from "vite";
  8. import react from "@vitejs/plugin-react";
  9. import tailwindcss from "@tailwindcss/vite";
  10. import { chromium } from "playwright-core";
  11. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  12. const entry = "/packages/ui/test/browser/manual-review/pending/workflow-file-actions.fixture.tsx";
  13. const artifacts = join(root, "outputs/review-retry-20261021/browser-file-actions");
  14. const mocks = new Map([
  15. [
  16. "@/v4/V4ConversationContext.js",
  17. `
  18. const conversation = {
  19. layer: { acquire: () => ({ release() {} }) },
  20. workflowRunArtifacts: async () => ({ artifacts: [{
  21. id: 'review-file', kind: 'file', title: 'Review file', version: 1,
  22. sourcePath: 'out/review.txt', contentType: 'text/plain', bytes: 6,
  23. versions: [{version: 1, publishedAt: 1, bytes: 6}], itemCount: 0
  24. }] }),
  25. workflowRunArtifactRead: async () => ({
  26. dataBase64: 'cmV2aWV3', totalBytes: 6, nextOffset: null, mediaType: 'text/plain'
  27. }),
  28. workflowRunArtifactData: async () => ({items: [], hasMore: false})
  29. };
  30. export const useV4Conversation = () => conversation;
  31. export const V4PaneConversationProvider = ({children}) => children;
  32. `,
  33. ],
  34. [
  35. "@/v4/useConversationProjection.js",
  36. "export const useConversationProjection = () => ({snapshot: null});",
  37. ],
  38. // 查看器已由独立用例覆盖,此处只隔离重量级正文,保留真实头部与平台动作。
  39. [
  40. "@/app-shell/workflow-artifacts/WorkflowArtifactBody.js",
  41. "export const WorkflowArtifactBody = () => null;",
  42. ],
  43. ]);
  44. let server, browser, origin;
  45. before(async () => {
  46. await mkdir(artifacts, { recursive: true });
  47. server = await createServer({
  48. root,
  49. configFile: false,
  50. plugins: [
  51. {
  52. name: "workflow-file-actions-fixture",
  53. enforce: "pre",
  54. resolveId(id) {
  55. const source = id.replace(`${root}packages/ui/src/`, "@/");
  56. if (mocks.has(source)) return `\0fixture:${source}`;
  57. },
  58. load(id) {
  59. if (id.startsWith("\0fixture:")) return mocks.get(id.slice(9));
  60. },
  61. configureServer(vite) {
  62. vite.middlewares.use(async (req, res, next) => {
  63. if (!req.url?.startsWith("/fixture?")) return next();
  64. res.setHeader("Content-Type", "text/html");
  65. res.end(
  66. await vite.transformIndexHtml(
  67. req.url,
  68. `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"/></head><body><div id="root"></div><script type="module" src="${entry}"></script></body></html>`,
  69. ),
  70. );
  71. });
  72. },
  73. },
  74. react(),
  75. tailwindcss(),
  76. ],
  77. resolve: { alias: { "@": `${root}packages/ui/src` } },
  78. optimizeDeps: { entries: [entry.slice(1)], exclude: [...mocks.keys()] },
  79. server: { host: "127.0.0.1", port: 0, hmr: false },
  80. });
  81. await server.listen();
  82. origin = server.resolvedUrls.local[0];
  83. browser = await chromium.launch({ channel: "chrome", headless: true });
  84. });
  85. after(async () => {
  86. await browser?.close();
  87. await server?.close();
  88. });
  89. for (const width of [390, 1200])
  90. for (const theme of ["light", "dark"])
  91. for (const locale of ["zh-CN", "en-US"])
  92. test(`WORK-FILE-01/02 ${width}/${theme}/${locale}`, async () => {
  93. const context = await browser.newContext({
  94. viewport: { width, height: 844 },
  95. acceptDownloads: true,
  96. isMobile: width === 390,
  97. hasTouch: width === 390,
  98. });
  99. const page = await context.newPage();
  100. const errors = [];
  101. page.on("pageerror", (error) => errors.push(String(error)));
  102. try {
  103. await page.goto(`${origin}fixture?theme=${theme}&locale=${locale}`);
  104. const save = page.getByTestId("workflow-artifact-save-as");
  105. await save.waitFor();
  106. await page.waitForFunction(
  107. () => !document.querySelector('[data-testid="workflow-artifact-save-as"]')?.disabled,
  108. );
  109. await page.getByTestId("workflow-artifact-reveal").click();
  110. assert.equal(
  111. await page.getAttribute("body", "data-revealed"),
  112. "/workspace/out/review.txt",
  113. );
  114. await page.clock.install();
  115. await page.evaluate(() => {
  116. const original = URL.createObjectURL.bind(URL);
  117. URL.createObjectURL = (blob) => {
  118. const url = original(blob);
  119. document.body.dataset.downloadUrl = url;
  120. return url;
  121. };
  122. });
  123. const downloaded = page.waitForEvent("download");
  124. await save.click();
  125. const download = await downloaded;
  126. assert.match(download.suggestedFilename(), /\.txt$/);
  127. assert.equal(await readFile(await download.path(), "utf8"), "review");
  128. await page.clock.runFor(0);
  129. assert.equal(
  130. await page.evaluate(async () =>
  131. (await fetch(document.body.dataset.downloadUrl)).text(),
  132. ),
  133. "review",
  134. );
  135. await page.clock.runFor(40_000);
  136. assert.equal(
  137. await page.evaluate(async () => {
  138. try {
  139. await fetch(document.body.dataset.downloadUrl);
  140. return false;
  141. } catch {
  142. return true;
  143. }
  144. }),
  145. true,
  146. );
  147. assert.deepEqual(errors, []);
  148. await page.screenshot({ path: join(artifacts, `${width}-${theme}-${locale}.png`) });
  149. } finally {
  150. await context.close();
  151. }
  152. });