share-import-selection.test.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import assert from "node:assert/strict";
  2. import { before, after, test } from "node:test";
  3. import { fileURLToPath } from "node:url";
  4. import { mkdir, writeFile } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { tmpdir } from "node:os";
  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/share-import-selection.fixture.tsx";
  13. const artifacts = join(tmpdir(), "todo139-share-selection");
  14. let server, browser, origin;
  15. before(async () => {
  16. await mkdir(artifacts, { recursive: true });
  17. server = await createServer({
  18. root,
  19. configFile: false,
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "share-selection-fixture",
  25. configureServer(vite) {
  26. vite.middlewares.use(async (req, res, next) => {
  27. if (!req.url?.startsWith("/fixture?")) return next();
  28. res.setHeader("Content-Type", "text/html");
  29. res.end(
  30. await vite.transformIndexHtml(
  31. req.url,
  32. `<!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>`,
  33. ),
  34. );
  35. });
  36. },
  37. },
  38. ],
  39. resolve: { alias: { "@": `${root}packages/ui/src` } },
  40. optimizeDeps: { entries: [entry.slice(1)] },
  41. server: { host: "127.0.0.1", port: 0 },
  42. });
  43. await server.listen();
  44. origin = server.resolvedUrls.local[0];
  45. browser = await chromium.launch(
  46. process.env.CHROME_EXECUTABLE_PATH
  47. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  48. : { channel: "chrome" },
  49. );
  50. });
  51. after(async () => {
  52. await browser?.close();
  53. await server?.close();
  54. });
  55. for (const source of ["root", "recent", "default", "empty", "delayed"])
  56. for (const width of [390, 1200])
  57. test(`SHARE30/31/32 ${source} ${width}`, async () => {
  58. const page = await browser.newPage({ viewport: { width, height: 800 } });
  59. const errors = [],
  60. logs = [];
  61. page.on("pageerror", (error) => errors.push(String(error)));
  62. page.on("console", (message) => logs.push(message.text()));
  63. page.setDefaultTimeout(15000);
  64. const getConfig = async () => JSON.parse(await page.getByTestId("config").textContent());
  65. try {
  66. await page.goto(
  67. `${origin}fixture?source=${source}&locale=${width === 390 ? "zh-CN" : "en-US"}&theme=${width === 390 ? "light" : "dark"}`,
  68. );
  69. await page.getByRole("button", { name: "Import", exact: true }).click();
  70. assert.equal(await page.getByTestId("content").textContent(), "IMPORTED CONTENT");
  71. if (source === "delayed") {
  72. assert.equal((await getConfig()).modelSelection, undefined);
  73. await page.getByLabel("Message").fill("typed before Registry");
  74. await page.getByRole("button", { name: "Registry ready" }).click();
  75. }
  76. if (source === "empty") {
  77. await page.waitForFunction(
  78. () =>
  79. JSON.parse(document.querySelector('[data-testid="config"]').textContent).mode ===
  80. "plan",
  81. );
  82. assert.equal((await getConfig()).modelSelection, undefined);
  83. assert.equal(
  84. await page.getByRole("button", { name: "Submit", exact: true }).isDisabled(),
  85. true,
  86. );
  87. } else {
  88. await page.waitForFunction(
  89. () =>
  90. JSON.parse(document.querySelector('[data-testid="config"]').textContent)
  91. .modelSelection?.options?.reasoningLevel === "high",
  92. );
  93. assert.equal(
  94. (await getConfig()).mode,
  95. source === "root" ? "plan" : source === "recent" ? "yolo" : "build",
  96. );
  97. assert.match(
  98. await page.getByTestId("chat-model-select-trigger").innerText(),
  99. /recipient-model/,
  100. );
  101. assert.match(
  102. await page.getByTestId("chat-thought-level-select-trigger").innerText(),
  103. /High|高/,
  104. );
  105. await page.getByRole("button", { name: "Submit", exact: true }).click();
  106. assert.deepEqual(JSON.parse(await page.getByTestId("submission").textContent()), {
  107. modelSelection: (await getConfig()).modelSelection,
  108. mode: (await getConfig()).mode,
  109. });
  110. }
  111. if (source === "root" || source === "empty")
  112. assert.match(await page.getByTestId("root-draft").textContent(), /ROOT MUST STAY/);
  113. const imported = await getConfig();
  114. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  115. assert.deepEqual(await getConfig(), imported);
  116. await page.screenshot({ path: join(artifacts, `${source}-${width}.png`) });
  117. await page.getByRole("button", { name: "Ordinary new task" }).click();
  118. await page.waitForFunction(
  119. (expected) => document.querySelector('[data-testid="config"]').textContent === expected,
  120. JSON.stringify(imported),
  121. );
  122. if (source !== "empty") {
  123. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  124. await page.getByTestId("chat-thought-level-select-trigger").click();
  125. await page.getByRole("option", { name: /Low|低/, exact: false }).click();
  126. await page.waitForFunction(
  127. () =>
  128. JSON.parse(document.querySelector('[data-testid="config"]').textContent)
  129. .modelSelection?.options?.reasoningLevel === "low",
  130. );
  131. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  132. assert.equal((await getConfig()).modelSelection.options.reasoningLevel, "low");
  133. // 先前提交的快照不随之后改档位变化;重新打开也不重复初始化。
  134. assert.equal(
  135. JSON.parse(await page.getByTestId("submission").textContent()).modelSelection.options
  136. .reasoningLevel,
  137. "high",
  138. );
  139. }
  140. assert.deepEqual(errors, []);
  141. } finally {
  142. await writeFile(
  143. join(artifacts, `${source}-${width}.log`),
  144. JSON.stringify({ errors, logs }, null, 2),
  145. );
  146. await page.close();
  147. }
  148. });