sieobex/tests/device.ts
2026-10-01 02:46:52 -05:00

93 lines
4.2 KiB
TypeScript

// The same CLI suite against a real phone on a real cable.
//
// SIEOBEX_E2E_DEVICE=/dev/ttyUSB0 pnpm test:e2e:device
//
// Nothing here is emulated, so nothing about the phone is known in advance:
// the model and the platform come out of the phone and only their shape is
// checked. What cannot be discovered is where the tests may write, because
// the folders differ per generation - /Data/Misc on x65 and x75, no /Data
// at all on EGOLD - so SIEOBEX_E2E_DIR overrides the default.
//
// This writes to the phone. The suite creates sieobex-e2e.bin and a
// sieobex-tree folder inside that directory and deletes them again; it
// touches nothing else, but it is a real file system and worth pointing at
// a directory of no consequence.
import type { PhoneTarget } from "./target.js";
const DEFAULT_DIR = "/Data/Misc";
// A real phone is back on its AT interpreter a moment after the +++ escape,
// as long as the escape was allowed to finish. Five seconds is generous
// then, and raising it is a variable away if a phone wants more.
const DEFAULT_RECONNECT_WAIT_MS = 5000;
// Unlike an emulator, a real phone is not booting: a handshake that fails
// is worth reporting rather than retrying in silence, and a phone left in
// OBEX mode is escaped out of it by connect rather than waited out.
const DEFAULT_READY_TIMEOUT_MS = 45000;
export function deviceTarget(): PhoneTarget {
const device = process.env.SIEOBEX_E2E_DEVICE;
const writableDir = process.env.SIEOBEX_E2E_DIR ?? DEFAULT_DIR;
const wait = Number(process.env.SIEOBEX_E2E_RECONNECT_WAIT_MS ?? DEFAULT_RECONNECT_WAIT_MS);
const baudRate = process.env.SIEOBEX_E2E_BAUDRATE;
const baudRates = parseBaudRates(process.env.SIEOBEX_E2E_BAUDRATES);
return {
name: device ?? "a real phone",
unavailable: unavailable(device, writableDir, wait),
// Whatever is plugged in: discovered, not asserted
deviceName: undefined,
platform: undefined,
writableDir,
// The default is a guess that only holds for SGOLD and NewSGOLD; a
// directory named on purpose is never second guessed
mayPickWritableDir: !process.env.SIEOBEX_E2E_DIR,
// The root, because it is the one directory every phone has and
// never has empty: the writable one is empty again by the time the
// suite has cleaned up after itself, and a listing of nothing
// proves nothing. Point SIEOBEX_E2E_SMALL_FILES_DIR at a folder of
// real files to exercise the sizes and dates as well.
smallFilesDir: process.env.SIEOBEX_E2E_SMALL_FILES_DIR ?? "/",
reconnectWaitMS: wait,
readyTimeoutMS: Number(process.env.SIEOBEX_E2E_READY_TIMEOUT_MS ?? DEFAULT_READY_TIMEOUT_MS),
// A session each, so trim the list to keep a hardware run short
baudRates,
// Pinning the speed skips the AT probe, which otherwise walks
// 115200, 57600, 19200, 230400 and 9600 until the phone answers
connectArgs: baudRate ? ["--baudrate", baudRate] : undefined,
async start() {
return {
serial: device!,
// The phone keeps no log for us; sieobex's own is in the failure
log: () => "",
stop: async () => {},
};
},
};
}
// Speeds to pin a session to, one session each. The default is the three
// a Siemens data cable is actually seen at, all of them in the handshake's
// own probe list; `SIEOBEX_E2E_BAUDRATES=115200` keeps a hardware run
// short, and an empty value skips the test.
const DEFAULT_BAUD_RATES = [115200, 57600, 19200];
function parseBaudRates(value: string | undefined): number[] {
if (value == null)
return DEFAULT_BAUD_RATES;
return value.split(",").map((part) => Number(part.trim())).filter((rate) => Number.isInteger(rate) && rate > 0);
}
function unavailable(device: string | undefined, writableDir: string, wait: number): string | undefined {
if (!device)
return "set SIEOBEX_E2E_DEVICE (e.g. /dev/ttyUSB0) to run these against a real phone";
// The suite deletes what it creates, and a mistyped root would aim that
// at the phone's own folders
if (!writableDir.startsWith("/") || writableDir == "/")
return `SIEOBEX_E2E_DIR must be a directory below the root, not ${JSON.stringify(writableDir)}`;
if (!Number.isFinite(wait) || wait < 0)
return `SIEOBEX_E2E_RECONNECT_WAIT_MS must be a number of milliseconds, not ${JSON.stringify(process.env.SIEOBEX_E2E_RECONNECT_WAIT_MS)}`;
return undefined;
}