Files
software-mansion__argent/packages/tool-server/test/android-tv-launch.test.ts
Mateusz Ścianek b8afbed183 feat(tv): Apple TV (tvOS) and Android TV support (#374)
Adds end-to-end support for driving **Apple TV (tvOS)** and **Android TV
(leanback)** targets through argent, using a focus-driven interaction
model (TVs have no touchscreen — interaction is moving a focus highlight
with the remote).

This PR was reworked after review to **reuse the existing cross-platform
tool surface and the `tv-remote` tool from the Vega/Fire TV work
(#366)** rather than ship a separate family of `tv-*` tools.

## What this delivers

TV targets are driven by the **same cross-platform tools as every other
platform**, routed through the existing `dispatchByPlatform` fork
mechanism — there are no dedicated
`tv-describe`/`tv-navigate`/`tv-type`/`tv-set-focus` tools:

- **`describe`** — on a `runtimeKind: "tv"` target, returns the
focus-driven view (focused + focusable elements) instead of a tap tree.
On Android TV it auto-falls-back to the full `uiautomator` tree when the
RN focus engine exposes nothing.
- **`tv-remote`** — the cross-platform remote/D-pad tool from #366, now
extended with `ios` (Apple TV HID daemon) and `android` (Android TV adb
keyevents) branches alongside the existing `vega` branch. Single button,
a `repeat`, or a whole path (`["up","right","select"]`) in one call.
- **`keyboard`** — types into the focused field on a TV target (named
keys are rejected there — they're navigation, which belongs to
`tv-remote`).
- **`button`** stays hardware-only (phones/tablets); it is not a TV
tool.

**Apple TV** runs two native daemons (in-sim AX service + host-side HID
daemon), shipped via the `argent-private` submodule. **Android TV**
reuses the same tool surface, adb-backed (`input keyevent`, `uiautomator
dump`, `input text`).

## Remote vocabulary parity

`tv-remote` exposes the full 16-button vocabulary
(`up`/`down`/`left`/`right`/`select`/`back`/`home`/`menu`/`playPause`/`rewind`/`fastForward`/`next`/`previous`/`volumeUp`/`volumeDown`/`mute`):

| | Apple TV | Android TV | Vega |
|---|---|---|---|
| D-pad / select / back / menu / home / playPause | ✅ | ✅ | ✅ |
| media-transport + volume/mute | ❌ rejected | ✅ | ✅ |

Media-transport/volume keys **genuinely work on Android TV** (real
keycodes — verified live: `volumeUp` moved `STREAM_MUSIC`, `mute`
toggled per `dumpsys audio`). On the **Apple TV simulator** they are
**rejected with a clear error**: on-device testing confirmed the tvOS
sim's HID stack silently drops Consumer-Control events, so returning
success would be a lie.

## Key design points
- **tvOS UDIDs are UUID-shaped and indistinguishable from iOS by
shape.** Handled with `runtimeKind` detection (tvOS runtime string for
Apple TV; `pm list features` leanback/television for Android TV — *not*
`ro.build.characteristics`, which lies on TV emulators).
- Converted launch-app / restart-app / screenshot / screenshot-diff /
run-sequence from **eager** to **lazy** service resolution, so a tvOS
target never spins up (and hangs on) the iOS-only simulator-server /
native-devtools blueprints.
- Native injection selects the platform-matched (TVOSSIMULATOR) dylib
slice; daemons recycle across sim reboots and app relaunches so
injection and focus survive.
- SKILL.md footprint minimized per review: the two TV skills were
collapsed into a single lean **`argent-tv-interact`** (~40 lines).

## Verification
- `npm run build`, ESLint, Prettier, and the full tool-server suite
(**1565 tests**) all pass; CI green (incl. the Apple TV / Android TV /
Vega e2e jobs).
- Core flows verified live against real apps on both an Apple TV 4K
simulator and a Google ATV emulator.

## Dependency / merge order
Pins the `argent-private` submodule to the tip of its `feat/tv-support`
branch (software-mansion/argent-private#20). **Merge that PR to `main`
first, then bump the submodule pointer here to the merged SHA before
merging this PR.**

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 14:59:37 +02:00

160 lines
6.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const execFileMock = vi.fn();
vi.mock("node:child_process", async () => {
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
return {
...actual,
execFile: (
cmd: string,
args: readonly string[],
opts: unknown,
cb?: (err: Error | null, out: { stdout: string; stderr: string }) => void
) => {
const callback = typeof opts === "function" ? opts : cb!;
const options = typeof opts === "function" ? undefined : opts;
const result = execFileMock(cmd, args, options);
if (result instanceof Error) callback(result, { stdout: "", stderr: "" });
else callback(null, result ?? { stdout: "", stderr: "" });
},
};
});
vi.mock("../src/utils/android-binary", () => ({
resolveAndroidBinary: vi.fn(async (name: "adb" | "emulator") => name),
__resetAndroidBinaryCacheForTesting: () => {},
}));
import {
normalizeActivityComponent,
resolveLauncherActivity,
} from "../src/tools/launch-app/platforms/android";
// Returns the shell command string of an `adb -s <serial> shell <cmd>` call.
function shellCmd(args: string[]): string {
return args[0] === "-s" && args[2] === "shell" ? (args[3] ?? "") : "";
}
beforeEach(() => {
execFileMock.mockReset();
});
describe("normalizeActivityComponent — shared by launch-app + restart-app", () => {
const PKG = "com.example.app";
it("makes a bare class name relative so `am start` doesn't reject it", () => {
// The trap: `${pkg}/MainActivity` is treated as a default-package class and
// rejected ("no match"). It must become `${pkg}/.MainActivity`.
expect(normalizeActivityComponent(PKG, "MainActivity")).toBe(`${PKG}/.MainActivity`);
});
it("passes a relative (dot-prefixed) activity through under the bundle id", () => {
expect(normalizeActivityComponent(PKG, ".MainActivity")).toBe(`${PKG}/.MainActivity`);
});
it("qualifies a fully-qualified class name with the bundle id", () => {
expect(normalizeActivityComponent(PKG, "com.other.SplashActivity")).toBe(
`${PKG}/com.other.SplashActivity`
);
});
it("leaves an already-complete pkg/Activity component untouched", () => {
expect(normalizeActivityComponent(PKG, "com.example.app/.MainActivity")).toBe(
`${PKG}/.MainActivity`
);
expect(normalizeActivityComponent(PKG, "other.pkg/full.Class")).toBe("other.pkg/full.Class");
});
});
describe("resolveLauncherActivity — leanback (Android TV)", () => {
it("resolves the LEANBACK_LAUNCHER activity first on TV targets", async () => {
execFileMock.mockImplementation((cmd: string, args: string[]) => {
const shell = shellCmd(args);
if (shell.includes("LEANBACK_LAUNCHER") && shell.includes("com.example.tv")) {
return { stdout: "com.example.tv/com.example.tv.LeanbackActivity\n", stderr: "" };
}
return { stdout: "", stderr: "" };
});
const component = await resolveLauncherActivity("emulator-5556", "com.example.tv", true);
expect(component).toBe("com.example.tv/com.example.tv.LeanbackActivity");
// The resolve query must have carried the leanback category.
const calls = execFileMock.mock.calls.map((c) => shellCmd(c[1] as string[]));
expect(calls.some((s) => s.includes("LEANBACK_LAUNCHER"))).toBe(true);
});
it("falls back to the standard LAUNCHER when no leanback activity exists", async () => {
execFileMock.mockImplementation((cmd: string, args: string[]) => {
const shell = shellCmd(args);
// No leanback match; a plain resolve (no category) returns the launcher.
if (
shell.startsWith("cmd package resolve-activity --brief ") &&
!shell.includes("-c ") &&
shell.includes("com.example.tv")
) {
return { stdout: "com.example.tv/com.example.tv.MainActivity\n", stderr: "" };
}
return { stdout: "", stderr: "" };
});
const component = await resolveLauncherActivity("emulator-5556", "com.example.tv", true);
expect(component).toBe("com.example.tv/com.example.tv.MainActivity");
});
it("does NOT query leanback for a non-TV target", async () => {
execFileMock.mockImplementation((cmd: string, args: string[]) => {
const shell = shellCmd(args);
if (shell.startsWith("cmd package resolve-activity --brief ") && !shell.includes("-c ")) {
return { stdout: "com.example.app/com.example.app.MainActivity\n", stderr: "" };
}
return { stdout: "", stderr: "" };
});
const component = await resolveLauncherActivity("emulator-5554", "com.example.app", false);
expect(component).toBe("com.example.app/com.example.app.MainActivity");
const calls = execFileMock.mock.calls.map((c) => shellCmd(c[1] as string[]));
expect(calls.some((s) => s.includes("LEANBACK_LAUNCHER"))).toBe(false);
});
it("rejects the system ResolverActivity and falls through to the real launcher", async () => {
execFileMock.mockImplementation((cmd: string, args: string[]) => {
const shell = shellCmd(args);
// The leanback resolve returns the system chooser (the package has no
// leanback activity) — it matches the component shape but must NOT be
// treated as resolved.
if (shell.includes("LEANBACK_LAUNCHER")) {
return {
stdout: "android/com.android.internal.app.ResolverActivity\n",
stderr: "",
};
}
// The plain LAUNCHER resolve then finds the real activity.
if (shell.startsWith("cmd package resolve-activity --brief ") && !shell.includes("-c ")) {
return { stdout: "com.example.tv/com.example.tv.MainActivity\n", stderr: "" };
}
return { stdout: "", stderr: "" };
});
const component = await resolveLauncherActivity("emulator-5556", "com.example.tv", true);
expect(component).toBe("com.example.tv/com.example.tv.MainActivity");
});
it("throws (not launches the chooser) when only the system ResolverActivity resolves", async () => {
execFileMock.mockImplementation(() => ({
stdout: "android/com.android.internal.app.ResolverActivity\n",
stderr: "",
}));
await expect(resolveLauncherActivity("emulator-5556", "com.example.tv", true)).rejects.toThrow(
/LEANBACK_LAUNCHER or LAUNCHER/
);
});
it("throws a leanback-aware error when nothing resolves on a TV target", async () => {
execFileMock.mockImplementation(() => ({ stdout: "", stderr: "" }));
await expect(resolveLauncherActivity("emulator-5556", "com.example.tv", true)).rejects.toThrow(
/LEANBACK_LAUNCHER or LAUNCHER/
);
});
});