Files
Rtty/desktop/test/e2e_ws_test.dart

68 lines
2.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 端到端集成测试桌面端连接层RttyPtyBackend↔ Rust 服务端。
//
// 运行前需先启动 Rust 服务端cd .. && cargo run
// 运行flutter test test/e2e_ws_test.dart
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:rtty_desktop/src/rtty_pty_backend.dart';
import 'support.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('RttyPtyBackend connects to Rust server and receives raw ANSI output',
() async {
if (!await isServerUp('127.0.0.1', 8080)) {
markTestSkipped('Rtty server not running');
return;
}
final backend = RttyPtyBackend();
final states = <ConnState>[];
String? sessionId;
final raw = StringBuffer();
backend.onStateChanged = (s) => states.add(s);
backend.onReady = (id, cols, rows) => sessionId = '$id:$cols:$rows';
backend.output.listen((Uint8List b) {
raw.write(String.fromCharCodes(b));
});
await backend.connect(host: '127.0.0.1', port: 8080);
// 等待连接就绪socket 已连接 + 收到 ready 帧)。
final deadline = DateTime.now().add(const Duration(seconds: 5));
while ((backend.state != ConnState.connected || sessionId == null) &&
DateTime.now().isBefore(deadline)) {
await Future<void>.delayed(const Duration(milliseconds: 100));
}
expect(backend.state, ConnState.connected,
reason: '未能连接服务端 states=$states');
expect(sessionId, isNotNull, reason: '未收到 ready 帧 states=$states');
// 发送命令,等待回显。
backend.write(Uint8List.fromList(
'echo RTTY_E2E_${DateTime.now().millisecondsSinceEpoch}\r\n'.codeUnits));
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().length > 200 ? raw.toString().substring(raw.toString().length - 200) : raw.toString()}');
await backend.close();
expect(backend.state, ConnState.disconnected);
});
}