permission-full-access.test.mjs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import assert from "node:assert/strict";
  2. import { before, after, test } from "node:test";
  3. import { mkdir } from "node:fs/promises";
  4. import { fileURLToPath } from "node:url";
  5. import { join } from "node:path";
  6. import { createServer } from "vite";
  7. import react from "@vitejs/plugin-react";
  8. import tailwindcss from "@tailwindcss/vite";
  9. import { chromium } from "playwright-core";
  10. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  11. const entry = "/packages/ui/test/browser/manual-review/pending/permission-full-access.fixture.tsx";
  12. let server, browser, origin;
  13. const artifacts = join(root, "packages/desktop/.e2e-artifacts/permission-full-access-browser");
  14. before(async () => {
  15. await mkdir(artifacts, { recursive: true });
  16. server = await createServer({
  17. root,
  18. configFile: false,
  19. plugins: [
  20. react(),
  21. tailwindcss(),
  22. {
  23. name: "permission-fixture",
  24. configureServer(vite) {
  25. vite.middlewares.use(async (req, res, next) => {
  26. if (!req.url?.startsWith("/fixture?")) return next();
  27. res.setHeader("Content-Type", "text/html");
  28. res.end(
  29. await vite.transformIndexHtml(
  30. req.url,
  31. `<!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>`,
  32. ),
  33. );
  34. });
  35. },
  36. },
  37. ],
  38. resolve: { alias: { "@": `${root}packages/ui/src` } },
  39. optimizeDeps: { entries: [entry.slice(1)] },
  40. server: { host: "127.0.0.1", port: 0 },
  41. });
  42. await server.listen();
  43. origin = server.resolvedUrls.local[0];
  44. browser = await chromium.launch(
  45. process.env.CHROME_EXECUTABLE_PATH
  46. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  47. : { channel: "chrome" },
  48. );
  49. });
  50. after(async () => {
  51. await browser?.close();
  52. await server?.close();
  53. });
  54. for (const width of [390, 1200])
  55. for (const theme of ["light", "dark"]) {
  56. test(`PA158 shared approval UI ${width}/${theme}`, async () => {
  57. const page = await browser.newPage({ viewport: { width, height: 1000 } });
  58. const errors = [];
  59. page.on("pageerror", (error) => errors.push(error.message));
  60. try {
  61. const mobile = width === 390;
  62. await page.goto(`${origin}fixture?theme=${theme}&locale=${mobile ? "zh-CN" : "en-US"}`);
  63. const options = page.getByRole("option");
  64. await options.first().waitFor();
  65. assert.equal(await options.count(), 4);
  66. assert.equal(
  67. await options.nth(1).getAttribute("aria-label"),
  68. mobile ? "始终允许此命令" : "Always allow this command",
  69. );
  70. assert.equal(
  71. await options.nth(2).getAttribute("aria-label"),
  72. mobile ? "完全访问" : "Full access",
  73. );
  74. await options.first().focus();
  75. await page.keyboard.press("5");
  76. // 权限选项通过 RAF 转移焦点,等待真实焦点落点,避免在同帧抢先断言。
  77. await page.waitForFunction(() => document.activeElement?.tagName === "TEXTAREA");
  78. assert.equal(
  79. await page.getByRole("textbox").evaluate((el) => document.activeElement === el),
  80. true,
  81. );
  82. assert.equal(await page.getByTestId("response").textContent(), "");
  83. assert.equal(
  84. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  85. true,
  86. );
  87. await page.screenshot({ path: join(artifacts, `${width}-${theme}.png`), fullPage: true });
  88. await options.first().focus();
  89. await page.keyboard.press("3");
  90. await page.keyboard.press("Enter");
  91. await page.waitForFunction(
  92. () => document.querySelector('[data-testid="response"]')?.textContent === "fullAccess",
  93. );
  94. assert.deepEqual(errors, []);
  95. } finally {
  96. await page.close();
  97. }
  98. });
  99. }
  100. for (const width of [390, 1200])
  101. for (const theme of ["light", "dark"]) {
  102. test(`long permission command ${width}/${theme}`, async () => {
  103. const page = await browser.newPage({ viewport: { width, height: 900 } });
  104. try {
  105. await page.goto(`${origin}fixture?theme=${theme}&command=long`);
  106. const command = page.getByTestId("execute-command");
  107. await command.waitFor();
  108. assert.equal(await page.getByText("没有输出。", { exact: true }).count(), 0);
  109. const toggle = page.getByRole("button", { name: "展开命令", exact: true });
  110. await toggle.waitFor();
  111. assert.equal(await toggle.getAttribute("aria-expanded"), "false");
  112. assert.equal(
  113. await toggle.evaluate(
  114. (el) => el.previousElementSibling?.getAttribute("data-testid") === "execute-command",
  115. ),
  116. true,
  117. );
  118. const alignment = await toggle.evaluate((el) => {
  119. const text = el.parentElement.querySelector("pre");
  120. const range = document.createRange();
  121. range.selectNodeContents(el.firstChild);
  122. return Math.abs(range.getBoundingClientRect().left - text.getBoundingClientRect().left);
  123. });
  124. assert.ok(alignment < 0.1, `command toggle text alignment: ${alignment}`);
  125. assert.equal(
  126. await command.evaluate(
  127. (el) => el.clientHeight / Number.parseFloat(getComputedStyle(el).lineHeight),
  128. ),
  129. 3,
  130. );
  131. assert.match(
  132. await toggle.evaluate((el) => getComputedStyle(el).backgroundColor),
  133. /^rgba\([^,]+, [^,]+, [^,]+, 0\)$/,
  134. );
  135. assert.equal(await toggle.locator("svg").count(), 1);
  136. assert.deepEqual(
  137. await toggle.evaluate((el) => {
  138. const style = getComputedStyle(el);
  139. return [
  140. el.dataset.variant,
  141. style.paddingLeft,
  142. style.paddingRight,
  143. style.borderLeftWidth,
  144. style.borderRightWidth,
  145. ];
  146. }),
  147. ["link", "0px", "0px", "0px", "0px"],
  148. );
  149. await page.screenshot({
  150. path: join(artifacts, `command-collapsed-${width}-${theme}.png`),
  151. fullPage: true,
  152. });
  153. await page.waitForFunction(() => document.activeElement?.getAttribute("role") === "option");
  154. await page.keyboard.press("Shift+Tab");
  155. assert.equal(await toggle.evaluate((el) => document.activeElement === el), true);
  156. await page.keyboard.press("Enter");
  157. assert.equal(
  158. await page
  159. .getByRole("button", { name: "收起", exact: true })
  160. .getAttribute("aria-expanded"),
  161. "true",
  162. );
  163. assert.equal(await page.getByTestId("response").textContent(), "");
  164. await page.keyboard.press("Tab");
  165. assert.equal(
  166. await page
  167. .getByRole("option")
  168. .first()
  169. .evaluate((el) => document.activeElement === el),
  170. true,
  171. );
  172. await page.keyboard.press("Shift+Tab");
  173. await page.keyboard.press("Shift+Tab");
  174. assert.equal(await command.evaluate((el) => document.activeElement === el), true);
  175. const collapse = page.getByRole("button", { name: "收起", exact: true });
  176. await page.waitForFunction(() => {
  177. const button = document.querySelector('button[aria-expanded="true"]');
  178. return (
  179. button &&
  180. /^rgba\([^,]+, [^,]+, [^,]+, 0\)$/.test(getComputedStyle(button).backgroundColor)
  181. );
  182. });
  183. assert.equal(await collapse.locator("svg.rotate-180").count(), 1);
  184. const collapseBefore = await collapse.boundingBox();
  185. const geometry = await command.evaluate((el) => {
  186. const card = el.closest('[data-testid="execute-detail"]');
  187. const style = getComputedStyle(card);
  188. const rightInset =
  189. card.getBoundingClientRect().right -
  190. Number.parseFloat(style.borderRightWidth) -
  191. el.getBoundingClientRect().right;
  192. const prompt = el.querySelector('[data-testid="execute-command-prompt"]');
  193. if (!prompt) throw new Error("Command prompt must be inside the viewport");
  194. const promptTop = prompt.getBoundingClientRect().top;
  195. el.scrollTop = el.scrollHeight;
  196. const promptScrolled =
  197. prompt.getBoundingClientRect().top < el.getBoundingClientRect().top;
  198. el.scrollTop = 0;
  199. const promptRestored = prompt.getBoundingClientRect().top === promptTop;
  200. el.scrollTop = el.scrollHeight;
  201. return {
  202. promptScrolled,
  203. promptRestored,
  204. rightInset,
  205. end: el.scrollTop + el.clientHeight >= el.scrollHeight - 1,
  206. overflow: getComputedStyle(el).overflowY,
  207. complete: el.textContent.endsWith('COMMAND_END"'),
  208. };
  209. });
  210. assert.deepEqual(geometry, {
  211. rightInset: 4,
  212. end: true,
  213. overflow: "auto",
  214. complete: true,
  215. promptScrolled: true,
  216. promptRestored: true,
  217. });
  218. assert.deepEqual(await collapse.boundingBox(), collapseBefore);
  219. assert.equal(
  220. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  221. true,
  222. );
  223. await page.screenshot({
  224. path: join(artifacts, `command-${width}-${theme}.png`),
  225. fullPage: true,
  226. });
  227. await page.getByRole("button", { name: "收起", exact: true }).click();
  228. await page.getByRole("button", { name: "展开命令", exact: true }).waitFor();
  229. await page.getByRole("option").first().click();
  230. assert.equal(await page.getByTestId("response").textContent(), "allowOnce");
  231. await page.goto(`${origin}fixture?theme=${theme}`);
  232. await page.getByRole("option").first().waitFor();
  233. assert.equal(await page.getByRole("button", { name: "展开命令", exact: true }).count(), 0);
  234. } finally {
  235. await page.close();
  236. }
  237. });
  238. }