import assert from "node:assert/strict"; import test from "node:test"; import { forwardAndroidApi } from "./android-api.mjs"; function fixture(env, output = "List of devices attached\nphone-a\tdevice\n") { const calls = []; const run = (_command, args) => { calls.push(args); return args[0] === "devices" ? output : ""; }; return { env, run, calls }; } await test("Android loopback API is forwarded only to the authorized device", () => { const setup = fixture( { TAURI_ENV_PLATFORM: "android" }, "List of devices attached\nlocked-phone\tunauthorized\nphone-a\tdevice\n", ); forwardAndroidApi(setup); assert.deepEqual(setup.calls.at(-1), [ "-s", "phone-a", "reverse", "tcp:8080", "tcp:8080", ]); }); await test("desktop and remote API development do not change device forwarding", () => { for (const env of [ {}, { TAURI_ENV_PLATFORM: "android", ALTITUDE_API_URL: "https://api.example.com/api", }, ]) { const setup = fixture(env); forwardAndroidApi(setup); assert.deepEqual(setup.calls, []); } }); await test("multiple devices require an explicit selection", () => { const setup = fixture( { TAURI_ENV_PLATFORM: "android" }, "List of devices attached\nphone-a\tdevice\nphone-b\tdevice\n", ); assert.throws(() => forwardAndroidApi(setup), /ANDROID_SERIAL/); assert.equal(setup.calls.length, 1); }); await test("an explicitly selected device uses a custom local API port", () => { const setup = fixture( { TAURI_ENV_PLATFORM: "android", ANDROID_SERIAL: "phone-b", ALTITUDE_API_URL: "http://localhost:9090/api", }, "List of devices attached\nphone-a\tdevice\nphone-b\tdevice\n", ); forwardAndroidApi(setup); assert.deepEqual(setup.calls.at(-1), [ "-s", "phone-b", "reverse", "tcp:9090", "tcp:9090", ]); }); await test("an unauthorized or missing selected device does not receive a reverse mapping", () => { const setup = fixture( { TAURI_ENV_PLATFORM: "android", ANDROID_SERIAL: "locked-phone" }, "List of devices attached\nlocked-phone\tunauthorized\n", ); assert.throws(() => forwardAndroidApi(setup), /not ready/); assert.equal(setup.calls.length, 1); });