terminal-mobile-input.test.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import assert from "node:assert/strict";
  2. import { after, before, test } from "node:test";
  3. import { mkdir, writeFile } 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/terminal-mobile-input.fixture.tsx";
  12. const artifacts = join(root, "packages/desktop/.e2e-artifacts/terminal-mobile-input");
  13. let server, browser, origin;
  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: "terminal-input-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. `<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, hmr: false },
  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. async function openFixture({
  55. mobile = true,
  56. persistent = false,
  57. windows = false,
  58. theme = "dark",
  59. device = "iphone",
  60. } = {}) {
  61. const page = await browser.newPage({
  62. viewport: { width: mobile ? 390 : 1200, height: 700 },
  63. isMobile: mobile,
  64. hasTouch: mobile,
  65. userAgent:
  66. mobile && device === "android"
  67. ? "Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 Chrome/131.0.0.0 Mobile Safari/537.36"
  68. : mobile && device === "ipad"
  69. ? "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 Version/18.0 Safari/605.1.15"
  70. : mobile
  71. ? "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1"
  72. : undefined,
  73. });
  74. if (device === "ipad") {
  75. await page.addInitScript(() =>
  76. Object.defineProperty(navigator, "maxTouchPoints", { get: () => 5 }),
  77. );
  78. }
  79. await page.addInitScript(
  80. (platform) => Object.defineProperty(navigator, "platform", { get: () => platform }),
  81. mobile
  82. ? device === "android"
  83. ? "Linux armv8l"
  84. : device === "ipad"
  85. ? "MacIntel"
  86. : "iPhone"
  87. : windows
  88. ? "Win32"
  89. : "Linux x86_64",
  90. );
  91. await page.goto(
  92. `${origin}fixture?theme=${theme}${persistent ? "&persistent" : ""}${windows ? "&windows" : ""}`,
  93. );
  94. await page.waitForSelector('body[data-ready="true"]');
  95. await page.locator(".xterm-helper-textarea").focus();
  96. return page;
  97. }
  98. async function softType(page, text, delayed = true) {
  99. await page.locator(".xterm-helper-textarea").evaluate(
  100. async (textarea, { text, delayed }) => {
  101. for (const char of text) {
  102. textarea.dispatchEvent(
  103. new KeyboardEvent("keydown", {
  104. key: "Unidentified",
  105. keyCode: 229,
  106. bubbles: true,
  107. composed: true,
  108. }),
  109. );
  110. // 故障的必要条件:keydown 的旧定时检查早于 input;不是等待界面稳定。
  111. if (delayed) await new Promise((resolve) => setTimeout(resolve, 10));
  112. textarea.dispatchEvent(
  113. new InputEvent("beforeinput", {
  114. inputType: "insertText",
  115. data: char,
  116. bubbles: true,
  117. composed: true,
  118. }),
  119. );
  120. textarea.value += char;
  121. textarea.dispatchEvent(
  122. new InputEvent("input", {
  123. inputType: "insertText",
  124. data: char,
  125. bubbles: true,
  126. composed: true,
  127. }),
  128. );
  129. textarea.dispatchEvent(
  130. new KeyboardEvent("keyup", { key: char, bubbles: true, composed: true }),
  131. );
  132. }
  133. },
  134. { text, delayed },
  135. );
  136. }
  137. async function expectWrites(page, expected) {
  138. await page
  139. .waitForFunction(
  140. (value) => document.querySelector("output").textContent === JSON.stringify(value),
  141. expected,
  142. { timeout: 1500 },
  143. )
  144. .catch(async () => {
  145. assert.equal(JSON.parse(await page.locator("output").textContent()), expected);
  146. });
  147. assert.equal(JSON.parse(await page.locator("output").textContent()), expected);
  148. }
  149. for (const persistent of [false, true])
  150. for (const theme of ["light", "dark"]) {
  151. test(`TMI01 mobile 229 input ${persistent ? "persistent" : "bottom"}/${theme}`, async () => {
  152. const page = await openFixture({ persistent, theme });
  153. try {
  154. await softType(page, "aabb cd😀");
  155. await expectWrites(page, "aabb cd😀");
  156. await softType(page, "xy", false);
  157. await expectWrites(page, "aabb cd😀xy");
  158. if (persistent) {
  159. await page.getByRole("button", { name: "Toggle" }).click();
  160. await page.getByRole("button", { name: "Toggle" }).click();
  161. await page.locator(".xterm-helper-textarea").focus();
  162. await softType(page, "z");
  163. await expectWrites(page, "aabb cd😀xyz");
  164. }
  165. await page.screenshot({ path: join(artifacts, `${persistent}-${theme}.png`) });
  166. } finally {
  167. await writeFile(
  168. join(artifacts, `${persistent}-${theme}.json`),
  169. JSON.stringify({ writes: await page.locator("output").textContent() }),
  170. );
  171. await page.close();
  172. }
  173. });
  174. }
  175. for (const windows of [false, true])
  176. test(`TMI02 ${windows ? "Windows" : "Linux"} normal input stays single`, async () => {
  177. const page = await openFixture({ mobile: false, windows });
  178. try {
  179. await page.keyboard.type("aabb cd");
  180. await expectWrites(page, "aabb cd");
  181. await page.keyboard.press("Backspace");
  182. await expectWrites(page, "aabb cd\x7f");
  183. } finally {
  184. await page.close();
  185. }
  186. });
  187. for (const platform of ["mobile", "windows", "linux"])
  188. test(`TMI03 ${platform} composition commits once`, async () => {
  189. const page = await openFixture({
  190. mobile: platform === "mobile",
  191. windows: platform === "windows",
  192. });
  193. try {
  194. await page.locator(".xterm-helper-textarea").evaluate(async (textarea) => {
  195. textarea.dispatchEvent(
  196. new KeyboardEvent("keydown", { key: "Unidentified", keyCode: 229, bubbles: true }),
  197. );
  198. textarea.dispatchEvent(
  199. new CompositionEvent("compositionstart", { bubbles: true, data: "" }),
  200. );
  201. textarea.value = "中文";
  202. textarea.dispatchEvent(
  203. new CompositionEvent("compositionupdate", { bubbles: true, data: "中文" }),
  204. );
  205. textarea.dispatchEvent(
  206. new InputEvent("input", {
  207. inputType: "insertCompositionText",
  208. data: "中文",
  209. isComposing: true,
  210. bubbles: true,
  211. composed: true,
  212. }),
  213. );
  214. await new Promise((resolve) => setTimeout(resolve, 10));
  215. if (document.querySelector("output").textContent !== '""')
  216. throw new Error("候选文字被提前提交");
  217. textarea.dispatchEvent(
  218. new CompositionEvent("compositionend", { bubbles: true, data: "中文" }),
  219. );
  220. textarea.dispatchEvent(
  221. new InputEvent("input", {
  222. inputType: "insertText",
  223. data: "中文",
  224. bubbles: true,
  225. composed: true,
  226. }),
  227. );
  228. textarea.dispatchEvent(new KeyboardEvent("keyup", { key: "Unidentified", bubbles: true }));
  229. });
  230. // 包含现有 Windows 的 150ms 去重窗口,证明没有稍后重复写入。
  231. await page.waitForTimeout(200);
  232. await expectWrites(page, "中文");
  233. } finally {
  234. await page.close();
  235. }
  236. });
  237. test("TMI04 mobile backspace works with an empty hidden textarea", async () => {
  238. const page = await openFixture();
  239. try {
  240. await softType(page, "abc");
  241. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  242. textarea.dispatchEvent(
  243. new KeyboardEvent("keydown", { key: "Unidentified", keyCode: 229, bubbles: true }),
  244. );
  245. const accepted = textarea.dispatchEvent(
  246. new InputEvent("beforeinput", {
  247. inputType: "deleteContentBackward",
  248. bubbles: true,
  249. composed: true,
  250. cancelable: true,
  251. }),
  252. );
  253. if (accepted) {
  254. textarea.value = textarea.value.slice(0, -1);
  255. textarea.dispatchEvent(
  256. new InputEvent("input", {
  257. inputType: "deleteContentBackward",
  258. bubbles: true,
  259. composed: true,
  260. }),
  261. );
  262. }
  263. textarea.dispatchEvent(new KeyboardEvent("keyup", { key: "Backspace", bubbles: true }));
  264. });
  265. await expectWrites(page, "abc\x7f");
  266. } finally {
  267. await page.close();
  268. }
  269. });
  270. test("TMI05 mobile composition and following text share an event turn", async () => {
  271. const page = await openFixture();
  272. try {
  273. await page.locator(".xterm-helper-textarea").evaluate(async (textarea) => {
  274. textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
  275. textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
  276. textarea.value = "中文";
  277. textarea.dispatchEvent(
  278. new CompositionEvent("compositionupdate", { data: "中文", bubbles: true }),
  279. );
  280. await new Promise((resolve) => setTimeout(resolve, 10));
  281. textarea.dispatchEvent(
  282. new CompositionEvent("compositionend", { data: "中文", bubbles: true }),
  283. );
  284. textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
  285. textarea.value += "x";
  286. textarea.dispatchEvent(
  287. new InputEvent("input", {
  288. data: "x",
  289. inputType: "insertText",
  290. bubbles: true,
  291. composed: true,
  292. }),
  293. );
  294. textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
  295. });
  296. await expectWrites(page, "中文x");
  297. } finally {
  298. await page.close();
  299. }
  300. });
  301. test("TMI06 mobile input after keyup and plain input remain single", async () => {
  302. const page = await openFixture();
  303. try {
  304. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  305. textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
  306. textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
  307. textarea.value = "a";
  308. textarea.dispatchEvent(
  309. new InputEvent("input", {
  310. data: "a",
  311. inputType: "insertText",
  312. bubbles: true,
  313. composed: true,
  314. }),
  315. );
  316. textarea.value = "b";
  317. textarea.dispatchEvent(
  318. new InputEvent("input", {
  319. data: "b",
  320. inputType: "insertText",
  321. bubbles: true,
  322. composed: true,
  323. }),
  324. );
  325. });
  326. await expectWrites(page, "ab");
  327. } finally {
  328. await page.close();
  329. }
  330. });
  331. for (const device of ["android", "ipad"])
  332. test(`TMI07 ${device} soft input`, async () => {
  333. const page = await openFixture({ device });
  334. try {
  335. await softType(page, "aabb 😀");
  336. await expectWrites(page, "aabb 😀");
  337. } finally {
  338. await page.close();
  339. }
  340. });
  341. test("TMI08 cancelled composition releases mobile input", async () => {
  342. const page = await openFixture();
  343. try {
  344. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  345. textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
  346. textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
  347. textarea.dispatchEvent(new CompositionEvent("compositionend", { data: "", bubbles: true }));
  348. textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
  349. });
  350. await softType(page, "ab");
  351. await expectWrites(page, "ab");
  352. const oldInput = await page.locator(".xterm-helper-textarea").elementHandle();
  353. await page.getByRole("button", { name: "Toggle" }).click();
  354. await oldInput.evaluate((textarea) => {
  355. textarea.dispatchEvent(
  356. new InputEvent("input", {
  357. data: "stale",
  358. inputType: "insertText",
  359. bubbles: true,
  360. composed: true,
  361. }),
  362. );
  363. });
  364. await expectWrites(page, "ab");
  365. } finally {
  366. await page.close();
  367. }
  368. });
  369. test("TMI09 Linux trailing composed input does not enable Windows fallback", async () => {
  370. const page = await openFixture({ mobile: false });
  371. try {
  372. await page.keyboard.down("x");
  373. // 超出 Windows 最近输入的去重窗口;Linux 不应进入该补偿路径再发一次。
  374. await page.waitForTimeout(200);
  375. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  376. textarea.value = "x";
  377. textarea.dispatchEvent(
  378. new InputEvent("input", {
  379. inputType: "insertText",
  380. data: "x",
  381. bubbles: true,
  382. composed: true,
  383. }),
  384. );
  385. });
  386. await page.keyboard.up("x");
  387. await page.waitForTimeout(200);
  388. await expectWrites(page, "x");
  389. } finally {
  390. await page.close();
  391. }
  392. });
  393. test("TMI10 terminal status replies do not end an active composition", async () => {
  394. const page = await openFixture();
  395. try {
  396. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  397. textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
  398. });
  399. await page.getByRole("button", { name: "PTY status" }).evaluate((button) => button.click());
  400. await expectWrites(page, "\x1b[0n");
  401. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  402. textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
  403. textarea.value = "中";
  404. textarea.dispatchEvent(
  405. new CompositionEvent("compositionupdate", { data: "中", bubbles: true }),
  406. );
  407. textarea.dispatchEvent(
  408. new InputEvent("input", {
  409. data: "中",
  410. inputType: "insertText",
  411. bubbles: true,
  412. composed: true,
  413. }),
  414. );
  415. });
  416. await expectWrites(page, "\x1b[0n");
  417. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  418. textarea.dispatchEvent(new CompositionEvent("compositionend", { data: "中", bubbles: true }));
  419. textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
  420. });
  421. await expectWrites(page, "\x1b[0n中");
  422. } finally {
  423. await page.close();
  424. }
  425. });
  426. for (const persistent of [false, true]) {
  427. for (const keypress of [false, true]) {
  428. test(`${keypress ? "TMI12" : "TMI11"} mobile Enter ${persistent ? "persistent" : "bottom"}/${keypress ? "keypress" : "input"}`, async () => {
  429. const page = await openFixture({ persistent, device: "android" });
  430. try {
  431. await softType(page, "echo ok");
  432. if (persistent) {
  433. await page.getByRole("button", { name: "Toggle" }).click();
  434. await page.getByRole("button", { name: "Toggle" }).click();
  435. await page.locator(".xterm-helper-textarea").focus();
  436. }
  437. if (!keypress) {
  438. await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
  439. // 在 229 的同轮事件中产生原生换行 input,保留修复前 xterm 读取 textarea 的机会。
  440. // 不使用 press("Enter"),它会补发 keypress,掩盖手机无 keypress 时的回车丢失。
  441. textarea.addEventListener("keydown", () => document.execCommand("insertLineBreak"), {
  442. once: true,
  443. });
  444. });
  445. }
  446. const cdp = await page.context().newCDPSession(page);
  447. await cdp.send("Input.dispatchKeyEvent", {
  448. type: keypress ? "keyDown" : "rawKeyDown",
  449. key: keypress ? "Enter" : "Unidentified",
  450. windowsVirtualKeyCode: 229,
  451. nativeVirtualKeyCode: 229,
  452. ...(keypress ? { text: "\r", unmodifiedText: "\r" } : {}),
  453. });
  454. await cdp.send("Input.dispatchKeyEvent", {
  455. type: "keyUp",
  456. key: keypress ? "Enter" : "Unidentified",
  457. windowsVirtualKeyCode: 229,
  458. nativeVirtualKeyCode: 229,
  459. });
  460. // 等原 xterm 的下一轮读取结束,精确断言一次回车,防止遗漏迟到的重复写入。
  461. await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 0)));
  462. await expectWrites(page, "echo ok\r");
  463. await page.keyboard.press("Enter");
  464. await expectWrites(page, "echo ok\r\r");
  465. } finally {
  466. await page.close();
  467. }
  468. });
  469. }
  470. }