Files
pipelines/app/test/application/runs_destination_test.dart
T

303 lines
10 KiB
Dart

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/app_session.dart';
import 'package:pipelines_app/application/runs_destination.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/runs/screen.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
ServerEndpoint webServer() => ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: true,
);
Widget materialApp(
AppSession session,
ServerEndpoint server, {
int? initialRunId,
ValueChanged<int>? onRunSelected,
ValueChanged<int?>? onBack,
}) => MaterialApp(
home: Scaffold(
body: RunsDestination(
session: session,
server: server,
initialRunId: initialRunId,
onRunSelected: onRunSelected,
onBack: onBack,
),
),
);
Map<String, dynamic>? listRequest(FakeChannel channel) => channel.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == 'list_runs')
.firstOrNull;
Iterable<Map<String, dynamic>> detailRequests(FakeChannel channel, int id) =>
channel.outgoing
.map((frame) => jsonDecode(frame as String) as Map<String, dynamic>)
.where((frame) => frame['type'] == 'get_run' && frame['run_id'] == id);
void main() {
testWidgets('gate explains missing native configuration without connecting', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = ServerEndpoint.resolve(
base: Uri.parse('http://127.0.0.1:8096/'),
web: false,
);
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await tester.pumpWidget(materialApp(session, server));
expect(find.text('Server endpoint not configured'), findsOneWidget);
expect(find.textContaining('PIPELINES_SERVER_URL'), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
expect(channel.outgoing, isEmpty);
});
testWidgets('gate shows connecting, then mounts the normal runs screen', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
final starting = session.start();
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
expect(find.text('Connecting to Pipelines…'), findsOneWidget);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
channel.readyCompleter.complete();
await starting;
await tester.pump();
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
final list = listRequest(channel)!;
channel.incoming.add(
jsonEncode({
'id': list['id'],
'type': 'runs',
'runs': [
{'id': 5, 'name': 'Live fixture run', 'status': 'succeeded'},
],
}),
);
await tester.pump();
expect(find.text('Live fixture run'), findsOneWidget);
});
testWidgets('a refused socket shows a friendly reason, never the exception', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(
channelFactory: (_) => channel,
backoffBase: const Duration(hours: 1),
backoffMax: const Duration(hours: 1),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
final starting = session.start();
channel.readyCompleter.completeError(
StateError('WebSocketChannelException: Failed to connect'),
);
await starting;
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
expect(find.text('Pipelines server unavailable'), findsOneWidget);
expect(find.textContaining('Retrying automatically'), findsOneWidget);
expect(find.textContaining('WebSocketChannelException'), findsNothing);
expect(find.widgetWithText(FilledButton, 'Retry'), findsOneWidget);
expect(find.byType(RunsScreen), findsNothing);
// The client keeps retrying by design; end the retry loop explicitly so
// the test does not leave a wall-clock timer behind.
session.dispose();
});
testWidgets(
'a drop keeps the list and reports reconnecting, not unavailable',
(tester) async {
final first = FakeChannel()..readyCompleter.complete();
final second = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) => sockets++ == 0 ? first : second,
backoffBase: const Duration(milliseconds: 200),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await session.start();
await tester.pumpWidget(materialApp(session, server));
await tester.pump();
await tester.pump();
first.incoming.add(
jsonEncode({
'id': listRequest(first)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Before reconnect', 'status': 'running'},
],
}),
);
await tester.pump();
expect(find.text('Before reconnect'), findsOneWidget);
first.incoming.addError(StateError('network drop'));
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
expect(find.text('Reconnecting to Pipelines…'), findsOneWidget);
expect(find.text('Pipelines server unavailable'), findsNothing);
expect(find.text('Before reconnect'), findsOneWidget);
for (
var attempt = 0;
attempt < 40 && listRequest(second) == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 50));
}
expect(listRequest(second), isNotNull);
expect(find.text('Reconnecting to Pipelines…'), findsNothing);
second.incoming.add(
jsonEncode({
'id': listRequest(second)!['id'],
'type': 'runs',
'runs': [
{'id': 2, 'name': 'After reconnect', 'status': 'succeeded'},
],
}),
);
await tester.pump();
expect(find.text('After reconnect'), findsOneWidget);
expect(find.text('Before reconnect'), findsNothing);
},
);
testWidgets(
'reconnect keeps the open detail instead of resetting to the list',
(tester) async {
final first = FakeChannel()..readyCompleter.complete();
final second = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
channelFactory: (_) => sockets++ == 0 ? first : second,
backoffBase: const Duration(milliseconds: 10),
);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
await session.start();
await tester.pumpWidget(materialApp(session, server, initialRunId: 1));
await tester.pump();
await tester.pump();
first.incoming.add(
jsonEncode({
'id': listRequest(first)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Open run', 'status': 'running'},
],
}),
);
await tester.pump();
final firstDetail = detailRequests(first, 1).single;
first.incoming.add(
jsonEncode({
'id': firstDetail['id'],
'type': 'run',
'run_detail': {'id': 1, 'title': 'Open run', 'status': 'running'},
}),
);
await tester.pump();
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
first.incoming.addError(StateError('network drop'));
await tester.pump();
expect(find.byType(RunsScreen), findsOneWidget);
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Reconnecting to Pipelines…'), findsOneWidget);
for (
var attempt = 0;
attempt < 60 && listRequest(second) == null;
attempt++
) {
await tester.pump(const Duration(milliseconds: 10));
}
expect(listRequest(second), isNotNull);
second.incoming.add(
jsonEncode({
'id': listRequest(second)!['id'],
'type': 'runs',
'runs': [
{'id': 1, 'name': 'Open run', 'status': 'running'},
],
}),
);
for (
var attempt = 0;
attempt < 20 && detailRequests(second, 1).isEmpty;
attempt++
) {
await tester.pump(const Duration(milliseconds: 10));
}
final refreshed = detailRequests(second, 1).single;
expect(find.text('Open run'), findsOneWidget);
second.incoming.add(
jsonEncode({
'id': refreshed['id'],
'type': 'run',
'run_detail': {'id': 1, 'title': 'Open run', 'status': 'succeeded'},
}),
);
await tester.pump();
expect(find.text('Open run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
},
);
testWidgets('gate uses Cupertino progress without a Material ancestor', (
tester,
) async {
final channel = FakeChannel();
final client = PipelinesClient(channelFactory: (_) => channel);
final server = webServer();
final session = AppSession(endpoint: server.uri, client: client);
addTearDown(session.dispose);
session.start();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
child: RunsDestination(session: session, server: server),
),
),
);
await tester.pump();
expect(find.text('Connecting to Pipelines…'), findsOneWidget);
expect(find.byType(CupertinoActivityIndicator), findsOneWidget);
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byType(Material), findsNothing);
});
}