import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/pipelines_app.dart'; import 'package:pipelines_app/application/templates_destination.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/templates/screen.dart'; import 'package:pipelines_app/features/templates/ui/template_create_wording.dart'; import 'package:pipelines_app/features/templates/ui/template_detail.dart' show templateDetailBackKey; import 'package:shared_preferences/shared_preferences.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; import '../features/templates/ui/template_gallery_test_support.dart' show GalleryGateway, galleryTemplates, pumpGallery, templatesReply; import 'pipelines_app_test.dart' show webServer; import 'pipelines_replies.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.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 < 100 && 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(); } Future pumpApp( WidgetTester tester, { required FakeChannel channel, required PipelinesClient client, required PlatformRouteInformationProvider provider, }) async { tester.view.physicalSize = const Size(1100, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); SharedPreferences.setMockInitialValues({}); await tester.pumpWidget( PipelinesApp( server: webServer(), client: client, // This fixture tests template routing, not native update plugins. androidSupported: false, routeInformationProvider: provider, ), ); await tester.pump(); } /// Answers the reads a Templates page makes when it opens by route. Future readTemplates( WidgetTester tester, FakeChannel channel, { int listRead = 1, }) async { await tester.pump(const Duration(milliseconds: 600)); await reply(tester, channel, 'list_templates', listRead, { 'type': 'templates', 'templates': [ { 'key': 'release', 'name': 'Release workflow', 'description': 'Ship a version', 'source': 'name: "release"\n', 'scheduled': true, }, ], }); // The shell's own ping shares the request queue; let it go out before the // gallery's validate reads, as the list read is followed by the shell's ping. await tester.pumpAndSettle(); await reply(tester, channel, 'validate', listRead, { 'type': 'graph', 'graph': {'stages': []}, }); } void main() { testWidgets('non-embedded TemplatesScreen keeps its own native bar', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(galleryTemplates), ); await pumpGallery(tester, gateway); expect(find.byType(SegmentedBar), findsOneWidget); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pump(); expect(find.text('Definition source'), findsOneWidget); expect(find.byKey(templatesBackKey), findsOneWidget); expect(gateway.callsOf('list_templates'), hasLength(1)); }); testWidgets( 'a /templates link opens the embedded page and Back restores Settings', (tester) async { 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 pumpApp( tester, channel: channel, client: client, provider: provider, ); await replyPipelines(tester, channel, fixture); await provider.didPushRouteInformation( RouteInformation(uri: Uri.parse('/templates')), ); await readTemplates(tester, channel); await tester.pumpAndSettle(); final screen = tester.widget( find.byType(TemplatesScreen), ); expect(screen.embedded, isTrue); expect(find.text('Release workflow'), findsOneWidget); // One visible navigation bar: the Templates page owns it, the shell is // preserved underneath. expect(find.byType(SegmentedBar), findsOneWidget); expect(sockets, 1); expect(requestsOf(channel, 'list_templates'), hasLength(1)); // Refresh sits in the bar's actions pill, not in the body. Finder inBar(Key key) => find.descendant( of: find.byType(SegmentedBar), matching: find.byKey(key), ); expect(inBar(templatesRefreshKey), findsOneWidget); expect(find.byKey(templatesRefreshKey), findsOneWidget); expect(inBar(templatesPageBackKey), findsOneWidget); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pump(); await tester.pump(); expect(find.text('Definition source'), findsOneWidget); // One Back at a time, in the bar's round leading piece: the open // template's Back replaces the page's. expect(inBar(templateDetailBackKey), findsOneWidget); expect(find.byKey(templateDetailBackKey), findsOneWidget); expect(find.byKey(templatesPageBackKey), findsNothing); // Detail Back returns to the gallery, then the page Back closes the // page to Settings. await tester.tap(find.byKey(templateDetailBackKey)); await tester.pump(); // The gallery hands the bar back to the page's Back on the next frame. await tester.pump(); await tester.tap(find.byKey(templatesPageBackKey)); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/settings'); expect(find.byType(TemplatesScreen), findsNothing); expect(sockets, 1); // Browser Back from /templates follows the same close path. await provider.didPushRouteInformation( RouteInformation(uri: Uri.parse('/templates')), ); await readTemplates(tester, channel, listRead: 2); await tester.pumpAndSettle(); expect(find.byType(TemplatesScreen), findsOneWidget); await provider.didPushRouteInformation( RouteInformation(uri: Uri.parse('/settings')), ); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/settings'); expect(find.byType(TemplatesScreen), findsNothing); expect(requestsOf(channel, 'list_templates'), hasLength(2)); expect(sockets, 1); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }, ); testWidgets('an accepted create re-reads Pipelines exactly once', ( tester, ) async { 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 pumpApp(tester, channel: channel, client: client, provider: provider); await replyPipelines(tester, channel, fixture); await provider.didPushRouteInformation( RouteInformation(uri: Uri.parse('/templates')), ); expect( find.byKey(const Key('pipeline-row-41'), skipOffstage: false), findsNothing, ); await readTemplates(tester, channel); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pump(); await tester.tap(find.byKey(templateCreateActionKey)); await tester.pump(); await tester.enterText(find.byKey(templateCreateNameKey), 'Probe release'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pump(); await reply(tester, channel, 'validate', 2, { 'type': 'graph', 'graph': {'stages': []}, }); await reply(tester, channel, 'submit', 1, { 'type': 'pipeline', 'detail': {'id': 41, 'name': 'Probe release'}, }); expect( find.text('Created "Probe release". It is in the Pipelines list.'), findsOneWidget, ); // The detail itself never adds a row: the hidden list is still the old one // until the fresh server reply arrives. final refreshes = await waitForCount(tester, channel, 'list_pipelines', 2); expect( find.byKey(const Key('pipeline-row-41'), skipOffstage: false), findsNothing, ); channel.incoming.add( jsonEncode({ 'id': refreshes[1]['id'], 'type': 'pipelines', 'pipelines': [ ...fixture.summaries, pipelineSummary(id: 41, name: 'Probe release'), ], }), ); await tester.pump(); await reply(tester, channel, 'list_runs', 2, { 'type': 'runs', 'runs': fixture.runs, }); expect( find.byKey(const Key('pipeline-row-41'), skipOffstage: false), findsOneWidget, ); await tester.tap(find.byKey(templateCreateCancelKey)); await tester.pump(); await tester.tap(find.byKey(templateDetailBackKey)); await tester.pump(); // The gallery hands the bar back to the page's Back on the next frame. await tester.pump(); await tester.tap(find.byKey(templatesPageBackKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('shell.destination.pipelines'))); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/settings'); // A tab retap preserves Settings; explicit Back returns to Pipelines. await tester.tap(find.byKey(const Key('shell.back'))); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/pipelines'); expect(find.text('Probe release'), findsOneWidget); // Only the accepted detail's invalidation re-read the list; showing it // again adds no request. expect(requestsOf(channel, 'list_pipelines'), hasLength(2)); expect(requestsOf(channel, 'list_runs'), hasLength(2)); // One validation derives the gallery count; the other belongs to Create. expect(requestsOf(channel, 'validate'), hasLength(2)); expect(requestsOf(channel, 'submit'), hasLength(1)); expect(sockets, 1); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); }