import 'dart:convert'; 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/pipelines_destination.dart'; import 'package:pipelines_app/application/server_endpoint.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/pipelines/screen.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; ServerEndpoint webServer() => ServerEndpoint.resolve( base: Uri.parse('http://127.0.0.1:8096/'), web: true, ); Map? requestOf(FakeChannel channel, String type) => channel .outgoing .map((frame) => jsonDecode(frame as String) as Map) .where((frame) => frame['type'] == type) .firstOrNull; Widget materialApp(AppSession session, ServerEndpoint server) => MaterialApp( home: Scaffold( body: PipelinesDestination(session: session, server: server), ), ); Future waitFor( WidgetTester tester, bool Function() check, String label, ) async { for (var attempt = 0; attempt < 60 && !check(); attempt++) { await tester.pump(const Duration(milliseconds: 10)); } expect(check(), isTrue, reason: 'Timed out waiting for $label'); } Future replyList( WidgetTester tester, FakeChannel channel, PipelineFixture fixture, ) async { await waitFor( tester, () => requestOf(channel, 'list_pipelines') != null, 'list_pipelines', ); final pipelines = requestOf(channel, 'list_pipelines')!; channel.incoming.add( jsonEncode({ 'id': pipelines['id'], 'type': 'pipelines', 'pipelines': fixture.summaries, }), ); await tester.pump(); await waitFor( tester, () => requestOf(channel, 'list_runs') != null, 'list_runs', ); final runs = requestOf(channel, 'list_runs')!; channel.incoming.add( jsonEncode({'id': runs['id'], 'type': 'runs', 'runs': fixture.runs}), ); await tester.pump(); } Future replyDetail( WidgetTester tester, FakeChannel channel, PipelineFixture fixture, int id, ) async { await waitFor( tester, () => requestOf(channel, 'get_pipeline') != null, 'get_pipeline', ); final request = requestOf(channel, 'get_pipeline')!; final detail = fixture.detailFor( request['pipeline_id'] as int, request['version'] as int, )!; channel.incoming.add( jsonEncode({'id': request['id'], 'type': 'pipeline', 'detail': detail}), ); await tester.pump(); } void main() { testWidgets('missing native configuration never opens a socket', ( 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(PipelinesScreen), findsNothing); expect(channel.outgoing, isEmpty); }); testWidgets('the list is only requested after the first connection', ( tester, ) async { final fixture = PipelineFixture(); 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(PipelinesScreen), findsNothing); expect(requestOf(channel, 'list_pipelines'), isNull); expect(channel.outgoing, isEmpty); channel.readyCompleter.complete(); await starting; await tester.pump(); await replyList(tester, channel, fixture); await tester.pump(); expect(find.byType(PipelinesScreen), findsOneWidget); expect(find.text('Alpha review'), findsOneWidget); expect(requestOf(channel, 'list_pipelines'), isNotNull); }); testWidgets('an unavailable first connection is honest and retryable', ( 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('Failed to connect')); await starting; await tester.pumpWidget(materialApp(session, server)); await tester.pump(); expect(find.text('Pipelines server unavailable'), findsOneWidget); expect(find.textContaining('Failed to connect'), findsNothing); expect(find.widgetWithText(FilledButton, 'Retry'), findsOneWidget); expect(find.byType(PipelinesScreen), findsNothing); expect(channel.outgoing, isEmpty); // The client keeps retrying by design; end its backoff timer explicitly. session.dispose(); }); testWidgets('reconnect refreshes the list and the open detail in place', ( tester, ) async { tester.view.physicalSize = const Size(1100, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final fixture = PipelineFixture(); 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)); await tester.pump(); await replyList(tester, first, fixture); await tester.pump(); expect(find.text('Alpha review'), findsOneWidget); await tester.tap(find.byKey(const Key('pipeline-row-1'))); await tester.pump(); await replyDetail(tester, first, fixture, 1); await tester.pump(); expect(find.text('hash-alpha'), findsOneWidget); expect( tester.widget(find.byKey(const Key('pipeline-row-1'))).selected, isTrue, ); first.incoming.addError(StateError('network drop')); await tester.pump(); expect(find.byType(PipelinesScreen), findsOneWidget); expect(find.text('Reconnecting to Pipelines…'), findsOneWidget); expect(find.text('hash-alpha'), findsOneWidget); await waitFor( tester, () => requestOf(second, 'list_pipelines') != null, 'reconnected list request', ); await replyList(tester, second, fixture); await waitFor( tester, () => requestOf(second, 'get_pipeline') != null, 'reconnected detail request', ); await replyDetail(tester, second, fixture, 1); await tester.pump(); expect(find.text('Reconnecting to Pipelines…'), findsNothing); expect(find.text('hash-alpha'), findsOneWidget); expect( tester.widget(find.byKey(const Key('pipeline-row-1'))).selected, isTrue, reason: 'A reconnect must not remount the screen or drop the selection.', ); }); }