import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/application/pipelines_app.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/pipelines/ui/run_pipeline_wording.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; import 'pipelines_app_test.dart' show webServer; import 'pipelines_replies.dart'; List> requestsOf(FakeChannel channel, String type) => channel.outgoing .map((frame) => jsonDecode(frame as String) as Map) .where((frame) => frame['type'] == type) .toList(); Future>> waitForCount( WidgetTester tester, FakeChannel channel, String type, int count, ) async { for ( var attempt = 0; attempt < 150 && requestsOf(channel, type).length < count; attempt++ ) { await tester.pump(const Duration(milliseconds: 10)); } final requests = requestsOf(channel, type); expect( requests.length, greaterThanOrEqualTo(count), reason: 'Timed out waiting for $type #$count', ); return requests; } Future reply( WidgetTester tester, FakeChannel channel, String type, int count, Map body, ) async { final requests = await waitForCount(tester, channel, type, count); channel.incoming.add(jsonEncode({'id': requests[count - 1]['id'], ...body})); await tester.pump(); await tester.pump(); } void main() { // The Run dialog reads the device's pending launch store before it opens. setUp(() => SharedPreferences.setMockInitialValues({})); testWidgets('an accepted launch opens the returned run and re-reads the ' 'runs list exactly once', (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 channel = FakeChannel()..readyCompleter.complete(); var sockets = 0; final client = PipelinesClient( channelFactory: (_) { sockets++; return channel; }, ); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')), ); addTearDown(provider.dispose); await tester.pumpWidget( PipelinesApp( server: webServer(), client: client, cupertino: false, routeInformationProvider: provider, ), ); await tester.pump(); await replyPipelines(tester, channel, fixture); // The pipeline browser's own last-run join read is the boot list read. expect(requestsOf(channel, 'list_runs'), hasLength(1)); await tester.tap(find.byKey(const Key('pipeline-row-1'))); await tester.pump(); await replyPipelineDetail(tester, channel, fixture); await reply(tester, channel, 'list_schedules', 1, { 'type': 'schedules', 'schedules': [], }); await tester.tap(find.byKey(runPipelineActionKey)); await tester.pumpAndSettle(); // The dialog asks for recent directories; answer it so the transport is // free for the launch request this test asserts. await waitForRequest(tester, channel, 'chat_directories'); final directories = pipelineRequest(channel, 'chat_directories')!; channel.incoming.add( jsonEncode({ 'id': directories['id'], 'type': 'chat_directories', 'chat_directories': [], }), ); await tester.pump(); await tester.enterText( find.byKey(runPipelinePromptKey), 'Review the change.', ); await tester.enterText(find.byKey(runPipelineWorkspaceKey), '/work/alpha'); await tester.tap(find.byKey(runPipelineConfirmKey)); await tester.pump(); final launches = await waitForCount(tester, channel, 'run_pipeline', 1); final launch = launches.single; expect(launch['pipeline_id'], 1); expect(launch['version'], 2); expect(launch['workspace'], '/work/alpha'); expect(launch['input'], {'prompt': 'Review the change.'}); expect(launch.containsKey('notify_mode'), isFalse); channel.incoming.add( jsonEncode({ 'id': launch['id'], 'type': 'run_started', 'run_detail': { 'id': 77, 'pipeline_id': 1, 'title': 'Started from the browser', 'status': 'running', }, }), ); await tester.pump(); await tester.pump(); expect(provider.value.uri.path, '/runs/77'); // The Runs destination mounted after the accepted launch and performed the // one fresh list read; no row came from the run reply. final lists = await waitForCount(tester, channel, 'list_runs', 2); expect(lists, hasLength(2)); channel.incoming.add( jsonEncode({ 'id': lists[1]['id'], 'type': 'runs', 'runs': [ ...fixture.runs, { 'id': 77, 'pipeline_id': 1, 'title': 'Started from the browser', 'status': 'running', }, ], }), ); final detail = await waitForCount(tester, channel, 'get_run', 1); expect(detail.single['run_id'], 77); channel.incoming.add( jsonEncode({ 'id': detail.single['id'], 'type': 'run', 'run_detail': { 'id': 77, 'pipeline_id': 1, 'title': 'Started from the browser', 'status': 'running', }, }), ); await tester.pump(); await tester.pumpAndSettle(); expect(find.text('Started from the browser'), findsWidgets); expect(provider.value.uri.path, '/runs/77'); expect(requestsOf(channel, 'list_runs'), hasLength(2)); expect(requestsOf(channel, 'run_pipeline'), hasLength(1)); expect(sockets, 1); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); }