import 'dart:io'; import 'package:ads_manager/data/manager_repository.dart'; Future main(List args) async { final repo = ManagerRepository(stdout.writeln); try { await repo.init(); repo.settings.usbMode = args.contains('--usb'); final devices = await repo.devices(); stdout.writeln( 'API devices: ${devices.map((d) => "${d.id} busy=${d.busy}").join(", ")}', ); final nodes = await repo.get('v2/nodes') as Map; stdout.writeln('Relay nodes: ${(nodes["nodes"] as List).length}'); stdout.writeln(await repo.run(repo.settings.adb, ['devices', '-l'])); if (!args.contains('--connect')) { return; } final device = devices.where((d) => !d.busy).firstOrNull; if (device == null) { stdout.writeln('SKIP: no idle device'); return; } final s = await repo.connect(device.id); for (var i = 0; i < 20 && !s.ready && !s.exited; i++) { await Future.delayed(const Duration(seconds: 3)); await repo.monitor(); } if (!s.ready) { throw StateError('ADB not ready: ${s.state}'); } stdout.writeln( 'Remote model: ${await repo.run(s.adb, ['-s', s.endpoint, 'shell', 'getprop', 'ro.product.model'])}', ); if (args.contains('--screen')) { await repo.screen(s); await Future.delayed(const Duration(seconds: 8)); if (s.screen == null) { throw StateError('scrcpy exited early'); } stdout.writeln('scrcpy process alive: ${s.screen!.pid}'); } if (args.contains('--usb')) { final path = await repo.run(s.adb, ['-s', s.endpoint, 'get-devpath']); // Windows ADB may return "unknown" for get-devpath. USB-only selection // must either select this virtual serial or report multiple USB devices. try { final selected = await repo.run(s.adb, ['-d', 'get-serialno']); if (selected.trim() != s.endpoint) { throw StateError('USB-only selection did not include virtual device'); } } on ProcessException catch (error) { if (!error.message.contains('more than one')) { rethrow; } } stdout.writeln('Native USB-only selection verified; get-devpath=$path'); await repo.disconnect(s.id); await Future.delayed(const Duration(seconds: 2)); final listing = await repo.run(s.adb, ['devices']); if (listing .split('\n') .any((line) => line.startsWith('${s.endpoint}\t'))) { throw StateError('USB transport was not removed'); } if (repo.reservedPorts.isNotEmpty) { throw StateError('Ports leaked after USB disconnect'); } } stdout.writeln('LIVE PASS'); } finally { await repo.shutdown(); } }