Files
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

238 lines
10 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { Registry, ServiceState } from "@argent/registry";
import { createStopSimulatorServerTool } from "../src/tools/simulator/stop-simulator-server";
import { createStopAllSimulatorServersTool } from "../src/tools/simulator/stop-all-simulator-servers";
import { stopMetroTool } from "../src/tools/simulator/stop-metro";
function createMockRegistry(services: Map<string, { state: ServiceState; dependents: string[] }>) {
return {
getSnapshot: vi.fn(() => ({
services,
namespaces: [],
tools: [],
})),
disposeService: vi.fn(async () => {}),
} as unknown as Registry;
}
describe("stop-simulator-server", () => {
it("disposes the correct URN for a running simulator", async () => {
const services = new Map([
["SimulatorServer:AAAA-BBBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "AAAA-BBBB" });
expect(result).toEqual({ stopped: true, udid: "AAAA-BBBB" });
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:AAAA-BBBB");
});
it("returns stopped: false for a UDID with no running server", async () => {
const services = new Map<string, { state: ServiceState; dependents: string[] }>();
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "CCCC-DDDD" });
expect(result).toEqual({ stopped: false, udid: "CCCC-DDDD" });
expect(registry.disposeService).not.toHaveBeenCalled();
});
it("returns stopped: false for an IDLE simulator", async () => {
const services = new Map([
["SimulatorServer:EEEE-FFFF", { state: ServiceState.IDLE, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "EEEE-FFFF" });
expect(result).toEqual({ stopped: false, udid: "EEEE-FFFF" });
expect(registry.disposeService).not.toHaveBeenCalled();
});
it("returns stopped: false for an ERROR node (e.g. tvOS) but still cleans it up", async () => {
// A tvOS UDID: the SimulatorServer blueprint throws on start, leaving the
// node in ERROR. It never ran, so we must not report stopped: true — but we
// still dispose to clear the dead node.
const services = new Map([
["SimulatorServer:TV-UDID", { state: ServiceState.ERROR, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "TV-UDID" });
expect(result).toEqual({ stopped: false, udid: "TV-UDID" });
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:TV-UDID");
});
it("reports stopped: true for a STARTING simulator", async () => {
const services = new Map([
["SimulatorServer:GGGG-HHHH", { state: ServiceState.STARTING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "GGGG-HHHH" });
expect(result).toEqual({ stopped: true, udid: "GGGG-HHHH" });
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:GGGG-HHHH");
});
it("stops the live TvControl daemon for a tvOS UDID whose SimulatorServer never ran", async () => {
// A tvOS UDID (iOS-shaped) holds a live TvControl service that owns the
// spawned tvos-ax / tvos-hid daemons, while its SimulatorServer node sits in
// ERROR (the blueprint rejects tvOS). Stopping the device must reap the TV
// daemon, not just clean up the dead SimulatorServer node.
const udid = "12345678-1234-1234-1234-123456789012";
const services = new Map([
[`SimulatorServer:${udid}`, { state: ServiceState.ERROR, dependents: [] }],
[`TvControl:${udid}`, { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid });
expect(result).toEqual({ stopped: true, udid });
expect(registry.disposeService).toHaveBeenCalledWith(`SimulatorServer:${udid}`);
expect(registry.disposeService).toHaveBeenCalledWith(`TvControl:${udid}`);
});
it("stops the live AndroidTvControl service for an Android TV serial", async () => {
const serial = "emulator-5554";
const services = new Map([
[`AndroidTvControl:${serial}`, { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: serial });
expect(result).toEqual({ stopped: true, udid: serial });
expect(registry.disposeService).toHaveBeenCalledWith(`AndroidTvControl:${serial}`);
});
it("does not target TvControl for a chromium id", async () => {
const services = new Map([
["ChromiumCdp:chromium-cdp-9222", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopSimulatorServerTool(registry);
const result = await tool.execute!({}, { udid: "chromium-cdp-9222" });
expect(result).toEqual({ stopped: true, udid: "chromium-cdp-9222" });
expect(registry.disposeService).toHaveBeenCalledOnce();
expect(registry.disposeService).toHaveBeenCalledWith("ChromiumCdp:chromium-cdp-9222");
});
});
describe("stop-all-simulator-servers", () => {
it("disposes all running SimulatorServer URNs", async () => {
const services = new Map([
["SimulatorServer:AAA", { state: ServiceState.RUNNING, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
["JsRuntimeDebugger:CCC", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({
stopped: ["SimulatorServer:AAA", "SimulatorServer:BBB"],
});
expect(registry.disposeService).toHaveBeenCalledTimes(2);
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:AAA");
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:BBB");
});
it("returns empty list when no simulators are running", async () => {
const services = new Map<string, { state: ServiceState; dependents: string[] }>();
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({ stopped: [] });
expect(registry.disposeService).not.toHaveBeenCalled();
});
it("skips IDLE simulators", async () => {
const services = new Map([
["SimulatorServer:AAA", { state: ServiceState.IDLE, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({ stopped: ["SimulatorServer:BBB"] });
expect(registry.disposeService).toHaveBeenCalledOnce();
});
it("disposes an ERROR node (e.g. tvOS) but omits it from the stopped list", async () => {
const services = new Map([
["SimulatorServer:TV-UDID", { state: ServiceState.ERROR, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
// Both get disposed (cleanup), but only the live one is reported as stopped.
expect(result).toEqual({ stopped: ["SimulatorServer:BBB"] });
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:TV-UDID");
expect(registry.disposeService).toHaveBeenCalledWith("SimulatorServer:BBB");
expect(registry.disposeService).toHaveBeenCalledTimes(2);
});
it("stops the focus-driven TV control services (Apple TV + Android TV)", async () => {
// The TvControl daemon owns the spawned tvos-ax / tvos-hid processes, so a
// session-end stop must dispose it — not just the simulator-server/CDP nodes.
const services = new Map([
["TvControl:APPLE-TV", { state: ServiceState.RUNNING, dependents: [] }],
["AndroidTvControl:emulator-5556", { state: ServiceState.RUNNING, dependents: [] }],
["SimulatorServer:BBB", { state: ServiceState.RUNNING, dependents: [] }],
]);
const registry = createMockRegistry(services);
const tool = createStopAllSimulatorServersTool(registry);
const result = await tool.execute!({}, undefined);
expect(result).toEqual({
stopped: ["TvControl:APPLE-TV", "AndroidTvControl:emulator-5556", "SimulatorServer:BBB"],
});
expect(registry.disposeService).toHaveBeenCalledWith("TvControl:APPLE-TV");
expect(registry.disposeService).toHaveBeenCalledWith("AndroidTvControl:emulator-5556");
expect(registry.disposeService).toHaveBeenCalledTimes(3);
});
});
describe("stop-metro", () => {
it("defaults to port 8081", () => {
expect(stopMetroTool.zodSchema).toBeDefined();
const parsed = stopMetroTool.zodSchema!.parse({});
expect(parsed.port).toBe(8081);
});
it("accepts a custom port", () => {
const parsed = stopMetroTool.zodSchema!.parse({ port: 9090 });
expect(parsed.port).toBe(9090);
});
it("returns stopped: false when no process on port", async () => {
// Use a high port unlikely to have anything listening
const result = await stopMetroTool.execute!({}, { port: 59999 });
expect(result.stopped).toBe(false);
expect(result.port).toBe(59999);
expect(result.pids).toEqual([]);
});
});