Initial MeshPort SDK and cross-platform client

This commit is contained in:
shenlei
2026-10-10 15:16:32 +09:00
commit e73e599165
664 changed files with 209894 additions and 0 deletions
@@ -0,0 +1,76 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_lab/features/lab_screen.dart';
void main() {
testWidgets(
'TOML import starts real native engine and can stop it',
(tester) async {
await tester.pumpWidget(const MaterialApp(home: LabScreen()));
await tester.runAsync(() async {
await Future<void>.delayed(const Duration(milliseconds: 500));
});
await tester.pump();
expect(find.textContaining('原生库已加载'), findsOneWidget);
await tester.tap(find.text('设置').last);
await tester.pumpAndSettle();
await tester.tap(find.text('导入'));
await tester.pumpAndSettle();
await tester.tap(find.text('粘贴 TOML 并连接'));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField).last, '''
ipv4 = "10.79.0.11"
listeners = []
stun_servers = []
stun_servers_v6 = []
[network_identity]
network_name = "ui-local-test"
network_secret = "test-only"
[flags]
default_protocol = "udp"
''');
await tester.tap(find.text('校验并连接').last);
await tester.pump(const Duration(milliseconds: 500));
await tester.pumpAndSettle();
for (
var i = 0;
i < 40 && find.textContaining('导入配置并启动引擎').evaluate().isEmpty;
i++
) {
await tester.runAsync(() async {
await Future<void>.delayed(const Duration(milliseconds: 100));
});
await tester.pump();
}
expect(find.textContaining('导入配置并启动引擎'), findsOneWidget);
await tester.tap(find.text('概览').last);
await tester.pumpAndSettle();
for (
var i = 0;
i < 40 && find.text('引擎运行中 · 0 个节点').evaluate().isEmpty;
i++
) {
await tester.runAsync(
() => Future<void>.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<void>.delayed(const Duration(milliseconds: 100));
});
await tester.pump();
}
expect(find.text('尚未连接'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
await tester.runAsync(() async {
await Future<void>.delayed(const Duration(milliseconds: 100));
});
},
skip: !const bool.hasEnvironment('MESHPORT_LIBRARY'),
);
}
+129
View File
@@ -0,0 +1,129 @@
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;
default:
throw StateError('Unexpected call: ${call.method}');
}
});
});
tearDown(
() =>
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null),
);
Future<void> waitFor(WidgetTester tester, Finder finder) async {
for (var i = 0; i < 60 && finder.evaluate().isEmpty; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
await tester.pump();
}
expect(finder, findsWidgets);
}
Future<void> openSettings(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: LabScreen()));
await waitFor(tester, find.textContaining('原生库已加载'));
await tester.tap(find.text('设置').last);
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 openSettings(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 waitFor(tester, find.textContaining('网络配置已保存到 macOS 钥匙串'));
expect(stored, contains('我的网络'));
expect(find.text('尚未连接'), findsNothing); // The settings page is selected.
await tester.pumpWidget(const SizedBox.shrink());
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 100)),
);
await openSettings(tester);
await waitFor(tester, find.textContaining('已自动载入保存的网络配置'));
expect(tester.widget<TextField>(field('网络名称')).controller!.text, '我的网络');
expect(
tester.widget<TextField>(field('网络密钥')).controller!.text,
'test-only-secret',
);
expect(
tester.widget<TextField>(field('节点名称')).controller!.text,
'saved-node',
);
expect(
tester.widget<TextField>(field('虚拟 IPv4')).controller!.text,
'10.79.0.11/24',
);
await tester.tap(find.text('概览').last);
await tester.pumpAndSettle();
expect(find.text('尚未连接'), findsOneWidget);
await tester.tap(find.text('设置').last);
await tester.pumpAndSettle();
await tester.tap(find.text('删除已保存配置'));
await waitFor(tester, find.textContaining('已删除保存的配置'));
expect(stored, isNull);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: !const bool.hasEnvironment('MESHPORT_LIBRARY'),
);
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 openSettings(tester);
await waitFor(tester, find.textContaining('已自动载入保存的网络配置'));
await tester.ensureVisible(find.text('保存配置'));
await tester.tap(find.text('保存配置'));
await waitFor(tester, find.textContaining('网络配置已保存到 macOS 钥匙串'));
expect(stored, original);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: !const bool.hasEnvironment('MESHPORT_LIBRARY'),
);
}
+26
View File
@@ -0,0 +1,26 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_flutter/meshport_flutter.dart';
void main() {
test(
'Quality includes timeouts in loss but excludes them from RTT percentiles',
() {
final summary = QualitySummary([
{'success': true, 'elapsed_ms': 10},
{'success': true, 'elapsed_ms': 20},
{'success': false},
{'success': true, 'elapsed_ms': 30},
]);
expect(summary.lossPercent, 25);
expect(summary.medianMs, 20);
expect(summary.p95Ms, 30);
expect(summary.jitterMs, 10);
expect(
QualitySummary([
{'success': false},
]).medianMs,
isNull,
);
},
);
}
@@ -0,0 +1,53 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_flutter/meshport_flutter.dart';
void main() {
test(
'Unix control serializes replies and removes socket on close',
() async {
String? path;
Socket? helper;
final operations = <String>[];
final client = await openMacSystemVpn(
launch: (address, uid) async {
path = address;
expect(uid, greaterThan(0));
helper = await Socket.connect(
InternetAddress(address, type: InternetAddressType.unix),
0,
);
helper!
.cast<List<int>>()
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((line) {
final request = jsonDecode(line) as Map<String, dynamic>;
final operation = request['op'] as String;
operations.add(operation);
helper!.write(
'${jsonEncode({
'ok': true,
'data': {'operation': operation},
})}\n',
);
});
},
);
try {
final results = await Future.wait([
client.request({'op': 'status'}),
client.request({'op': 'validate', 'toml': ''}),
]);
expect(results.map((r) => r['operation']), ['status', 'validate']);
await client.close();
expect(operations, ['status', 'validate', 'stop']);
expect(await Directory(File(path!).parent.path).exists(), false);
} finally {
helper?.destroy();
}
},
skip: !Platform.isMacOS,
);
}
+141
View File
@@ -0,0 +1,141 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_lab/features/network_telemetry.dart';
void main() {
testWidgets('Shows current device before any remote peer is discovered', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: NodeTelemetry(
status: {
'local_peer': {
'peer_id': 42,
'hostname': 'MeshPort-Lab',
'ipv4': '192.168.20.5/24',
'state': 'Running',
'udp_nat_type': 'Symmetric',
'version': 'test-core',
},
'peers': [],
'routes': [],
},
),
),
),
);
expect(find.text('MeshPort-Lab · 当前设备'), findsOneWidget);
expect(find.textContaining('192.168.20.5/24'), findsOneWidget);
expect(find.textContaining('节点 ID 42'), findsOneWidget);
expect(find.text('运行中'), findsOneWidget);
expect(find.text('尚未发现其他节点。'), findsOneWidget);
expect(find.textContaining('中继'), findsNothing);
expect(find.textContaining('ms'), findsNothing);
});
testWidgets('Displays direct RTT and distinguishes relayed estimates', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: NodeTelemetry(
status: {
'peers': [
{
'peer_id': 1,
'direct': true,
'latency_ms': 23.4,
'rx_bytes': 2048,
'tx_bytes': 1024,
'rx_bytes_per_second': 1024.0,
'tx_bytes_per_second': 512.0,
},
],
'routes': [
{'peer_id': 1, 'hostname': 'game-server', 'ipv4': '10.77.0.2'},
{
'peer_id': 2,
'hostname': 'relay-target',
'estimated_latency_ms': 89,
'next_hop_peer_id': 1,
},
],
},
),
),
),
);
expect(find.text('23.4 ms'), findsOneWidget);
expect(find.text('89.0 ms'), findsOneWidget);
expect(find.textContaining('链路 RTT'), findsOneWidget);
expect(find.textContaining('路由估算'), findsOneWidget);
expect(find.textContaining('1.0 KB/s'), findsOneWidget);
expect(find.textContaining('未单独计量'), findsOneWidget);
});
testWidgets('A direct connection can coexist with a relayed selected route', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: NodeTelemetry(
status: {
'peers': [
{'peer_id': 1, 'direct': true, 'latency_ms': 166.5},
],
'routes': [
{
'peer_id': 1,
'hostname': 'DIG-US',
'next_hop_peer_id': 2,
'estimated_latency_ms': 108,
'udp_nat_type': 'FullCone',
'version': '2.4.5',
},
],
},
),
),
),
);
expect(find.textContaining('中继 · 有直连可用'), findsOneWidget);
expect(find.text('108.0 ms'), findsOneWidget);
expect(find.text('166.5 ms'), findsNothing);
expect(find.textContaining('FullCone'), findsOneWidget);
});
testWidgets('Punched Redmi AX6 uses measured RTT and direct label', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: NodeTelemetry(
status: {
'peers': [
{
'peer_id': 3157594741,
'direct': true,
'hole_punched': true,
'latency_ms': 7.497,
},
],
'routes': [
{
'peer_id': 3157594741,
'hostname': 'Redmi_AX6',
'next_hop_peer_id': 3157594741,
'estimated_latency_ms': 7,
},
],
},
),
),
),
);
expect(find.text('7.5 ms'), findsOneWidget);
expect(find.textContaining('直连 · 打洞建立 · 链路 RTT'), findsOneWidget);
expect(find.textContaining('中继'), findsNothing);
});
}
+19
View File
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_lab/features/lab_screen.dart';
void main() {
testWidgets('Starts disconnected and exposes networking configuration', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(home: LabScreen(initializeNative: false)),
);
expect(find.text('尚未连接'), findsOneWidget);
await tester.tap(find.text('设置').last);
await tester.pumpAndSettle();
expect(find.text('网络密钥'), findsOneWidget);
expect(find.text('校验并连接'), findsOneWidget);
expect(find.text('保存配置'), findsOneWidget);
});
}