import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/templates/screen.dart'; import 'package:pipelines_app/features/templates/ui/template_detail.dart'; import 'package:pipelines_app/features/templates/ui/template_gallery.dart' show templateAvatarKey; import 'package:pipelines_app/ui/layout/app_list_row.dart'; import '../data/template_test_support.dart' show source; import 'template_gallery_test_support.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; void main() { testWidgets('loading shows the exact line, then the server catalogue', ( tester, ) async { final reply = Completer(); final gateway = GalleryGateway((type, fields) => reply.future); await pumpGallery(tester, gateway, settle: false); await tester.pump(); expect(find.byKey(templatesLoadingKey), findsOneWidget); expect(find.text('Loading templates…'), findsOneWidget); expect(gateway.calls.single.type, 'list_templates'); expect(gateway.calls.single.fields, isEmpty); reply.complete(templatesReply(galleryTemplates)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); expect(find.text('Release workflow'), findsOneWidget); // One subtitle line: the description, then the parsed stage count. expect(find.text('Ship a version · 2 stages'), findsOneWidget); expect(find.text('No display name · 2 stages'), findsOneWidget); expect(find.text('(unnamed template)'), findsOneWidget); expect(find.byKey(templatesLoadingKey), findsNothing); // The app's shared rows with an initial avatar; no chevrons or tiles. expect(find.byType(AppListRow), findsNWidgets(2)); expect(find.byKey(templateAvatarKey('release')), findsOneWidget); expect(find.byType(ListTile), findsNothing); expect(find.byIcon(Icons.chevron_right), findsNothing); }); testWidgets('an unknown stage count is left out of the row', (tester) async { final gateway = GalleryGateway((type, fields) async => templatesReply(galleryTemplates)) ..validationHandler = (type, fields) async { throw Exception('invalid definition'); }; await pumpGallery(tester, gateway); expect(find.textContaining('stage count'), findsNothing); expect(find.textContaining('stages'), findsNothing); expect(find.text('Ship a version'), findsOneWidget); expect(gateway.callsOf('validate'), hasLength(2)); expect( gateway.callsOf('validate').first.fields['source'], galleryTemplates.first['source'], ); }); testWidgets('an empty catalogue states it without inventing rows', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(const []), ); await pumpGallery(tester, gateway); expect(find.byKey(templatesEmptyKey), findsOneWidget); expect( find.text('No templates are available on this server.'), findsOneWidget, ); expect(find.byKey(templatesGalleryKey), findsNothing); }); testWidgets('a refusal offers Retry; a failed refresh keeps the rows', ( tester, ) async { var mode = 'refuse'; final gateway = GalleryGateway((type, fields) async { if (type != 'list_templates') throw StateError('unexpected $type'); if (mode == 'refuse') { throw Exception('catalogue unavailable'); } return templatesReply(galleryTemplates); }); await pumpGallery(tester, gateway); expect(find.byKey(templatesErrorKey), findsOneWidget); expect(find.text('catalogue unavailable'), findsOneWidget); expect(find.byKey(templatesGalleryKey), findsNothing); mode = 'ok'; await tester.tap(find.byKey(templatesRetryKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); expect(find.text('Release workflow'), findsOneWidget); mode = 'refuse'; await tester.tap(find.byKey(templatesRefreshKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); expect(find.text('Release workflow'), findsOneWidget); expect(find.text('catalogue unavailable'), findsOneWidget); expect(find.byKey(templatesErrorBannerKey), findsOneWidget); expect(find.byKey(templatesErrorKey), findsNothing); mode = 'ok'; await tester.tap(find.byKey(templatesBannerRetryKey)); await tester.pumpAndSettle(); expect(find.text('catalogue unavailable'), findsNothing); expect(find.byKey(templatesErrorBannerKey), findsNothing); expect(find.text('Release workflow'), findsOneWidget); }); testWidgets('refresh re-reads the catalogue and keeps the selection', ( tester, ) async { var calls = 0; final gateway = GalleryGateway((type, fields) async { calls++; return templatesReply( calls == 1 ? galleryTemplates : [ ...galleryTemplates, templateJson(key: 'new', name: 'New template'), ], ); }); await pumpGallery(tester, gateway); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); await tester.tap(find.byKey(templatesRefreshKey)); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); expect(find.text('Release workflow'), findsOneWidget); expect(gateway.callsOf('list_templates'), hasLength(2)); await tester.tap(find.byKey(templatesBackKey)); await tester.pumpAndSettle(); expect(find.text('New template'), findsOneWidget); }); testWidgets('selecting a template shows metadata and verbatim source only', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(galleryTemplates), ); await pumpGallery(tester, gateway); expect(gateway.callsOf('list_templates'), hasLength(1)); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); // The bar's title pill names the template; the body does not repeat it. final bar = find.byType(SegmentedBar); expect( find.descendant(of: bar, matching: find.text('Release workflow')), findsOneWidget, ); expect( find.descendant(of: bar, matching: find.text('Ship a version')), findsOneWidget, ); expect(find.text('Release workflow'), findsOneWidget); expect(find.text('Ship a version'), findsOneWidget); expect(find.text('Key'), findsNothing); expect(find.byKey(templateKeyKey), findsOneWidget); expect(find.text('Includes a schedule'), findsOneWidget); expect(find.text(templateSourceLabel), findsOneWidget); expect(selectableSource(source), findsOneWidget); expect(selectableSource('release'), findsOneWidget); expect(find.text(templateCreateActionLabel), findsOneWidget); expect(gateway.callsOf('validate'), hasLength(2)); expect( gateway.callsOf('validate').map((call) => call.fields['source']), everyElement(source), ); expect(gateway.callsOf('submit'), isEmpty); await tester.tap(find.byKey(templatesBackKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); expect(find.byKey(templateDetailKey), findsNothing); }); testWidgets('system Back is not intercepted by the open template', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(galleryTemplates), ); await pumpGallery(tester, gateway); await tester.tap(find.byKey(templateRowKey('blank'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); expect(selectableSource('blank'), findsOneWidget); expect(find.byType(PopScope), findsNothing); // Nothing is pushed, so Back is the platform's; the screen keeps no // Back behavior of its own. expect(await tester.binding.handlePopRoute(), isFalse); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); }); testWidgets('embedded use drops the app bar and keeps native detail back', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(galleryTemplates), ); await pumpGallery(tester, gateway, embedded: true); expect(find.byType(SegmentedBar), findsNothing); expect(find.byKey(templatesRefreshKey), findsOneWidget); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); await tester.tap(find.byKey(templateDetailBackKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); }); testWidgets( 'a failed refresh with a template open is visible and retryable', (tester) async { var mode = 'ok'; final gateway = GalleryGateway((type, fields) async { if (type != 'list_templates') throw StateError('unexpected $type'); if (mode == 'refuse') throw Exception('refresh unavailable'); return templatesReply(galleryTemplates); }); await pumpGallery(tester, gateway); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); mode = 'refuse'; await tester.tap(find.byKey(templatesRefreshKey)); await tester.pumpAndSettle(); expect(gateway.callsOf('list_templates'), hasLength(2)); expect(find.byKey(templateDetailKey), findsOneWidget); expect(find.byKey(templatesErrorBannerKey), findsOneWidget); expect(find.text('refresh unavailable'), findsOneWidget); expect(find.byKey(templatesBannerRetryKey), findsOneWidget); expect(find.byKey(templatesErrorKey), findsNothing); expect(find.text(templateSourceLabel), findsOneWidget); expect(find.text(templateCreateActionLabel), findsOneWidget); expect( tester.widget(find.byKey(templatesBannerRetryKey)), isA(), ); expect(tester.takeException(), isNull); mode = 'ok'; await tester.tap(find.byKey(templatesBannerRetryKey)); await tester.pumpAndSettle(); expect(find.text('refresh unavailable'), findsNothing); expect(find.byKey(templatesErrorBannerKey), findsNothing); expect(find.byKey(templateDetailKey), findsOneWidget); expect(find.text(templateSourceLabel), findsOneWidget); expect(gateway.callsOf('list_templates'), hasLength(3)); await tester.tap(find.byKey(templatesBackKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesGalleryKey), findsOneWidget); expect(find.text('Release workflow'), findsOneWidget); expect(find.text('(unnamed template)'), findsOneWidget); }, ); testWidgets('an embedded failed refresh is visible above the open template', ( tester, ) async { var mode = 'ok'; final gateway = GalleryGateway((type, fields) async { if (type != 'list_templates') throw StateError('unexpected $type'); if (mode == 'refuse') throw Exception('refresh unavailable'); return templatesReply(galleryTemplates); }); await pumpGallery(tester, gateway, embedded: true); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(find.byKey(templateDetailBackKey), findsOneWidget); mode = 'refuse'; await tester.tap(find.byKey(templatesRefreshKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesErrorBannerKey), findsOneWidget); expect(find.text('refresh unavailable'), findsOneWidget); expect(find.byKey(templateDetailKey), findsOneWidget); expect(find.byKey(templateDetailBackKey), findsOneWidget); mode = 'ok'; await tester.tap(find.byKey(templatesBannerRetryKey)); await tester.pumpAndSettle(); expect(find.byKey(templatesErrorBannerKey), findsNothing); expect(find.byKey(templateDetailKey), findsOneWidget); expect(gateway.callsOf('list_templates'), hasLength(3)); }); testWidgets('2x text on a short phone has no overflow and stays reachable', ( tester, ) async { final gateway = GalleryGateway( (type, fields) async => templatesReply(galleryTemplates), ); await pumpGallery( tester, gateway, size: const Size(360, 640), textScale: 2, ); expect(tester.takeException(), isNull); await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.text(templateCreateActionLabel), findsOneWidget); expect(selectableSource(source), findsOneWidget); expect(find.byKey(templatesBackKey), findsOneWidget); }); testWidgets('only the newest reply wins and a disposed screen ignores one', ( tester, ) async { final first = Completer(); final stale = Completer(); var calls = 0; final gateway = GalleryGateway((type, fields) { calls++; if (calls == 1) return first.future; if (calls == 2) return stale.future; return Future.value( templatesReply([templateJson(key: 'fresh', name: 'Fresh')]), ); }); await pumpGallery(tester, gateway, settle: false); first.complete( templatesReply([templateJson(key: 'old', name: 'Old reply')]), ); await tester.pumpAndSettle(); expect(find.text('Old reply'), findsOneWidget); await tester.tap(find.byKey(templatesRefreshKey)); await tester.pump(); final replacement = GalleryGateway( (type, fields) async => templatesReply([templateJson(key: 'other', name: 'Other server')]), ); await pumpGallery(tester, replacement); expect(find.text('Other server'), findsOneWidget); stale.complete( templatesReply([templateJson(key: 'stale', name: 'Stale reply')]), ); await tester.pumpAndSettle(); expect(find.text('Stale reply'), findsNothing); expect(find.text('Other server'), findsOneWidget); final pending = Completer(); final disposedGateway = GalleryGateway((type, fields) => pending.future); await pumpGallery(tester, disposedGateway, settle: false); await tester.pumpWidget(const SizedBox.shrink()); pending.complete(templatesReply(galleryTemplates)); await tester.pump(); expect(tester.takeException(), isNull); }); }