thought-level-display.test.mjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/thought-level-display.fixture.tsx";
  13. const artifacts = join(tmpdir(), "todo153-thought-level-display");
  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: "thought-level-display-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. : {},
  49. );
  50. });
  51. after(async () => {
  52. await browser?.close();
  53. await server?.close();
  54. });
  55. for (const width of [390, 1200]) {
  56. for (const theme of ["light", "dark"]) {
  57. for (const locale of ["zh-CN", "en-US"]) {
  58. test(`TL-01/02 ${width}/${theme}/${locale}`, async () => {
  59. const page = await browser.newPage({
  60. viewport: { width, height: 850 },
  61. hasTouch: width === 390,
  62. });
  63. const errors = [];
  64. page.on("pageerror", (error) => errors.push(String(error)));
  65. page.setDefaultTimeout(10000);
  66. const activate = (locator) => (width === 390 ? locator.tap() : locator.click());
  67. const waitValue = async (value) => {
  68. await page.waitForFunction(
  69. (expected) =>
  70. document.querySelector('[data-testid="committed"]').textContent === expected,
  71. value,
  72. );
  73. assert.equal(await page.getByTestId("value").textContent(), value);
  74. };
  75. const label =
  76. locale === "zh-CN"
  77. ? [
  78. "关闭",
  79. "极低",
  80. "低",
  81. "中",
  82. "高",
  83. "极高",
  84. "最高",
  85. "极致",
  86. "极高",
  87. "极高",
  88. "Provider default",
  89. ]
  90. : [
  91. "Off",
  92. "Minimal",
  93. "Low",
  94. "Medium",
  95. "High",
  96. "Extra high",
  97. "Max",
  98. "Ultra",
  99. "Extra high",
  100. "Extra high",
  101. "Provider default",
  102. ];
  103. try {
  104. await page.goto(`${origin}fixture?${new URLSearchParams({ theme, locale })}`);
  105. const trigger = page.getByTestId("select").getByRole("combobox");
  106. await trigger.waitFor();
  107. assert.equal(await trigger.getAttribute("aria-label"), label[1]);
  108. assert.equal(await page.getByTestId("committed").textContent(), "");
  109. await activate(trigger);
  110. await page.getByRole("listbox").waitFor();
  111. assert.deepEqual(await page.getByRole("option").allTextContents(), label);
  112. await page.getByRole("listbox").evaluate(async (element) => {
  113. await Promise.allSettled(
  114. element.getAnimations({ subtree: true }).map((animation) => animation.finished),
  115. );
  116. });
  117. const box = await page.getByRole("listbox").boundingBox();
  118. assert.ok(box.x >= 0 && box.x + box.width <= width, JSON.stringify(box));
  119. await page.screenshot({
  120. animations: "disabled",
  121. path: join(artifacts, `${width}-${theme}-${locale}-menu.png`),
  122. });
  123. await activate(page.getByTestId("chat-thought-level-select-item-ultra"));
  124. await waitValue("ultra");
  125. assert.equal(await trigger.getAttribute("aria-label"), label[7]);
  126. // 使用生产快捷键 hook;这里的顺序能区分配置顺序与旧名称排序。
  127. await page.keyboard.press("Control+t");
  128. await waitValue("extra-high");
  129. assert.equal(await trigger.getAttribute("aria-expanded"), "false");
  130. await activate(page.getByTestId("cycle").getByRole("button"));
  131. await waitValue("extra_high");
  132. await page.keyboard.press("Control+t");
  133. await waitValue("default");
  134. assert.equal(await trigger.getAttribute("aria-label"), "Provider default");
  135. await page.keyboard.press("Control+t");
  136. await waitValue("nothink");
  137. assert.equal(await trigger.getAttribute("aria-label"), label[0]);
  138. const subagent = page.getByTestId("subagent").getByRole("combobox");
  139. assert.equal(await subagent.getAttribute("aria-label"), label[0]);
  140. await activate(subagent);
  141. await page.getByRole("listbox").waitFor();
  142. assert.deepEqual(await page.getByRole("option").allTextContents(), label);
  143. await activate(page.getByTestId("chat-thought-level-select-item-minimal"));
  144. await waitValue("minimal");
  145. assert.equal(await subagent.getAttribute("aria-label"), label[1]);
  146. // 桌面用真实键盘选择;手机用触控,均应提交 low 原值。
  147. if (width === 1200) {
  148. await trigger.focus();
  149. await page.keyboard.press("ArrowDown");
  150. await page.getByRole("listbox").waitFor();
  151. // Radix 在定位完成后聚焦当前项,需等实际焦点落定再发送下一次按键。
  152. await page.waitForFunction(
  153. () =>
  154. document.activeElement?.getAttribute("data-testid") ===
  155. "chat-thought-level-select-item-minimal",
  156. );
  157. await page.keyboard.press("ArrowDown");
  158. await page.waitForFunction(
  159. () =>
  160. document.activeElement?.getAttribute("data-testid") ===
  161. "chat-thought-level-select-item-low",
  162. );
  163. await page.keyboard.press("Enter");
  164. } else {
  165. await activate(trigger);
  166. await activate(page.getByTestId("chat-thought-level-select-item-low"));
  167. }
  168. await waitValue("low");
  169. assert.equal(await trigger.getAttribute("aria-label"), label[2]);
  170. await page.screenshot({
  171. animations: "disabled",
  172. path: join(artifacts, `${width}-${theme}-${locale}-selected.png`),
  173. });
  174. assert.deepEqual(errors, []);
  175. } finally {
  176. await writeFile(
  177. join(artifacts, `${width}-${theme}-${locale}.log`),
  178. JSON.stringify({ errors }, null, 2),
  179. );
  180. await page.close();
  181. }
  182. });
  183. }
  184. }
  185. }