Files
software-mansion__argent/packages/tool-server/test/command-on-path.test.ts
Ignacy Łątka 7400179b5b Windows support: Android + Chromium host control plane (#421)
## What

Makes Argent run on Windows. iOS Simulator is macOS-only (Apple), so on
Windows that means the **Android + Chromium** host control plane. The
blocker was twofold: (1) no Windows `simulator-server` binary, and (2)
POSIX-only assumptions in the tool-server.

Companion radon PR builds the binary and runs the Android emulator E2E
on Windows: software-mansion/radon#134.

## Changes

**Windows-aware tool-server / resolver / bundling**
- New shared `commandOnPath()` helper — `where` on Windows, `command -v`
via `/bin/sh` on POSIX. `android-binary`, `check-deps`, and `vega-cli`
previously hardcoded `/bin/sh`, which never matches on Windows.
- `android-binary`: append `.exe` to `adb`/`emulator` in the
`$ANDROID_HOME` fallback; probe `%LOCALAPPDATA%\Android\Sdk` (Android
Studio's Windows default).
- simulator-server resolver + dispatcher + `bundle-tools` +
`download-simulator-server.sh`: ship and resolve `simulator-server.exe`
on `win32` (new `simulatorServerBinaryName()`), add `win32` to the
bundled host keys, add the `simulator-server-argent-windows.exe`
download target with PE arch validation.
- `stop-metro`: resolve listening PIDs via `netstat -ano` on Windows
(`lsof` is POSIX-only and threw ENOENT).
- `adb.ts`: AVD root resolution used `$HOME` + `startsWith("/")`
(rejects `C:\…`); now `os.homedir()` + `path.isAbsolute()`.
- **`boot-device`**: the emulator was spawned `detached` with
`stdio:"ignore"`; on Windows a detached emulator with NUL stdout/stderr
never reaches `sys.boot_completed` (verified on `windows-latest`: the
identical flag set boots fine with real handles, hangs with NUL).
Redirect the detached emulator's output to a temp log file on win32.
POSIX keeps `"ignore"`.

**Tests** — cross-platform unit coverage for `commandOnPath` (both
branches), win32 `.exe` Android resolution, win32 `.exe`
simulator-server resolution, and a win32 AVD drive-path case; dep-gate
tests now mock at the `commandOnPath` boundary so they're
platform-agnostic.

**CI** — `windows-e2e.yml` (`windows-chromium` on `windows-latest`):
runs the resolver/dep unit tests natively on Windows, confirms `adb.exe`
resolution via the SDK root, then drives a **real headless Chrome over
CDP** through the tool-server (discover → screenshot → describe → tap →
observe the DOM mutation). Flow in `scripts/ci/windows-chromium-e2e.mjs`
(also runnable locally / in Docker). The `simulator-server.exe` build +
the full **Android-emulator E2E on Windows** live in radon CI (the
binary's source is the private radon repo).

## Verification (all green)
- **Chromium control plane on real Windows** (`windows-latest`): `✓
discovered chromium-cdp-9222 → ✓ screenshot → ✓ describe → ✓ gesture-tap
→ ✓ DOM mutation observed`. Resolver/dep unit tests run natively;
`adb.exe` resolves via the SDK root (it's *not* on PATH on the runner —
the gap the resolver closes).
- **Android emulator on real Windows** (radon#134, `windows-latest` /
WHPX): the real Argent path — `boot-device` → `{ booted: true }` →
screenshot via `simulator-server.exe` → gesture-tap — all green. (The
boot-device fix above is what makes the detached emulator reach
`boot_completed`.)
- **Local clean-room E2E**: the Chromium flow runs green in a headless
Linux Docker container (fresh `npm ci`, no host pollution).
- **No regressions**: full tool-server suite (1624) +
native-devtools-ios (16); ESLint/Prettier/typecheck clean; **Wayland
E2E** (Linux AVD boot) and **Vega VVD E2E** still pass (they exercise
the changed `adb.ts` / `boot-device.ts` / `vega-cli.ts` paths).

## Notes
Android-emulator E2E needs WHPX nested virtualization — confirmed
available on the current hosted `windows-latest` image (Server 2025);
the emulator boots hardware-accelerated in ~3 min.

---------

Co-authored-by: Ignacy Łątka <claude-hubert.gancarczyk@swmansion.com>
Co-authored-by: Ignacy Łątka <latekvo@users.noreply.github.com>
2026-07-22 13:53:32 +02:00

114 lines
4.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
// Mock execFile with the repo's standard idiom: the callback receives a single
// `{ stdout, stderr }` value (so `promisify(execFile)` resolves to that object),
// or an Error to model a non-zero exit (command not found).
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 result = execFileMock(cmd, args);
if (result instanceof Error) callback(result, { stdout: "", stderr: "" });
else callback(null, result ?? { stdout: "", stderr: "" });
},
};
});
import { commandOnPath } from "../src/utils/command-on-path";
const realPlatform = process.platform;
function setPlatform(platform: NodeJS.Platform): void {
Object.defineProperty(process, "platform", { value: platform, configurable: true });
}
describe("commandOnPath", () => {
beforeEach(() => execFileMock.mockReset());
afterEach(() => {
setPlatform(realPlatform);
vi.restoreAllMocks();
});
it("uses `command -v` via /bin/sh on POSIX and returns the trimmed path", async () => {
setPlatform("darwin");
execFileMock.mockReturnValue({ stdout: "/usr/bin/adb\n", stderr: "" });
const result = await commandOnPath("adb");
expect(result).toBe("/usr/bin/adb");
expect(execFileMock).toHaveBeenCalledWith("/bin/sh", ["-c", "command -v adb"]);
});
it("uses `where` on Windows and returns the first matching line", async () => {
setPlatform("win32");
// `where` prints one path per match, CRLF-terminated; the first wins.
execFileMock.mockReturnValue({
stdout: "C:\\Android\\platform-tools\\adb.exe\r\nC:\\other\\adb.bat\r\n",
stderr: "",
});
const result = await commandOnPath("adb");
expect(result).toBe("C:\\Android\\platform-tools\\adb.exe");
expect(execFileMock).toHaveBeenCalledWith("where", ["adb"]);
});
it("returns null when the command is not on PATH (non-zero exit)", async () => {
setPlatform("darwin");
execFileMock.mockReturnValue(new Error("not found"));
expect(await commandOnPath("nope")).toBeNull();
});
it("returns null on Windows when `where` finds nothing", async () => {
setPlatform("win32");
execFileMock.mockReturnValue(new Error("INFO: Could not find files"));
expect(await commandOnPath("nope")).toBeNull();
});
it("returns null when the resolver emits only blank lines", async () => {
setPlatform("win32");
execFileMock.mockReturnValue({ stdout: "\r\n \r\n", stderr: "" });
expect(await commandOnPath("adb")).toBeNull();
});
it("skips a CWD match (`where` searches CWD before PATH) and takes the PATH one", async () => {
setPlatform("win32");
vi.spyOn(process, "cwd").mockReturnValue("C:\\work\\repo");
// `where adb` lists the planted CWD copy first, the real SDK adb second.
execFileMock.mockReturnValue({
stdout: "C:\\work\\repo\\adb.exe\r\nC:\\Android\\platform-tools\\adb.exe\r\n",
stderr: "",
});
expect(await commandOnPath("adb")).toBe("C:\\Android\\platform-tools\\adb.exe");
});
it("matches the CWD case-insensitively when skipping it", async () => {
setPlatform("win32");
vi.spyOn(process, "cwd").mockReturnValue("C:\\Work\\Repo");
execFileMock.mockReturnValue({
stdout: "c:\\work\\repo\\adb.exe\r\nC:\\Android\\platform-tools\\adb.exe\r\n",
stderr: "",
});
expect(await commandOnPath("adb")).toBe("C:\\Android\\platform-tools\\adb.exe");
});
it("returns null when the only match is the CWD-planted binary", async () => {
setPlatform("win32");
vi.spyOn(process, "cwd").mockReturnValue("C:\\work\\repo");
execFileMock.mockReturnValue({ stdout: "C:\\work\\repo\\adb.exe\r\n", stderr: "" });
expect(await commandOnPath("adb")).toBeNull();
});
it("rejects names with shell/glob metacharacters without shelling out", async () => {
setPlatform("darwin");
for (const bad of ["adb; rm -rf /", "$(whoami)", "adb*", "a b", "`id`", ""]) {
expect(await commandOnPath(bad)).toBeNull();
}
// Guard short-circuits before any execFile — nothing was ever spawned.
expect(execFileMock).not.toHaveBeenCalled();
});
});