Files
MeshPort/apps/meshport_lab/test/profile_test.dart
T
shenleiandClaude Opus 5.5 dc1bfc99d2 Move mesh networking logic into the SDK and fix lifecycle bugs
SDK:
- Add build_config/migrate_config, mesh_cidrs, route_generation, health
  and start_bridged; expose abi_info (capability revision 2).
- Add wait_routes long-poll; stop wakes waiters, and context_free now
  stops the network immediately instead of waiting for waiters to time out.

Hosts:
- Dart: waitRoutes(), MeshProxySession route watcher, ABI capability check.
- Android: fix a deadlock where every VPN request waited on its own
  single-thread worker; requests are now async, status is served from a
  snapshot, and combined mode reports Partial when the mesh stops.
- macOS helper: mesh-only unified mode stops when the mesh is lost;
  installed_subnet_routes excludes the overlay.
- Lab: restore Partial sessions, write migrated profiles back, remove the
  unreachable TOML import dialog; native test now connects via the form.

Docs updated accordingly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 18:17:42 +09:00

166 lines
5.5 KiB
Dart

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();
String? stored;
setUp(() {
stored = null;
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
switch (call.method) {
case 'load_profile':
return stored;
case 'save_profile':
stored = call.arguments as String;
return null;
case 'delete_profile':
stored = null;
return null;
case 'save_proxy_profile':
return null;
default:
throw StateError('Unexpected call: ${call.method}');
}
});
});
tearDown(
() =>
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null),
);
// The overview shows only the newest log lines; read the full log list.
List<String> logs(WidgetTester tester) =>
((tester.state(find.byType(LabScreen, skipOffstage: false)) as dynamic)
.logs
as List)
.cast<String>();
Future<void> waitForLog(WidgetTester tester, String text) async {
for (
var i = 0;
i < 100 && !logs(tester).any((line) => line.contains(text));
i++
) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
await tester.pump();
}
expect(logs(tester), contains(contains(text)));
}
Future<void> openApp(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: LabScreen()));
await waitForLog(tester, '原生库已加载');
await waitForLog(tester, '载入');
}
Future<void> openPage(WidgetTester tester, String tab, String entry) async {
await tester.tap(find.text(tab).last);
await tester.pumpAndSettle();
await tester.tap(find.text(entry));
await tester.pumpAndSettle();
}
Future<void> back(WidgetTester tester) async {
await tester.pageBack();
await tester.pumpAndSettle();
}
Finder field(String label) => find.byWidgetPredicate(
(widget) => widget is TextField && widget.decoration?.labelText == label,
);
testWidgets(
'save without connecting, restore on restart, delete persisted profile',
(tester) async {
tester.view.physicalSize = const Size(1400, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await openApp(tester);
await openPage(tester, '组网', '基础配置');
await tester.enterText(field('网络名称'), '我的网络');
await tester.enterText(field('网络密钥'), 'test-only-secret');
await tester.enterText(field('节点名称'), 'saved-node');
await tester.enterText(field('虚拟 IPv4 / 前缀'), '10.79.0.11/24');
await tester.tap(find.text('保存草稿'));
await waitForLog(tester, '网络配置已保存到');
expect(stored, contains('我的网络'));
await tester.pumpWidget(const SizedBox.shrink());
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 100)),
);
await openApp(tester);
await waitForLog(tester, '已自动载入保存的网络配置');
await openPage(tester, '组网', '基础配置');
String text(String label) =>
tester.widget<TextField>(field(label)).controller!.text;
expect(text('网络名称'), '我的网络');
expect(text('网络密钥'), 'test-only-secret');
expect(text('节点名称'), 'saved-node');
expect(text('虚拟 IPv4 / 前缀'), '10.79.0.11/24');
await back(tester);
await openPage(tester, '设置', '默认配置');
await tester.tap(find.text('删除已保存配置'));
await waitForLog(tester, '已删除保存的配置');
expect(stored, isNull);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: Platform.environment['MESHPORT_LIBRARY'] == null,
);
testWidgets(
'unchanged restored profile retains advanced TOML',
(tester) async {
stored = '''
hostname = "advanced-node"
ipv4 = "10.79.0.11/24"
listeners = []
stun_servers = []
stun_servers_v6 = []
[network_identity]
network_name = "advanced"
network_secret = "test-only"
[flags]
disable_udp_hole_punching = true
''';
final original = stored;
await openApp(tester);
await waitForLog(tester, '已自动载入保存的网络配置');
await openPage(tester, '设置', '默认配置');
await tester.tap(find.text('保存配置'));
await waitForLog(tester, '网络配置已保存到');
expect(stored, original);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: Platform.environment['MESHPORT_LIBRARY'] == null,
);
testWidgets(
'legacy profile is migrated and written back on restore',
(tester) async {
stored = '''
hostname = "legacy-node"
ipv4 = "10.79.0.11/24"
subnet_routes = ["192.168.21.0/24"]
[network_identity]
network_name = "legacy"
network_secret = "test-only"
''';
await openApp(tester);
await waitForLog(tester, '已自动载入保存的网络配置');
expect(stored, isNot(contains('subnet_routes')));
expect(stored, contains('legacy-node'));
await tester.pumpWidget(const SizedBox.shrink());
},
skip: Platform.environment['MESHPORT_LIBRARY'] == null,
);
}