import 'dart:async'; import 'package:material_ui/material_ui.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:ads_manager/data/agent_repository.dart'; import 'package:ads_manager/data/agent_service.dart'; import 'package:ads_manager/ui/agent_model.dart'; import 'package:ads_manager/ui/agent_view.dart'; class FakeApi implements AgentApi { @override String connectionKey = 'first-center'; @override String adminToken = ''; final calls = []; Completer>? pending; JsonMap status(String id) => { 'id': id, 'name': id, 'pid': 123, 'supervised': true, 'devices': [ { 'id': '$id-phone', 'target': '127.0.0.1:5555', 'enabled': true, 'connected': true, }, ], }; @override Future> list() async => pending == null ? [ for (final id in ['one', 'two']) {'id': id, 'online': true, 'status': status(id)}, ] : pending!.future; @override Future request(String agent, JsonMap operation) async { calls.add({...operation, 'agent': agent}); return switch (operation['op']) { 'devices_discover' => [ { 'serial': '192.168.1.2:5555', 'state': 'device', 'configured': false, }, ], 'config_get' => { 'id': agent, 'devices': [], 'ignored_usb_serials': ['USB-A', 'USB-B'], 'usb_device_ids': {'USB-A': 'stable-id'}, }, 'logs' => { 'instance': 'i', 'next': 1, 'entries': [ {'time': 1, 'level': 'INFO', 'message': 'private-token'}, ], }, _ => {}, }; } @override Future local(String config, JsonMap operation) async => status('local'); @override Future importAdmin(String path) async {} @override Future initialize(String path, JsonMap config, String token) async {} @override Future start(String path) async {} @override Future install(String path) async {} @override String redact(String text) => text.replaceAll('private-token', '[REDACTED]'); } void main() { test( 'USB policy applies only to selected agent and preserves identity memory', () async { final api = FakeApi(); final m = AgentModel(AgentRepository(api)); await m.refresh(); m.select('two'); await m.setAutoUsb(true); final enabled = api.calls.lastWhere((c) => c['op'] == 'config_set'); expect(enabled['agent'], 'two'); expect((enabled['config'] as Map)['auto_register_usb'], true); expect((enabled['config'] as Map)['usb_device_ids'], { 'USB-A': 'stable-id', }); m.adbSerial = 'USB-A'; await m.unignoreUsb(); expect((api.calls.last['config'] as Map)['ignored_usb_serials'], [ 'USB-B', ]); await m.setAutoUsb(false); expect( (api.calls.lastWhere((c) => c['op'] == 'config_set')['config'] as Map)['auto_register_usb'], false, ); m.dispose(); }, ); test( 'selected agent scopes mutations and log secrets are redacted', () async { final api = FakeApi(); final m = AgentModel(AgentRepository(api)); await m.refresh(); expect(m.agents.length, 2); m.select('two'); m.deviceId = 'new-phone'; m.target = '127.0.0.1:5555'; await m.addDevice(); expect(api.calls.last['agent'], 'two'); expect(api.calls.last['op'], 'device_add'); await m.deviceOperation('device_enable', enabled: false); expect(api.calls.last['id'], 'two-phone'); expect(api.calls.last['enabled'], false); await m.discoverDevices(); m.chooseDiscovered(m.discoveredDevices.single); expect(m.target, '192.168.1.2:5555'); expect(m.adbSerial, isEmpty); await m.readLogs(); expect(m.logText, contains('[REDACTED]')); expect(m.logText, isNot(contains('private-token'))); expect(m.logText.endsWith('\n'), true); expect(() => m.agents.clear(), throwsUnsupportedError); m.dispose(); }, ); test('late remote response cannot overwrite local mode', () async { final api = FakeApi()..pending = Completer>(); final m = AgentModel(AgentRepository(api)); final refresh = m.refresh(); m.setLocal(true); api.pending!.complete([ {'id': 'old', 'online': true, 'status': api.status('old')}, ]); await refresh; expect(m.agents, isEmpty); await m.refresh(); expect(m.selectedId, 'local'); m.dispose(); }); test('changing center blocks mutations from a stale list', () async { final api = FakeApi(); final m = AgentModel(AgentRepository(api)); await m.refresh(); api.connectionKey = 'second-center'; await m.restart(); expect(api.calls, isEmpty); expect(m.notice, contains('先刷新')); await m.refresh(); await m.restart(); expect(api.calls.last['op'], 'restart'); m.dispose(); }); for (final width in [620.0, 1280.0]) { testWidgets('agent management layout and selection at $width', ( tester, ) async { tester.view.physicalSize = Size(width, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final m = AgentModel(AgentRepository(FakeApi())); await m.refresh(); await tester.pumpWidget( MaterialApp( home: Scaffold(body: AgentView(model: m)), ), ); await tester.pumpAndSettle(); expect(find.textContaining('two · 在线'), findsOneWidget); await tester.tap(find.textContaining('two · 在线')); await tester.pumpAndSettle(); expect(m.selectedId, 'two'); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); m.dispose(); }); } }