Files
software-mansion__argent/packages/tool-server/test/android-tv-control.test.ts
Ignacy Łątka 44b38d490e fix(tool-server): inject Android keyboard/button over adb input (#449) (#455)
## What & why

`keyboard` and `button` drive Android through the simulator-server's
USB-HID transport (`pressKey` / `cmd:"button"`). The guest only receives
those HID events when the AVD exposes a hardware keyboard (`hw.keyboard
= yes`) — the default, but CI / headless AVDs are routinely created with
`hw.keyboard = no` (and `hw.mainKeys = no`), where the guest **silently
drops** them. Because that transport is fire-and-forget, both tools
returned `{ typed }` / `{ pressed }` **success while injecting
nothing**.

This still affects current `main`: after the keyboard refactor,
`makeAndroidImpl` routes Android **TV** to `adb input` but Android
**phones/tablets** still to `typeSimulatorServer` (the HID path).

Fixes #449 (found during 0.13.0 RC E2E testing on a `hw.keyboard=no` AVD
— `argent-test-ci-repro`, the config CI uses).

## Change

Route Android **phone/tablet** key / text / button injection through
`adb shell input text` and `input keyevent` (new
`utils/android-input.ts`), the same on-device transport Android TV and
Vega already use:

- **`keyboard`** — `platforms/android.ts` now sends the non-TV branch to
a `typeAndroidPhone` helper (adb `input`); the Android-TV and iOS
branches are untouched.
- **`button`** — the Android branch presses via `injectAndroidKeycode`;
iOS keeps the simulator-server HID path.

`adb input` lands regardless of `hw.keyboard`, on emulators (any config)
and physical devices alike, and a non-zero exit surfaces as a **thrown
error** instead of a silent no-op — directly addressing the
fire-and-forget concern already called out in the `button` tool. Touch
gestures are unchanged.

## Verification

- **Unit tests — 11/11 pass** (rebased on current `main`): new
`keyboard-android.test.ts` (vocabulary parity vs `NAMED_KEYS`, `input
text` shell-quoting incl. spaces, `input keyevent` mapping, empty-text
no-op, newline rejection) + updated `button.test.ts` (Android `back` now
asserts `KEYCODE_BACK` over adb).
- **eslint** on the changed files: clean.
- **Live tool-server** on a `hw.keyboard=no` AVD: the `keyboard` tool
now issues `adb … shell input text '…'` (vs the RC's HID `pressKey`),
and a failed inject **raises an error** rather than a fake success. The
`adb input` path was independently observed typing/deleting on this
exact AVD during RC testing (where the HID path silently no-op'd).
- Note: a full `tsc --build` / whole-suite run couldn't be completed in
my environment (the `argent-private` submodule isn't checked out, which
breaks the npm workspace install, and `main` added upstream deps my base
lacked). `platforms/android.ts` mirrors the existing `platforms/ios.ts`
branch structure one-for-one, and the pre-rebase revision of this change
passed `tsc --build` + eslint. CI here is authoritative for the full
typecheck.

## Reviewer notes

- `input text` receives the string as one shell-quoted argv token, so
spaces are preserved verbatim; newlines are rejected (they'd truncate
the `input` line) — callers use `key: "enter"`.
- `keys` counts by codepoint, matching the tv/vega/simulator-server
backends.
2026-07-10 12:51:24 +02:00

201 lines
7.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { FAILURE_CODES, getFailureSignal } from "@argent/registry";
// The Android TV backend drives the device entirely through `adb` helpers.
// Mock them so the factory + api run without a real device.
vi.mock("../src/utils/adb", () => ({
adbShell: vi.fn(async () => ""),
adbExecOutBinary: vi.fn(async () => Buffer.from("")),
shellQuote: (v: string) => `'${v.replace(/'/g, "'\\''")}'`,
getAndroidRuntimeKind: vi.fn(async () => "tv" as const),
}));
import {
androidTvControlBlueprint,
androidTvControlRef,
} from "../src/blueprints/android-tv-control";
import type { TvControlApi } from "../src/blueprints/tv-control-types";
import { adbShell, adbExecOutBinary, getAndroidRuntimeKind } from "../src/utils/adb";
import { InvalidToolInputError, UnsupportedOperationError } from "../src/utils/capability";
const mockShell = vi.mocked(adbShell);
const mockExecOut = vi.mocked(adbExecOutBinary);
const mockRuntimeKind = vi.mocked(getAndroidRuntimeKind);
const SERIAL = "emulator-5556";
const device = { id: SERIAL, platform: "android" as const, kind: "emulator" as const };
async function makeApi(): Promise<TvControlApi> {
const instance = await androidTvControlBlueprint.factory({}, device, { device });
return instance.api;
}
// Build a uiautomator dump with the given focusable nodes. `focusedIndex` marks
// which one carries focused="true".
function dump(
nodes: Array<{ label: string; bounds: string; cls?: string; text?: string }>,
focusedIndex: number
): Buffer {
const inner = nodes
.map((n, i) => {
const cls = n.cls ?? "android.widget.TextView";
const focused = i === focusedIndex ? "true" : "false";
const text = n.text ?? "";
return `<node class="${cls}" content-desc="${n.label}" text="${text}" bounds="${n.bounds}" focusable="true" focused="${focused}" enabled="true" package="com.example.tv" />`;
})
.join("");
return Buffer.from(`<?xml version='1.0'?><hierarchy rotation="0">${inner}</hierarchy>`);
}
beforeEach(() => {
mockShell.mockReset();
mockShell.mockResolvedValue("");
mockExecOut.mockReset();
mockRuntimeKind.mockReset();
mockRuntimeKind.mockResolvedValue("tv");
});
describe("android-tv-control — ref + factory gating", () => {
it("builds a namespaced ref", () => {
const ref = androidTvControlRef(device);
expect(ref.urn).toBe(`AndroidTvControl:${SERIAL}`);
expect(ref.options.device).toBe(device);
});
it("rejects a non-TV (mobile) device with an UnsupportedOperationError (→ 400, not 500)", async () => {
mockRuntimeKind.mockResolvedValue("mobile");
const err = await makeApi().then(
() => null,
(e) => e
);
expect(err).toBeInstanceOf(UnsupportedOperationError);
expect(err.message).toMatch(/Android-TV-only/);
});
it("rejects an offline / unknown serial", async () => {
mockRuntimeKind.mockResolvedValue(undefined);
await expect(makeApi()).rejects.toThrow(/no ready Android device/);
});
});
describe("android-tv-control — navigate maps to keyevents", () => {
it.each([
["up", 19],
["down", 20],
["left", 21],
["right", 22],
["select", 23],
["back", 4],
["home", 3],
["menu", 82],
["playPause", 85],
["rewind", 89],
["fastForward", 90],
["next", 87],
["previous", 88],
["volumeUp", 24],
["volumeDown", 25],
["mute", 164],
] as const)("sends %s as keyevent %i", async (direction, code) => {
const api = await makeApi();
await api.navigate(direction);
expect(mockShell).toHaveBeenCalledWith(SERIAL, `input keyevent ${code}`, expect.anything());
});
});
describe("android-tv-control — type", () => {
it("sends spaces as KEYCODE_SPACE keyevents, not %s", async () => {
const api = await makeApi();
await api.type("hello world");
const calls = mockShell.mock.calls.filter((c: unknown[]) => c[0] === SERIAL).map((c) => c[1]);
expect(calls).toEqual(["input text 'hello'", "input keyevent 62", "input text 'world'"]);
// The fragile "%s" space encoding is gone.
expect(calls.some((c: string) => c.includes("%s"))).toBe(false);
});
it("preserves a literal %s in user text (round-trip safe)", async () => {
const api = await makeApi();
await api.type("50%save");
const calls = mockShell.mock.calls.filter((c: unknown[]) => c[0] === SERIAL).map((c) => c[1]);
// Split after the '%' so the device never collapses an adjacent "%s" into a
// space: "%" and "save" arrive in separate `input text` calls, verbatim.
expect(calls).toEqual(["input text '50%'", "input text 'save'"]);
// No keyevent — there is no real space in the input.
expect(calls.some((c: string) => c.includes("keyevent"))).toBe(false);
});
it("is a no-op for empty text", async () => {
const api = await makeApi();
await api.type("");
// Only the factory's runtime-kind probe ran via getAndroidRuntimeKind; no
// `input text` shell-out.
expect(mockShell).not.toHaveBeenCalledWith(
SERIAL,
expect.stringContaining("input text"),
expect.anything()
);
});
// Android TV shares the `adb shell input text` sink with the phone path, and
// with it the sink's limitations: an emoji NPEs `sendText` on-device, other
// non-ASCII is silently dropped, and a newline truncates the input line. The
// shared guard must reject those up front (InvalidToolInputError → 400 +
// KEYBOARD_CHARACTER_UNSUPPORTED, same as every other keyboard backend)
// instead of letting them surface as a raw adb 500 (hubgan review).
it("rejects an emoji before any adb call (400-class + KEYBOARD_CHARACTER_UNSUPPORTED)", async () => {
const api = await makeApi();
const err = await api.type("ab😀").then(
() => {
throw new Error("expected type() to reject");
},
(e: unknown) => e
);
expect(err).toBeInstanceOf(InvalidToolInputError);
expect(getFailureSignal(err)?.error_code).toBe(FAILURE_CODES.KEYBOARD_CHARACTER_UNSUPPORTED);
expect(mockShell).not.toHaveBeenCalled();
});
it("rejects a newline before any adb call (400-class + KEYBOARD_CHARACTER_UNSUPPORTED)", async () => {
const api = await makeApi();
const err = await api.type("a\nb").then(
() => {
throw new Error("expected type() to reject");
},
(e: unknown) => e
);
expect(err).toBeInstanceOf(InvalidToolInputError);
expect(getFailureSignal(err)?.error_code).toBe(FAILURE_CODES.KEYBOARD_CHARACTER_UNSUPPORTED);
expect(mockShell).not.toHaveBeenCalled();
});
});
describe("android-tv-control — describe", () => {
it("projects the focused + focusable nodes from the uiautomator dump", async () => {
mockExecOut.mockResolvedValue(
dump(
[
{ label: "Home", bounds: "[0,0][100,50]" },
{ label: "Search", bounds: "[100,0][200,50]", cls: "android.widget.Button" },
],
0
)
);
const api = await makeApi();
const res = await api.describe();
expect(res.bundleId).toBe("com.example.tv");
expect(res.focused?.label).toBe("Home");
expect(res.focusable).toHaveLength(2);
expect(res.focusable[1]?.label).toBe("Search");
expect(res.focusable[1]?.traits).toContain("button");
});
});
describe("android-tv-control — recycleAx is a no-op", () => {
it("resolves without touching adb", async () => {
const api = await makeApi();
mockShell.mockClear();
await expect(api.recycleAx()).resolves.toBeUndefined();
expect(mockShell).not.toHaveBeenCalled();
});
});