66 lines
2.3 KiB
Dart
66 lines
2.3 KiB
Dart
// 端到端集成测试:桌面端连接层 ↔ Rust 服务端。
|
||
//
|
||
// 运行前需先启动 Rust 服务端:cd .. && cargo run
|
||
// 运行:flutter test test/e2e_ws_test.dart
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
import 'package:xterm/xterm.dart';
|
||
|
||
import 'package:rtty_desktop/src/rtty_client.dart';
|
||
|
||
import 'support.dart';
|
||
|
||
void main() {
|
||
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
||
test('RttyClient connects to Rust server and renders command output',
|
||
() async {
|
||
if (!await isServerUp('127.0.0.1', 8080)) {
|
||
markTestSkipped('Rtty server not running');
|
||
return;
|
||
}
|
||
final terminal = Terminal();
|
||
final client = RttyClient(terminal);
|
||
|
||
final states = <ConnState>[];
|
||
String? sessionId;
|
||
final raw = StringBuffer();
|
||
client.onStateChanged = (s) => states.add(s);
|
||
client.onReady = (id, cols, rows) => sessionId = '$id:$cols:$rows';
|
||
client.onRawData = (d) => raw.write(d);
|
||
|
||
await client.connect(host: '127.0.0.1', port: 8080);
|
||
|
||
// 等待连接就绪(socket 已连接 + 收到 ready 帧)。
|
||
final deadline = DateTime.now().add(const Duration(seconds: 5));
|
||
while ((!client.isConnected || sessionId == null) &&
|
||
DateTime.now().isBefore(deadline)) {
|
||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||
}
|
||
|
||
expect(client.isConnected, isTrue,
|
||
reason: '未能连接服务端 states=$states');
|
||
expect(sessionId, isNotNull,
|
||
reason: '未收到 ready 帧 states=$states');
|
||
|
||
// 发送命令,等待回显。
|
||
client.sendInput('echo RTTY_E2E_${DateTime.now().millisecondsSinceEpoch}\r\n');
|
||
|
||
String? matchedLine;
|
||
final outDeadline = DateTime.now().add(const Duration(seconds: 8));
|
||
while (DateTime.now().isBefore(outDeadline)) {
|
||
await Future<void>.delayed(const Duration(milliseconds: 150));
|
||
final text = raw.toString();
|
||
final lines = text.split('\n');
|
||
matchedLine = lines.where((l) => l.contains('RTTY_E2E')).lastOrNull;
|
||
if (matchedLine != null) break;
|
||
}
|
||
|
||
expect(matchedLine, isNotNull,
|
||
reason: '未捕获到命令回显。流末尾:'
|
||
'${raw.toString().split('').length > 200 ? raw.toString().substring(raw.toString().length - 200) : raw.toString()}');
|
||
|
||
await client.disconnect();
|
||
expect(client.state, ConnState.disconnected);
|
||
});
|
||
}
|