Files
pipelines/app/test/features/templates/ui/template_gallery_screen_test.dart
T

405 lines
14 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
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 '../data/template_test_support.dart' show source;
import 'template_gallery_test_support.dart';
void main() {
testWidgets('loading shows the exact line, then the server catalogue', (
tester,
) async {
final reply = Completer<JsonMap>();
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);
expect(find.text('Ship a version'), findsOneWidget);
expect(find.text('2 stages'), findsNWidgets(2));
expect(find.text('(unnamed template)'), findsOneWidget);
expect(find.byKey(templatesLoadingKey), findsNothing);
});
testWidgets('stage count is unavailable when server parsing refuses', (
tester,
) async {
final gateway =
GalleryGateway((type, fields) async => templatesReply(galleryTemplates))
..validationHandler = (type, fields) async {
throw Exception('invalid definition');
};
await pumpGallery(tester, gateway);
expect(find.text('stage count unavailable'), findsNWidgets(2));
expect(find.text('0 stages'), findsNothing);
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);
expect(find.text('Release workflow'), findsOneWidget);
expect(find.text('Ship a version'), findsOneWidget);
expect(find.text('Key'), 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('native back returns to the gallery', (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);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.byKey(templatesGalleryKey), findsOneWidget);
expect(find.byKey(templateDetailKey), findsNothing);
});
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(AppBar), 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('Cupertino host uses its own list, back and detail controls', (
tester,
) async {
final gateway = GalleryGateway(
(type, fields) async => templatesReply(galleryTemplates),
);
await pumpGallery(tester, gateway, cupertino: true);
expect(find.byType(CupertinoListTile), findsNWidgets(2));
expect(find.byType(Scaffold), findsNothing);
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pumpAndSettle();
expect(find.byKey(templateDetailKey), findsOneWidget);
expect(selectableSource(source), findsOneWidget);
expect(
tester
.widget<CupertinoButton>(find.byKey(templateCreateActionKey))
.onPressed,
isNotNull,
);
expect(find.byType(BackButton), findsNothing);
await tester.tap(find.byKey(templatesBackKey));
await tester.pumpAndSettle();
expect(find.byType(CupertinoListTile), findsNWidgets(2));
});
testWidgets(
'a failed refresh with a template open is visible and retryable',
(tester) async {
for (final cupertino in [false, true]) {
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, cupertino: cupertino);
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)),
cupertino ? isA<CupertinoButton>() : isA<TextButton>(),
);
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<JsonMap>();
final stale = Completer<JsonMap>();
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<JsonMap>();
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);
});
}