import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; Map? pipelineRequest(FakeChannel channel, String type) => channel.outgoing .map((frame) => jsonDecode(frame as String) as Map) .where((frame) => frame['type'] == type) .firstOrNull; Future waitForRequest( WidgetTester tester, FakeChannel channel, String type, ) async { for ( var attempt = 0; attempt < 60 && pipelineRequest(channel, type) == null; attempt++ ) { await tester.pump(const Duration(milliseconds: 10)); } expect( pipelineRequest(channel, type), isNotNull, reason: 'Timed out waiting for $type', ); } /// Waits for a later request of [type] than [afterId]. The transport serializes /// requests, so a destination that mounts later issues its read only after the /// previous reply arrives. Future> waitForLaterRequest( WidgetTester tester, FakeChannel channel, String type, String afterId, ) async { Map? later() => channel.outgoing .map((frame) => jsonDecode(frame as String) as Map) .where((frame) => frame['type'] == type && frame['id'] != afterId) .firstOrNull; for (var attempt = 0; attempt < 60 && later() == null; attempt++) { await tester.pump(const Duration(milliseconds: 10)); } expect(later(), isNotNull, reason: 'Timed out waiting for later $type'); return later()!; } Future replyPipelines( WidgetTester tester, FakeChannel channel, PipelineFixture fixture, ) async { await waitForRequest(tester, channel, 'list_pipelines'); final pipelines = pipelineRequest(channel, 'list_pipelines')!; channel.incoming.add( jsonEncode({ 'id': pipelines['id'], 'type': 'pipelines', 'pipelines': fixture.summaries, }), ); await tester.pump(); await waitForRequest(tester, channel, 'list_runs'); final runs = pipelineRequest(channel, 'list_runs')!; channel.incoming.add( jsonEncode({'id': runs['id'], 'type': 'runs', 'runs': fixture.runs}), ); await tester.pump(); await tester.pump(); } Future replyPipelineDetail( WidgetTester tester, FakeChannel channel, PipelineFixture fixture, ) async { await waitForRequest(tester, channel, 'get_pipeline'); final request = pipelineRequest(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(); await tester.pump(); }