import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:meshport_lab/features/lab_screen.dart'; void main() { const channel = MethodChannel('meshport/system_vpn'); final binding = TestWidgetsFlutterBinding.ensureInitialized(); // Isolate native startup from bundled personal profiles and external peers. const localProfile = ''' ipv4 = '10.79.0.10/24' listeners = [] stun_servers = [] stun_servers_v6 = [] [network_identity] network_name = 'ui-local-draft' network_secret = 'test-only' [flags] no_tun = true use_smoltcp = true disable_p2p = true [runtime] mode = 'application' '''; setUp( () => binding.defaultBinaryMessenger.setMockMethodCallHandler( channel, (call) async => call.method == 'load_profile' ? localProfile : null, ), ); tearDown( () => binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null), ); testWidgets( 'form connect starts real native engine and can stop it', (tester) async { tester.view.physicalSize = const Size(1400, 1600); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget(const MaterialApp(home: LabScreen())); final lab = tester.state(find.byType(LabScreen)) as dynamic; List logs() => (lab.logs as List).cast(); Finder field(String label) => find.byWidgetPredicate( (widget) => widget is TextField && widget.decoration?.labelText == label, ); Future waitForLog(String text) async { for (var i = 0; i < 60 && !logs().any((l) => l.contains(text)); i++) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 50)), ); await tester.pump(); } expect(logs(), contains(contains(text))); } await waitForLog('原生库已加载'); // Startup restores profiles first; let it finish before overriding them. await waitForLog('穿透'); // Mesh-only, in-app engine: no helper, VPN permission or proxy node. lab.runtimeMode = 'application'; lab.proxyProfile.feature = 'mesh'; lab.proxyProfile.entry = 'application'; await tester.tap(find.text('组网').last); await tester.pumpAndSettle(); await tester.tap(find.text('基础配置')); await tester.pumpAndSettle(); await tester.enterText(field('网络名称'), 'ui-local-test'); await tester.enterText(field('网络密钥'), 'test-only'); await tester.enterText(field('虚拟 IPv4 / 前缀'), '10.79.0.11/24'); await tester.tap(find.text('保存草稿')); await waitForLog('网络配置已保存到'); for (var i = 0; i < 40 && find.text('保存中…').evaluate().isNotEmpty; i++) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 50)), ); await tester.pump(); } await tester.pageBack(); await tester.pumpAndSettle(); await tester.tap(find.text('概览').last); await tester.pumpAndSettle(); await tester.tap(find.text('连接').last); await tester.pump(const Duration(milliseconds: 500)); for ( var i = 0; i < 40 && !logs().any((l) => l.contains('[启动组网引擎] 成功')); i++ ) { await tester.runAsync(() async { await Future.delayed(const Duration(milliseconds: 100)); }); await tester.pump(); } expect(logs(), contains(contains('[启动组网引擎] 成功'))); for ( var i = 0; i < 40 && find.text('引擎运行中 · 0 个节点').evaluate().isEmpty; i++ ) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 100)), ); await tester.pump(); } expect(find.text('引擎运行中 · 0 个节点'), findsOneWidget); expect(find.textContaining('当前设备'), findsOneWidget); expect(find.textContaining('节点 ID'), findsOneWidget); await tester.tap(find.text('断开连接')); for (var i = 0; i < 40 && find.text('尚未连接').evaluate().isEmpty; i++) { await tester.runAsync(() async { await Future.delayed(const Duration(milliseconds: 100)); }); await tester.pump(); } expect(find.text('尚未连接'), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); await tester.runAsync(() async { await Future.delayed(const Duration(milliseconds: 100)); }); }, skip: Platform.environment['MESHPORT_LIBRARY'] == null, ); }