90 lines
2.8 KiB
Dart
90 lines
2.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:iop_portal/main.dart';
|
|
import 'package:iop_portal/portal_config.dart';
|
|
import 'package:iop_portal/iop_wire/portal_wire_client.dart';
|
|
import 'package:iop_portal/gen/proto/iop/control.pb.dart';
|
|
import 'package:protobuf/protobuf.dart';
|
|
import 'package:fixnum/fixnum.dart';
|
|
|
|
class FakeWebSocket implements WebSocket {
|
|
final _controller = StreamController<dynamic>();
|
|
|
|
@override
|
|
StreamSubscription<dynamic> listen(
|
|
void Function(dynamic event)? onData, {
|
|
Function? onError,
|
|
void Function()? onDone,
|
|
bool? cancelOnError,
|
|
}) {
|
|
return _controller.stream.listen(
|
|
onData,
|
|
onError: onError,
|
|
onDone: onDone,
|
|
cancelOnError: cancelOnError,
|
|
);
|
|
}
|
|
|
|
@override
|
|
dynamic noSuchMethod(Invocation invocation) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
class FakePortalWireClient extends PortalWireClient {
|
|
final bool shouldSuccess;
|
|
final String mockMessage;
|
|
|
|
FakePortalWireClient({this.shouldSuccess = true, this.mockMessage = 'Welcome to Toki CP!'})
|
|
: super(FakeWebSocket());
|
|
|
|
@override
|
|
bool get isAlive => true;
|
|
|
|
@override
|
|
Future<Res> sendRequest<Req extends GeneratedMessage, Res extends GeneratedMessage>(
|
|
Req data, {
|
|
Duration timeout = const Duration(seconds: 30),
|
|
}) async {
|
|
if (data is PortalHelloRequest) {
|
|
final response = PortalHelloResponse()
|
|
..ready = shouldSuccess
|
|
..protocol = 'iop-wire-v1'
|
|
..serverTimeUnixNano = Int64(1716584400)
|
|
..message = mockMessage;
|
|
return response as Res;
|
|
}
|
|
throw UnimplementedError();
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('Portal App basic rendering and success handshake test', (WidgetTester tester) async {
|
|
final fakeClient = FakePortalWireClient(shouldSuccess: true);
|
|
|
|
await tester.pumpWidget(IopPortalApp(testClient: fakeClient));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
|
|
expect(find.text('IOP Portal'), findsOneWidget);
|
|
expect(find.text('HEALTH: OK'), findsOneWidget);
|
|
|
|
expect(find.text(PortalConfig.controlPlaneHttpUrl, findRichText: true), findsOneWidget);
|
|
expect(find.text(PortalConfig.controlPlaneWireUrl, findRichText: true), findsOneWidget);
|
|
|
|
expect(find.text('Connected'), findsOneWidget);
|
|
expect(find.textContaining('Handshake Success: Welcome to Toki CP!'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('Portal App connection error state test', (WidgetTester tester) async {
|
|
final fakeClient = FakePortalWireClient(shouldSuccess: false, mockMessage: 'Invalid Version');
|
|
|
|
await tester.pumpWidget(IopPortalApp(testClient: fakeClient));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
|
|
expect(find.text('Error'), findsOneWidget);
|
|
expect(find.textContaining('Handshake Rejected: Invalid Version'), findsOneWidget);
|
|
});
|
|
}
|