share-import-selection.test.mjs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "build",
  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((await getConfig()).mode, source === "recent" ? "yolo" : "build");
  94. assert.match(
  95. await page.getByTestId("chat-model-select-trigger").innerText(),
  96. /recipient-model/,
  97. );
  98. assert.match(
  99. await page.getByTestId("chat-thought-level-select-trigger").innerText(),
  100. /High|高/,
  101. );
  102. await page.getByRole("button", { name: "Submit", exact: true }).click();
  103. assert.deepEqual(JSON.parse(await page.getByTestId("submission").textContent()), {
  104. modelSelection: (await getConfig()).modelSelection,
  105. mode: (await getConfig()).mode,
  106. planEnabled: (await getConfig()).planEnabled,
  107. });
  108. }
  109. if (source === "root" || source === "empty")
  110. assert.match(await page.getByTestId("root-draft").textContent(), /ROOT MUST STAY/);
  111. const imported = await getConfig();
  112. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  113. assert.deepEqual(await getConfig(), imported);
  114. await page.screenshot({ path: join(artifacts, `${source}-${width}.png`) });
  115. await page.getByRole("button", { name: "Ordinary new task" }).click();
  116. await page.waitForFunction(
  117. (expected) => document.querySelector('[data-testid="config"]').textContent === expected,
  118. JSON.stringify(imported),
  119. );
  120. if (source !== "empty") {
  121. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  122. await page.getByTestId("chat-thought-level-select-trigger").click();
  123. await page.getByRole("option", { name: /Low|低/, exact: false }).click();
  124. await page.waitForFunction(
  125. () =>
  126. JSON.parse(document.querySelector('[data-testid="config"]').textContent)
  127. .modelSelection?.options?.reasoningLevel === "low",
  128. );
  129. await page.getByRole("button", { name: "Reopen", exact: true }).click();
  130. assert.equal((await getConfig()).modelSelection.options.reasoningLevel, "low");
  131. // 先前提交的快照不随之后改档位变化;重新打开也不重复初始化。
  132. assert.equal(
  133. JSON.parse(await page.getByTestId("submission").textContent()).modelSelection.options
  134. .reasoningLevel,
  135. "high",
  136. );
  137. }
  138. assert.deepEqual(errors, []);
  139. } finally {
  140. await writeFile(
  141. join(artifacts, `${source}-${width}.log`),
  142. JSON.stringify({ errors, logs }, null, 2),
  143. );
  144. await page.close();
  145. }
  146. });
  147. for (const source of ["session-empty", "session-text"])
  148. for (const width of [390, 1200])
  149. test(`CS01/02/03/10 current session ${source} ${width}`, async () => {
  150. const page = await browser.newPage({ viewport: { width, height: 800 } });
  151. const errors = [],
  152. logs = [];
  153. page.on("pageerror", (error) => errors.push(String(error)));
  154. page.on("console", (message) => logs.push(message.text()));
  155. const config = async () => JSON.parse(await page.getByTestId("config").textContent());
  156. const check = async (mode, planEnabled) => {
  157. logs.push(`expect ${mode}/${planEnabled}; actual ${JSON.stringify(await config())}`);
  158. await page.waitForFunction(
  159. ({ mode, planEnabled }) => {
  160. const value = JSON.parse(document.querySelector('[data-testid="config"]').textContent);
  161. return value.mode === mode && value.planEnabled === planEnabled;
  162. },
  163. { mode, planEnabled },
  164. { timeout: 10000 },
  165. );
  166. };
  167. const chooseMode = async (mode) => {
  168. await page.getByTestId("chat-mode-select-trigger").click();
  169. await page.getByTestId(`chat-mode-select-item-${mode}`).click();
  170. await page.keyboard.press("Escape");
  171. await page.getByRole("menu").waitFor({ state: "hidden" });
  172. };
  173. try {
  174. await page.goto(
  175. `${origin}fixture?source=${source}&locale=${width === 390 ? "zh-CN" : "en-US"}&theme=${width === 390 ? "light" : "dark"}`,
  176. );
  177. await check("build", false);
  178. assert.equal(
  179. await page.getByLabel("Message").inputValue(),
  180. source === "session-text" ? "UNSENT TEXT" : "",
  181. );
  182. await chooseMode("yolo");
  183. await chooseMode("plan");
  184. await check("yolo", true);
  185. await page.getByRole("button", { name: "Repeat snapshot", exact: true }).click();
  186. await check("yolo", true);
  187. await page.getByRole("button", { name: "Ordinary new task", exact: true }).click();
  188. await page.getByRole("button", { name: "Open session", exact: true }).click();
  189. await check("build", false);
  190. await page.reload();
  191. await check("build", false);
  192. assert.equal(
  193. await page.getByLabel("Message").inputValue(),
  194. source === "session-text" ? "UNSENT TEXT" : "",
  195. );
  196. await page.getByRole("button", { name: "New grant", exact: true }).click();
  197. await check("yolo", false);
  198. await chooseMode("build");
  199. await page.getByRole("button", { name: "Repeat snapshot", exact: true }).click();
  200. await check("build", false);
  201. await page.getByRole("button", { name: "New plan", exact: true }).click();
  202. await check("build", true);
  203. assert.equal((await config()).modelSelection.modelId, "recipient-model");
  204. const storage = await page.evaluate(() => JSON.stringify(localStorage));
  205. assert.doesNotMatch(
  206. storage,
  207. /lastPermissionGrantId|lastPlanTransitionId|initializeFromNewTask/,
  208. );
  209. if (source === "session-empty") assert.doesNotMatch(storage, /composer-draft/);
  210. await page.screenshot({ path: join(artifacts, `${source}-${width}.png`) });
  211. assert.deepEqual(errors, []);
  212. } finally {
  213. await writeFile(
  214. join(artifacts, `${source}-${width}.log`),
  215. JSON.stringify({ errors, logs }, null, 2),
  216. );
  217. await page.close();
  218. }
  219. });