| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- import assert from "node:assert/strict";
- import { after, before, test } from "node:test";
- import { mkdir, writeFile } from "node:fs/promises";
- import { fileURLToPath } from "node:url";
- import { join } from "node:path";
- import { createServer } from "vite";
- import react from "@vitejs/plugin-react";
- import tailwindcss from "@tailwindcss/vite";
- import { chromium } from "playwright-core";
- const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
- const entry = "/packages/ui/test/browser/manual-review/pending/terminal-mobile-input.fixture.tsx";
- const artifacts = join(root, "packages/desktop/.e2e-artifacts/terminal-mobile-input");
- let server, browser, origin;
- before(async () => {
- await mkdir(artifacts, { recursive: true });
- server = await createServer({
- root,
- configFile: false,
- plugins: [
- react(),
- tailwindcss(),
- {
- name: "terminal-input-fixture",
- configureServer(vite) {
- vite.middlewares.use(async (req, res, next) => {
- if (!req.url?.startsWith("/fixture?")) return next();
- res.setHeader("Content-Type", "text/html");
- res.end(
- await vite.transformIndexHtml(
- req.url,
- `<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>`,
- ),
- );
- });
- },
- },
- ],
- resolve: { alias: { "@": `${root}packages/ui/src` } },
- optimizeDeps: { entries: [entry.slice(1)] },
- server: { host: "127.0.0.1", port: 0, hmr: false },
- });
- await server.listen();
- origin = server.resolvedUrls.local[0];
- browser = await chromium.launch(
- process.env.CHROME_EXECUTABLE_PATH
- ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
- : { channel: "chrome" },
- );
- });
- after(async () => {
- await browser?.close();
- await server?.close();
- });
- async function openFixture({
- mobile = true,
- persistent = false,
- windows = false,
- theme = "dark",
- device = "iphone",
- } = {}) {
- const page = await browser.newPage({
- viewport: { width: mobile ? 390 : 1200, height: 700 },
- isMobile: mobile,
- hasTouch: mobile,
- userAgent:
- mobile && device === "android"
- ? "Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 Chrome/131.0.0.0 Mobile Safari/537.36"
- : mobile && device === "ipad"
- ? "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 Version/18.0 Safari/605.1.15"
- : mobile
- ? "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"
- : undefined,
- });
- if (device === "ipad") {
- await page.addInitScript(() =>
- Object.defineProperty(navigator, "maxTouchPoints", { get: () => 5 }),
- );
- }
- await page.addInitScript(
- (platform) => Object.defineProperty(navigator, "platform", { get: () => platform }),
- mobile
- ? device === "android"
- ? "Linux armv8l"
- : device === "ipad"
- ? "MacIntel"
- : "iPhone"
- : windows
- ? "Win32"
- : "Linux x86_64",
- );
- await page.goto(
- `${origin}fixture?theme=${theme}${persistent ? "&persistent" : ""}${windows ? "&windows" : ""}`,
- );
- await page.waitForSelector('body[data-ready="true"]');
- await page.locator(".xterm-helper-textarea").focus();
- return page;
- }
- async function softType(page, text, delayed = true) {
- await page.locator(".xterm-helper-textarea").evaluate(
- async (textarea, { text, delayed }) => {
- for (const char of text) {
- textarea.dispatchEvent(
- new KeyboardEvent("keydown", {
- key: "Unidentified",
- keyCode: 229,
- bubbles: true,
- composed: true,
- }),
- );
- // 故障的必要条件:keydown 的旧定时检查早于 input;不是等待界面稳定。
- if (delayed) await new Promise((resolve) => setTimeout(resolve, 10));
- textarea.dispatchEvent(
- new InputEvent("beforeinput", {
- inputType: "insertText",
- data: char,
- bubbles: true,
- composed: true,
- }),
- );
- textarea.value += char;
- textarea.dispatchEvent(
- new InputEvent("input", {
- inputType: "insertText",
- data: char,
- bubbles: true,
- composed: true,
- }),
- );
- textarea.dispatchEvent(
- new KeyboardEvent("keyup", { key: char, bubbles: true, composed: true }),
- );
- }
- },
- { text, delayed },
- );
- }
- async function expectWrites(page, expected) {
- await page
- .waitForFunction(
- (value) => document.querySelector("output").textContent === JSON.stringify(value),
- expected,
- { timeout: 1500 },
- )
- .catch(async () => {
- assert.equal(JSON.parse(await page.locator("output").textContent()), expected);
- });
- assert.equal(JSON.parse(await page.locator("output").textContent()), expected);
- }
- for (const persistent of [false, true])
- for (const theme of ["light", "dark"]) {
- test(`TMI01 mobile 229 input ${persistent ? "persistent" : "bottom"}/${theme}`, async () => {
- const page = await openFixture({ persistent, theme });
- try {
- await softType(page, "aabb cd😀");
- await expectWrites(page, "aabb cd😀");
- await softType(page, "xy", false);
- await expectWrites(page, "aabb cd😀xy");
- if (persistent) {
- await page.getByRole("button", { name: "Toggle" }).click();
- await page.getByRole("button", { name: "Toggle" }).click();
- await page.locator(".xterm-helper-textarea").focus();
- await softType(page, "z");
- await expectWrites(page, "aabb cd😀xyz");
- }
- await page.screenshot({ path: join(artifacts, `${persistent}-${theme}.png`) });
- } finally {
- await writeFile(
- join(artifacts, `${persistent}-${theme}.json`),
- JSON.stringify({ writes: await page.locator("output").textContent() }),
- );
- await page.close();
- }
- });
- }
- for (const windows of [false, true])
- test(`TMI02 ${windows ? "Windows" : "Linux"} normal input stays single`, async () => {
- const page = await openFixture({ mobile: false, windows });
- try {
- await page.keyboard.type("aabb cd");
- await expectWrites(page, "aabb cd");
- await page.keyboard.press("Backspace");
- await expectWrites(page, "aabb cd\x7f");
- } finally {
- await page.close();
- }
- });
- for (const platform of ["mobile", "windows", "linux"])
- test(`TMI03 ${platform} composition commits once`, async () => {
- const page = await openFixture({
- mobile: platform === "mobile",
- windows: platform === "windows",
- });
- try {
- await page.locator(".xterm-helper-textarea").evaluate(async (textarea) => {
- textarea.dispatchEvent(
- new KeyboardEvent("keydown", { key: "Unidentified", keyCode: 229, bubbles: true }),
- );
- textarea.dispatchEvent(
- new CompositionEvent("compositionstart", { bubbles: true, data: "" }),
- );
- textarea.value = "中文";
- textarea.dispatchEvent(
- new CompositionEvent("compositionupdate", { bubbles: true, data: "中文" }),
- );
- textarea.dispatchEvent(
- new InputEvent("input", {
- inputType: "insertCompositionText",
- data: "中文",
- isComposing: true,
- bubbles: true,
- composed: true,
- }),
- );
- await new Promise((resolve) => setTimeout(resolve, 10));
- if (document.querySelector("output").textContent !== '""')
- throw new Error("候选文字被提前提交");
- textarea.dispatchEvent(
- new CompositionEvent("compositionend", { bubbles: true, data: "中文" }),
- );
- textarea.dispatchEvent(
- new InputEvent("input", {
- inputType: "insertText",
- data: "中文",
- bubbles: true,
- composed: true,
- }),
- );
- textarea.dispatchEvent(new KeyboardEvent("keyup", { key: "Unidentified", bubbles: true }));
- });
- // 包含现有 Windows 的 150ms 去重窗口,证明没有稍后重复写入。
- await page.waitForTimeout(200);
- await expectWrites(page, "中文");
- } finally {
- await page.close();
- }
- });
- test("TMI04 mobile backspace works with an empty hidden textarea", async () => {
- const page = await openFixture();
- try {
- await softType(page, "abc");
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(
- new KeyboardEvent("keydown", { key: "Unidentified", keyCode: 229, bubbles: true }),
- );
- const accepted = textarea.dispatchEvent(
- new InputEvent("beforeinput", {
- inputType: "deleteContentBackward",
- bubbles: true,
- composed: true,
- cancelable: true,
- }),
- );
- if (accepted) {
- textarea.value = textarea.value.slice(0, -1);
- textarea.dispatchEvent(
- new InputEvent("input", {
- inputType: "deleteContentBackward",
- bubbles: true,
- composed: true,
- }),
- );
- }
- textarea.dispatchEvent(new KeyboardEvent("keyup", { key: "Backspace", bubbles: true }));
- });
- await expectWrites(page, "abc\x7f");
- } finally {
- await page.close();
- }
- });
- test("TMI05 mobile composition and following text share an event turn", async () => {
- const page = await openFixture();
- try {
- await page.locator(".xterm-helper-textarea").evaluate(async (textarea) => {
- textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
- textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
- textarea.value = "中文";
- textarea.dispatchEvent(
- new CompositionEvent("compositionupdate", { data: "中文", bubbles: true }),
- );
- await new Promise((resolve) => setTimeout(resolve, 10));
- textarea.dispatchEvent(
- new CompositionEvent("compositionend", { data: "中文", bubbles: true }),
- );
- textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
- textarea.value += "x";
- textarea.dispatchEvent(
- new InputEvent("input", {
- data: "x",
- inputType: "insertText",
- bubbles: true,
- composed: true,
- }),
- );
- textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
- });
- await expectWrites(page, "中文x");
- } finally {
- await page.close();
- }
- });
- test("TMI06 mobile input after keyup and plain input remain single", async () => {
- const page = await openFixture();
- try {
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
- textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
- textarea.value = "a";
- textarea.dispatchEvent(
- new InputEvent("input", {
- data: "a",
- inputType: "insertText",
- bubbles: true,
- composed: true,
- }),
- );
- textarea.value = "b";
- textarea.dispatchEvent(
- new InputEvent("input", {
- data: "b",
- inputType: "insertText",
- bubbles: true,
- composed: true,
- }),
- );
- });
- await expectWrites(page, "ab");
- } finally {
- await page.close();
- }
- });
- for (const device of ["android", "ipad"])
- test(`TMI07 ${device} soft input`, async () => {
- const page = await openFixture({ device });
- try {
- await softType(page, "aabb 😀");
- await expectWrites(page, "aabb 😀");
- } finally {
- await page.close();
- }
- });
- test("TMI08 cancelled composition releases mobile input", async () => {
- const page = await openFixture();
- try {
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
- textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
- textarea.dispatchEvent(new CompositionEvent("compositionend", { data: "", bubbles: true }));
- textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
- });
- await softType(page, "ab");
- await expectWrites(page, "ab");
- const oldInput = await page.locator(".xterm-helper-textarea").elementHandle();
- await page.getByRole("button", { name: "Toggle" }).click();
- await oldInput.evaluate((textarea) => {
- textarea.dispatchEvent(
- new InputEvent("input", {
- data: "stale",
- inputType: "insertText",
- bubbles: true,
- composed: true,
- }),
- );
- });
- await expectWrites(page, "ab");
- } finally {
- await page.close();
- }
- });
- test("TMI09 Linux trailing composed input does not enable Windows fallback", async () => {
- const page = await openFixture({ mobile: false });
- try {
- await page.keyboard.down("x");
- // 超出 Windows 最近输入的去重窗口;Linux 不应进入该补偿路径再发一次。
- await page.waitForTimeout(200);
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.value = "x";
- textarea.dispatchEvent(
- new InputEvent("input", {
- inputType: "insertText",
- data: "x",
- bubbles: true,
- composed: true,
- }),
- );
- });
- await page.keyboard.up("x");
- await page.waitForTimeout(200);
- await expectWrites(page, "x");
- } finally {
- await page.close();
- }
- });
- test("TMI10 terminal status replies do not end an active composition", async () => {
- const page = await openFixture();
- try {
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(new CompositionEvent("compositionstart", { data: "", bubbles: true }));
- });
- await page.getByRole("button", { name: "PTY status" }).evaluate((button) => button.click());
- await expectWrites(page, "\x1b[0n");
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 229, bubbles: true }));
- textarea.value = "中";
- textarea.dispatchEvent(
- new CompositionEvent("compositionupdate", { data: "中", bubbles: true }),
- );
- textarea.dispatchEvent(
- new InputEvent("input", {
- data: "中",
- inputType: "insertText",
- bubbles: true,
- composed: true,
- }),
- );
- });
- await expectWrites(page, "\x1b[0n");
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- textarea.dispatchEvent(new CompositionEvent("compositionend", { data: "中", bubbles: true }));
- textarea.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
- });
- await expectWrites(page, "\x1b[0n中");
- } finally {
- await page.close();
- }
- });
- for (const persistent of [false, true]) {
- for (const keypress of [false, true]) {
- test(`${keypress ? "TMI12" : "TMI11"} mobile Enter ${persistent ? "persistent" : "bottom"}/${keypress ? "keypress" : "input"}`, async () => {
- const page = await openFixture({ persistent, device: "android" });
- try {
- await softType(page, "echo ok");
- if (persistent) {
- await page.getByRole("button", { name: "Toggle" }).click();
- await page.getByRole("button", { name: "Toggle" }).click();
- await page.locator(".xterm-helper-textarea").focus();
- }
- if (!keypress) {
- await page.locator(".xterm-helper-textarea").evaluate((textarea) => {
- // 在 229 的同轮事件中产生原生换行 input,保留修复前 xterm 读取 textarea 的机会。
- // 不使用 press("Enter"),它会补发 keypress,掩盖手机无 keypress 时的回车丢失。
- textarea.addEventListener("keydown", () => document.execCommand("insertLineBreak"), {
- once: true,
- });
- });
- }
- const cdp = await page.context().newCDPSession(page);
- await cdp.send("Input.dispatchKeyEvent", {
- type: keypress ? "keyDown" : "rawKeyDown",
- key: keypress ? "Enter" : "Unidentified",
- windowsVirtualKeyCode: 229,
- nativeVirtualKeyCode: 229,
- ...(keypress ? { text: "\r", unmodifiedText: "\r" } : {}),
- });
- await cdp.send("Input.dispatchKeyEvent", {
- type: "keyUp",
- key: keypress ? "Enter" : "Unidentified",
- windowsVirtualKeyCode: 229,
- nativeVirtualKeyCode: 229,
- });
- // 等原 xterm 的下一轮读取结束,精确断言一次回车,防止遗漏迟到的重复写入。
- await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 0)));
- await expectWrites(page, "echo ok\r");
- await page.keyboard.press("Enter");
- await expectWrites(page, "echo ok\r\r");
- } finally {
- await page.close();
- }
- });
- }
- }
|