Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

13.4 KB
Raw Blame
/**
 * The terminal under a finger: a phone's soft keyboard has no Esc, no Tab, no arrows and no
 * Ctrl, so the key bar is the only way to drive a shell there. What matters is that its caps
 * reach the real pty — an arrow that types `[A` into the prompt, or a Ctrl that never
 * composes, looks like a working button and is not one — and that none of it appears where a
 * physical keyboard already exists.
 *
 * The same bar is where a phone copies from: xterm has no touch selection, so a press that
 * rests still makes one (terminal-selection.ts, wired in terminal-view.tsx) and the bar's
 * copy cap takes it. Both halves are checked against the clipboard the browser really holds.
 */
import { test, expect } from "@playwright/test";
import { provisionAndLogin } from "./auth.mjs";

const BASE = process.env.BASE_URL;
const U = "touchuser";
const P = "password123";

/** Live shells accumulate across spec reruns on one server; MAX 12/user would 429. */
async function killAllTerminals(request) {
  const { terminals } = await (await request.get(`${BASE}/api/terminals`)).json();
  for (const t of terminals) await request.delete(`${BASE}/api/terminals/${t.id}`);
}

const screenText = (page) => page.locator(".xterm-rows").innerText();

/** A tap on the screen focuses xterm, which is what raises the soft keyboard. */
async function focusShell(page) {
  await page.locator(".xterm-screen").click();
}

async function type(page, command) {
  await page.keyboard.type(command);
  await page.keyboard.press("Enter");
}

/**
 * "ready" only means the stream is attached; a login shell may still be sourcing profiles,
 * and input typed meanwhile sits in the tty buffer until it wakes up. Probe with a sentinel.
 */
async function waitForShell(page, tag) {
  await expect(page.locator(".xterm-rows")).toBeVisible({ timeout: 20000 });
  await expect(page.locator('[data-testid="terminal-status"][data-status="ready"]')).toBeVisible({
    timeout: 20000,
  });
  await focusShell(page);
  await type(page, `echo ${tag.slice(0, 2)}''${tag.slice(2)}`);
  await expect.poll(() => screenText(page), { timeout: 30000 }).toMatch(new RegExp(`${tag}$`, "m"));
}

/**
 * One finger dragged down the middle of the screen, from one fraction of its height to
 * another. Through CDP because that is the only way to produce touches a page cannot tell
 * from a real one — Playwright's own touchscreen taps and nothing more, and a hand-built
 * TouchEvent skips the hit testing and the `touch-action` the gesture depends on.
 */
async function dragFinger(page, from, to) {
  const box = await page.locator(".xterm-screen").boundingBox();
  const x = box.x + box.width / 2;
  const y = (fraction) => box.y + box.height * fraction;
  const cdp = await page.context().newCDPSession(page);
  await cdp.send("Input.dispatchTouchEvent", {
    type: "touchStart",
    touchPoints: [{ x, y: y(from) }],
  });
  const steps = 10;
  for (let i = 1; i <= steps; i++) {
    await cdp.send("Input.dispatchTouchEvent", {
      type: "touchMove",
      touchPoints: [{ x, y: y(from + ((to - from) * i) / steps) }],
    });
  }
  await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] });
  await cdp.detach();
}

/** One finger down at `point` and up again without travelling: a tap. */
async function touchTap(page, point) {
  const cdp = await page.context().newCDPSession(page);
  await cdp.send("Input.dispatchTouchEvent", {
    type: "touchStart",
    touchPoints: [{ x: point.x, y: point.y }],
  });
  await page.waitForTimeout(40);
  await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] });
  await cdp.detach();
}

/** One finger from `from` to `to`, in steps, then up. */
async function touchDrag(page, from, to) {
  const cdp = await page.context().newCDPSession(page);
  await cdp.send("Input.dispatchTouchEvent", {
    type: "touchStart",
    touchPoints: [{ x: from.x, y: from.y }],
  });
  const steps = 6;
  for (let i = 1; i <= steps; i++) {
    await cdp.send("Input.dispatchTouchEvent", {
      type: "touchMove",
      touchPoints: [
        { x: from.x + ((to.x - from.x) * i) / steps, y: from.y + ((to.y - from.y) * i) / steps },
      ],
    });
  }
  await page.waitForTimeout(60);
  await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] });
  await cdp.detach();
}

/** A point inside the screen, on the buffer row whose text carries `marker`. */
async function pointOnRow(page, marker, fraction) {
  const point = await page.evaluate(
    ({ marker, fraction }) => {
      const rows = [...document.querySelectorAll(".xterm-rows > div")];
      const row = rows.filter((r) => r.textContent?.includes(marker)).pop();
      if (!row) return null;
      const box = row.getBoundingClientRect();
      return { x: box.x + box.width * fraction, y: box.y + box.height / 2 };
    },
    { marker, fraction },
  );
  expect(point, `no row carrying ${marker}`).not.toBeNull();
  return point;
}

test.describe("touch", () => {
  test.use({ viewport: { width: 412, height: 915 }, hasTouch: true, isMobile: true });

  test("the key bar drives the shell: history, interrupt, and a sticky Ctrl", async ({ page }) => {
    await provisionAndLogin(page.request, U, P);
    await killAllTerminals(page.request);
    await page.goto(`${BASE}/terminal`);
    await waitForShell(page, "TOUCH_UP_1");

    const bar = page.getByTestId("terminal-key-bar");
    await expect(bar).toBeVisible();

    // The keyboard cap is a toggle over xterm's focus: it dismisses the soft keyboard
    // (which a phone otherwise leaves covering half the screen) and calls it back.
    const keyboardCap = page.getByTestId("terminal-key-keyboard");
    await expect(keyboardCap).toHaveAccessibleName(/收起键盘/);
    await keyboardCap.click();
    await expect(keyboardCap).toHaveAccessibleName(/调出键盘/);
    await keyboardCap.click();
    await expect(keyboardCap).toHaveAccessibleName(/收起键盘/);

    // ↑ recalls the previous command. A CSI arrow reaching a shell that asked for SS3 would
    // land as literal text instead, which is exactly what this catches.
    await type(page, "echo HISTORY_ONE");
    await expect.poll(() => screenText(page), { timeout: 15000 }).toContain("HISTORY_ONE");
    await page.getByTestId("terminal-key-up").click();
    await page.keyboard.press("Enter");
    await expect
      .poll(() => screenText(page).then((t) => t.split("HISTORY_ONE").length - 1), {
        timeout: 15000,
      })
      // The command echo and its output, twice over: four occurrences, not two.
      .toBeGreaterThanOrEqual(4);

    // ^C on a shell that is busy: without it the prompt would not come back for 45 seconds.
    await type(page, "sleep 45");
    await page.getByTestId("terminal-key-interrupt").click();
    await type(page, "echo AFTER_INTERRUPT");
    await expect.poll(() => screenText(page), { timeout: 15000 }).toContain("AFTER_INTERRUPT");

    // The sticky modifier: arm Ctrl on the bar, then the next character typed on the
    // keyboard composes with it (this is the path a soft keyboard's "c" takes).
    await type(page, "sleep 45");
    const ctrl = page.getByTestId("terminal-key-ctrl");
    await ctrl.click();
    await expect(ctrl).toHaveAttribute("aria-pressed", "true");
    await page.keyboard.type("c");
    await expect(ctrl).toHaveAttribute("aria-pressed", "false"); // spent
    await type(page, "echo AFTER_STICKY_CTRL");
    await expect.poll(() => screenText(page), { timeout: 15000 }).toContain("AFTER_STICKY_CTRL");
  });

  test("a finger scrolls a program that has taken the mouse", async ({ page }) => {
    await provisionAndLogin(page.request, U, P);
    await killAllTerminals(page.request);
    await page.goto(`${BASE}/terminal`);
    await waitForShell(page, "TOUCH_UP_2");

    // A plain shell: the drag is xterm's own business. It still scrolls the scrollback — the
    // `touch-none` the host now carries must not take that away — and NOTHING may reach the
    // pty, since a wheel report typed at a prompt is a line of garbage.
    await type(page, "seq 1 300");
    await expect.poll(() => screenText(page), { timeout: 15000 }).toContain("300");
    const scrollTop = () =>
      page.evaluate(() => document.querySelector(".xterm-viewport").scrollTop);
    const atBottom = await scrollTop();
    await dragFinger(page, 0.3, 0.8);
    await expect.poll(scrollTop, { timeout: 10000 }).toBeLessThan(atBottom);
    await expect(page.locator(".xterm-rows")).not.toContainText("[<");

    // The arrangement a TUI uses, Claude Code included: the alternate screen (no scrollback
    // left to scroll) plus any-event mouse tracking in SGR. `cat -v` then prints what the
    // program would have received, so the reports the finger produces are on the screen.
    await type(
      page,
      String.raw`printf '\033[?1049h\033[?1000h\033[?1002h\033[?1003h\033[?1006h'; cat -v`,
    );
    await expect
      .poll(() => page.evaluate(() => document.querySelector(".xterm")?.className ?? ""), {
        timeout: 15000,
      })
      .toContain("enable-mouse-events");

    // Up the screen: later content, which is a wheel rolled down — button 65.
    await dragFinger(page, 0.7, 0.3);
    await expect.poll(() => screenText(page), { timeout: 15000 }).toMatch(/\[<65;\d+;\d+M/);
    // And back down: earlier content, button 64.
    await dragFinger(page, 0.3, 0.7);
    await expect.poll(() => screenText(page), { timeout: 15000 }).toMatch(/\[<64;\d+;\d+M/);
  });
});

test.describe("touch selection", () => {
  test.use({ viewport: { width: 412, height: 915 }, hasTouch: true, isMobile: true });

  const A_ROW = "A".repeat(20);
  const B_ROW = "B".repeat(20);

  /**
   * Two dense rows of output: a row of one unbroken run is the shape where "which word did
   * this touch take" has one answer, so the clipboard can be asserted to the character
   * instead of to a substring that happens to be somewhere on the line.
   */
  async function printTwoRows(page) {
    await type(page, String.raw`printf '%s\n' ${A_ROW} ${B_ROW}`);
    await expect.poll(() => screenText(page), { timeout: 15000 }).toContain(B_ROW);
  }

  const clipboard = (page) => page.evaluate(() => navigator.clipboard.readText());

  test("the select cap arms a mode, and the copy cap copies what it took", async ({ page }) => {
    await provisionAndLogin(page.request, U, P);
    await killAllTerminals(page.request);
    await page.goto(`${BASE}/terminal`);
    await waitForShell(page, "TOUCH_COPY_1");
    await printTwoRows(page);

    const selectCap = page.getByTestId("terminal-key-select");
    const copyCap = page.getByTestId("terminal-key-copy");
    await expect(selectCap).toHaveAttribute("aria-pressed", "false");

    // Pulling a finger across the output with the mode off is the scroll it has always been,
    // and leaves nothing to copy — the drags in the test above are that behaviour's own.
    await touchDrag(page, await pointOnRow(page, A_ROW, 0.2), await pointOnRow(page, B_ROW, 0.2));
    await expect(copyCap).toHaveCount(0);

    await selectCap.click();
    await expect(selectCap).toHaveAttribute("aria-pressed", "true");
    await touchTap(page, await pointOnRow(page, A_ROW, 0.5));

    // One unbroken run is one word, so the touch takes the row and the cap offers it.
    await expect(copyCap).toBeVisible();
    await copyCap.click();
    expect(await clipboard(page)).toBe(A_ROW);
    // The cap lives exactly as long as the selection does, and the copy cleared it — which
    // only a write that actually landed does.
    await expect(copyCap).toHaveCount(0);

    // The mode is still armed, and leaving it is its own tap.
    await expect(selectCap).toHaveAttribute("aria-pressed", "true");
    await selectCap.click();
    await expect(selectCap).toHaveAttribute("aria-pressed", "false");
  });

  test("a finger that travels after the touch extends the selection", async ({ page }) => {
    await provisionAndLogin(page.request, U, P);
    await killAllTerminals(page.request);
    await page.goto(`${BASE}/terminal`);
    await waitForShell(page, "TOUCH_COPY_2");
    await printTwoRows(page);

    await page.getByTestId("terminal-key-select").click();
    // Down through the second row: the selection runs from the word the touch took to the
    // cell the finger stopped on.
    await touchDrag(page, await pointOnRow(page, A_ROW, 0.2), await pointOnRow(page, B_ROW, 0.2));

    const copyCap = page.getByTestId("terminal-key-copy");
    await expect(copyCap).toBeVisible();
    await copyCap.click();
    expect(await clipboard(page)).toMatch(new RegExp(`^${A_ROW}\nB{1,20}$`));
  });

  test("leaving the mode drops the selection", async ({ page }) => {
    await provisionAndLogin(page.request, U, P);
    await killAllTerminals(page.request);
    await page.goto(`${BASE}/terminal`);
    await waitForShell(page, "TOUCH_COPY_3");
    await printTwoRows(page);

    const selectCap = page.getByTestId("terminal-key-select");
    const copyCap = page.getByTestId("terminal-key-copy");
    await selectCap.click();
    await touchTap(page, await pointOnRow(page, A_ROW, 0.5));
    await expect(copyCap).toBeVisible();

    await selectCap.click();
    await expect(copyCap).toHaveCount(0);
  });
});

test("no key bar where there is a real keyboard", async ({ page }) => {
  await provisionAndLogin(page.request, U, P);
  await killAllTerminals(page.request);
  await page.goto(`${BASE}/terminal`);
  await expect(page.locator('[data-testid="terminal-status"][data-status="ready"]')).toBeVisible({
    timeout: 20000,
  });
  await expect(page.getByTestId("terminal-key-bar")).toHaveCount(0);
});