feat(app): add native template gallery with detail and create

This commit is contained in:
2026-10-21 22:03:55 +01:00
parent 76fb2cdd87
commit c56b5d45a4
11 changed files with 2070 additions and 0 deletions
@@ -0,0 +1,104 @@
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_create_wording.dart';
import '../data/template_test_support.dart' show source;
import 'template_gallery_test_support.dart';
Future<void> openRelease(WidgetTester tester) async {
await tester.tap(find.byKey(templateRowKey('release')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(templateCreateActionKey));
await tester.pumpAndSettle();
}
void main() {
testWidgets(
'Cancel sends nothing; Create validates and submits the verbatim source '
'once through the existing prompt',
(tester) async {
final gateway = GalleryGateway((type, fields) async {
if (type == 'list_templates') return templatesReply(galleryTemplates);
return acceptedCreate(type, fields);
});
final created = <JsonMap>[];
await pumpGallery(tester, gateway, onCreated: created.add);
await openRelease(tester);
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('Create from Release workflow'), findsOneWidget);
await tester.enterText(find.byKey(templateCreateNameKey), 'Café');
await tester.pump();
await tester.tap(find.byKey(templateCreateCancelKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(gateway.callsOf('validate'), isEmpty);
expect(gateway.callsOf('submit'), isEmpty);
expect(created, isEmpty);
await tester.tap(find.byKey(templateCreateActionKey));
await tester.pumpAndSettle();
expect(find.byKey(templateCreateNameKey), findsOneWidget);
await tester.enterText(find.byKey(templateCreateNameKey), ' Café ');
await tester.pump();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pumpAndSettle();
expect(gateway.callsOf('validate').single.fields, {'source': source});
expect(gateway.callsOf('submit').single.fields, {
'name': 'Café',
'source': source,
'create_only': true,
});
expect(gateway.callsOf('submit').single.fields.keys, [
'name',
'source',
'create_only',
]);
expect(created, [
{'id': 7, 'name': 'Created release'},
]);
expect(
find.text('Created "Created release". It is in the Pipelines list.'),
findsOneWidget,
);
expect(find.text('Close'), findsOneWidget);
await tester.tap(find.byKey(templateCreateCancelKey));
await tester.pumpAndSettle();
expect(find.byKey(templateDetailKey), findsOneWidget);
expect(created, hasLength(1));
expect(gateway.callsOf('submit'), hasLength(1));
},
);
testWidgets('the callback is optional and Cupertino uses its native prompt', (
tester,
) async {
final gateway = GalleryGateway((type, fields) async {
if (type == 'list_templates') return templatesReply(galleryTemplates);
return acceptedCreate(type, fields);
});
await pumpGallery(tester, gateway, cupertino: true);
await openRelease(tester);
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await tester.enterText(find.byKey(templateCreateNameKey), 'Cupertino');
await tester.pump();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pumpAndSettle();
expect(gateway.callsOf('validate'), hasLength(1));
expect(gateway.callsOf('submit'), hasLength(1));
expect(
find.text('Created "Created release". It is in the Pipelines list.'),
findsOneWidget,
);
expect(find.byKey(templateCreateCancelKey), findsOneWidget);
});
}
@@ -0,0 +1,379 @@
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('(unnamed template)'), findsOneWidget);
expect(find.byKey(templatesLoadingKey), findsNothing);
});
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'), isEmpty);
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);
});
}
@@ -0,0 +1,112 @@
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 '../data/template_test_support.dart' show source;
typedef GalleryHandler =
FutureOr<JsonMap> Function(String type, JsonMap fields);
/// A recording gateway whose replies come from [handler].
class GalleryGateway implements ApiGateway {
GalleryGateway(this.handler);
final GalleryHandler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
List<({String type, JsonMap fields})> callsOf(String type) =>
calls.where((call) => call.type == type).toList(growable: false);
}
JsonMap templateJson({
required String key,
String name = '',
String description = '',
String? definition,
bool scheduled = false,
}) => {
'key': key,
'name': name,
'description': description,
'source': definition ?? source,
'scheduled': scheduled,
};
JsonMap templatesReply(List<JsonMap> templates) => {'templates': templates};
final galleryTemplates = [
templateJson(
key: 'release',
name: 'Release workflow',
description: 'Ship a version',
scheduled: true,
),
templateJson(key: 'blank', description: 'No display name'),
];
Future<JsonMap> acceptedCreate(String type, JsonMap fields) async {
if (type == 'validate') return {'graph': {}};
if (type == 'submit') {
return {
'detail': {'id': 7, 'name': 'Created release'},
};
}
throw StateError('Unexpected request $type');
}
/// Pumps the gallery in a real Material or Cupertino host without fixed sizes.
Future<void> pumpGallery(
WidgetTester tester,
GalleryGateway gateway, {
bool cupertino = false,
bool embedded = false,
ValueChanged<JsonMap>? onCreated,
double textScale = 1,
Size size = const Size(800, 1000),
bool settle = true,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
);
final screen = TemplatesScreen(
gateway: gateway,
embedded: embedded,
onCreated: onCreated,
);
final home = embedded
? (cupertino
? CupertinoPageScaffold(child: screen)
: Scaffold(body: screen))
: screen;
await tester.pumpWidget(
cupertino
? CupertinoApp(builder: media, home: home)
: MaterialApp(builder: media, home: home),
);
if (settle) await tester.pumpAndSettle();
}
Finder selectableSource(String text) => find.byWidgetPredicate(
(widget) => widget is SelectableText && widget.data == text,
);