diff --git a/app/lib/features/templates/screen.dart b/app/lib/features/templates/screen.dart index a23bad26..f68cd642 100644 --- a/app/lib/features/templates/screen.dart +++ b/app/lib/features/templates/screen.dart @@ -67,6 +67,7 @@ class TemplatesScreen extends StatefulWidget { class _TemplatesScreenState extends State { late TemplateRepository _repository = TemplateRepository(widget.gateway); List _templates = const []; + Map _stageCounts = const {}; bool _loading = true; bool _loaded = false; String? _error; @@ -91,6 +92,7 @@ class _TemplatesScreenState extends State { // selection while the replacement is read. _repository = TemplateRepository(widget.gateway); _templates = const []; + _stageCounts = const {}; _loaded = false; _selectedKey = null; unawaited(_load()); @@ -107,8 +109,22 @@ class _TemplatesScreenState extends State { try { final templates = await _repository.listTemplates(); if (!_owns(generation)) return; + final stageCounts = {}; + for (final entry in templates) { + try { + final graph = await _repository.validate(entry.source); + final stages = graph['stages']; + stageCounts[entry.key] = stages is List ? stages.length : null; + } catch (_) { + // A refusal means the count is unknown, not that the template has + // zero stages. The catalogue remains usable, as it does on the web. + stageCounts[entry.key] = null; + } + if (!_owns(generation)) return; + } setState(() { _templates = templates; + _stageCounts = stageCounts; _loaded = true; _loading = false; final selected = _selectedKey; @@ -164,6 +180,7 @@ class _TemplatesScreenState extends State { final body = selected == null ? TemplateGallery( templates: _templates, + stageCounts: _stageCounts, loaded: _loaded, loading: _loading, error: _error, diff --git a/app/lib/features/templates/ui/template_gallery.dart b/app/lib/features/templates/ui/template_gallery.dart index 809b0496..74e335af 100644 --- a/app/lib/features/templates/ui/template_gallery.dart +++ b/app/lib/features/templates/ui/template_gallery.dart @@ -24,6 +24,7 @@ class TemplateGallery extends StatelessWidget { const TemplateGallery({ super.key, required this.templates, + required this.stageCounts, required this.loaded, required this.loading, required this.error, @@ -32,6 +33,9 @@ class TemplateGallery extends StatelessWidget { }); final List templates; + + /// Parsed counts; a present null value means the source could not be parsed. + final Map stageCounts; final bool loaded; final bool loading; final String? error; @@ -98,9 +102,7 @@ class TemplateGallery extends StatelessWidget { return ListTile( key: templateRowKey(entry.key), title: Text(templateGalleryName(entry)), - subtitle: entry.description.trim().isEmpty - ? null - : Text(entry.description), + subtitle: _materialSubtitle(entry), trailing: const Icon(Icons.chevron_right), onTap: () => onOpen(entry.key), ); @@ -118,9 +120,7 @@ class TemplateGallery extends StatelessWidget { CupertinoListTile( key: templateRowKey(entry.key), title: _cupertinoText(templateGalleryName(entry)), - subtitle: entry.description.trim().isEmpty - ? null - : _cupertinoText(entry.description), + subtitle: _cupertinoSubtitle(entry), trailing: const CupertinoListTileChevron(), onTap: () => onOpen(entry.key), ), @@ -135,4 +135,38 @@ class TemplateGallery extends StatelessWidget { /// with the content and nothing else about the tile is restyled. Widget _cupertinoText(String text) => DefaultTextStyle.merge(maxLines: 3, child: Text(text)); + + Widget _materialSubtitle(TemplateEntry entry) { + final description = entry.description.trim().isEmpty + ? null + : Text(entry.description); + final count = Text(_stageCountLabel(entry.key)); + if (description == null) return count; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [description, count], + ); + } + + Widget _cupertinoSubtitle(TemplateEntry entry) { + final description = entry.description.trim().isEmpty + ? null + : _cupertinoText(entry.description); + final count = _cupertinoText(_stageCountLabel(entry.key)); + if (description == null) return count; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [description, count], + ); + } + + String _stageCountLabel(String key) { + if (!stageCounts.containsKey(key) || stageCounts[key] == null) { + return 'stage count unavailable'; + } + final count = stageCounts[key]!; + return '$count ${count == 1 ? 'stage' : 'stages'}'; + } } diff --git a/app/test/application/templates_wiring_test.dart b/app/test/application/templates_wiring_test.dart index f194c4ca..4217eac1 100644 --- a/app/test/application/templates_wiring_test.dart +++ b/app/test/application/templates_wiring_test.dart @@ -128,6 +128,10 @@ Future openTemplates( }, ], }); + await reply(tester, channel, 'validate', listRead, { + 'type': 'graph', + 'graph': {'stages': []}, + }); } void main() { @@ -279,7 +283,7 @@ void main() { await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pump(); - await reply(tester, channel, 'validate', 1, { + await reply(tester, channel, 'validate', 2, { 'type': 'graph', 'graph': {'stages': []}, }); @@ -334,7 +338,8 @@ void main() { // again adds no request. expect(requestsOf(channel, 'list_pipelines'), hasLength(2)); expect(requestsOf(channel, 'list_runs'), hasLength(2)); - expect(requestsOf(channel, 'validate'), hasLength(1)); + // 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); diff --git a/app/test/features/templates/ui/template_gallery_create_test.dart b/app/test/features/templates/ui/template_gallery_create_test.dart index 3308d4fb..1dfb048a 100644 --- a/app/test/features/templates/ui/template_gallery_create_test.dart +++ b/app/test/features/templates/ui/template_gallery_create_test.dart @@ -36,7 +36,7 @@ void main() { await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); - expect(gateway.callsOf('validate'), isEmpty); + expect(gateway.callsOf('validate'), hasLength(2)); expect(gateway.callsOf('submit'), isEmpty); expect(created, isEmpty); @@ -48,7 +48,8 @@ void main() { await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); - expect(gateway.callsOf('validate').single.fields, {'source': source}); + expect(gateway.callsOf('validate'), hasLength(3)); + expect(gateway.callsOf('validate').last.fields, {'source': source}); expect(gateway.callsOf('submit').single.fields, { 'name': 'Café', 'source': source, @@ -93,7 +94,7 @@ void main() { await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); - expect(gateway.callsOf('validate'), hasLength(1)); + expect(gateway.callsOf('validate'), hasLength(3)); expect(gateway.callsOf('submit'), hasLength(1)); expect( find.text('Created "Created release". It is in the Pipelines list.'), diff --git a/app/test/features/templates/ui/template_gallery_screen_test.dart b/app/test/features/templates/ui/template_gallery_screen_test.dart index ca4d93dc..2316d18f 100644 --- a/app/test/features/templates/ui/template_gallery_screen_test.dart +++ b/app/test/features/templates/ui/template_gallery_screen_test.dart @@ -31,10 +31,31 @@ void main() { 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 { @@ -148,7 +169,11 @@ void main() { expect(selectableSource(source), findsOneWidget); expect(selectableSource('release'), findsOneWidget); expect(find.text(templateCreateActionLabel), findsOneWidget); - expect(gateway.callsOf('validate'), isEmpty); + 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)); diff --git a/app/test/features/templates/ui/template_gallery_test_support.dart b/app/test/features/templates/ui/template_gallery_test_support.dart index 9c94f9c4..80e80768 100644 --- a/app/test/features/templates/ui/template_gallery_test_support.dart +++ b/app/test/features/templates/ui/template_gallery_test_support.dart @@ -17,11 +17,22 @@ class GalleryGateway implements ApiGateway { final GalleryHandler handler; final List<({String type, JsonMap fields})> calls = []; + GalleryHandler? validationHandler; @override Future call(String type, [JsonMap fields = const {}]) async { final copy = Map.from(fields); calls.add((type: type, fields: copy)); + if (type == 'validate' && validationHandler != null) { + return validationHandler!(type, copy); + } + if (type == 'validate') { + return { + 'graph': { + 'stages': [{}, {}], + }, + }; + } return handler(type, copy); } diff --git a/app/tool/templates_gallery_probe.dart b/app/tool/templates_gallery_probe.dart index c42d26af..5cde213b 100644 --- a/app/tool/templates_gallery_probe.dart +++ b/app/tool/templates_gallery_probe.dart @@ -100,7 +100,11 @@ class _ProbeGateway implements ApiGateway { publish(); await Future.delayed(const Duration(milliseconds: 200)); return { - 'graph': {'stages': const []}, + 'graph': { + 'stages': const [ + {'key': 'release'}, + ], + }, }; } if (type == 'submit') { diff --git a/app/tool/templates_gallery_probe.mjs b/app/tool/templates_gallery_probe.mjs index 6ad5bac1..8d1ee8f8 100644 --- a/app/tool/templates_gallery_probe.mjs +++ b/app/tool/templates_gallery_probe.mjs @@ -241,7 +241,7 @@ try { await clickLabel('Release workflow', { contains: true }); await assertBody('Definition source'); const before = await state(); - if (before?.listCalls !== 1 || before?.validates !== 0 || before?.submits !== 0) { + if (before?.listCalls !== 1 || before?.validates !== 2 || before?.submits !== 0) { throw new Error(`Opening the template made unexpected requests: ${JSON.stringify(before)}`); } @@ -251,7 +251,7 @@ try { await assertBody('Definition source'); await assertBody('Create pipeline from this template'); const refused = await state(); - if (refused?.listCalls !== 2 || refused?.validates !== 0 || refused?.submits !== 0) { + if (refused?.listCalls !== 2 || refused?.validates !== 2 || refused?.submits !== 0) { throw new Error(`The failed refresh did not stay read-only: ${JSON.stringify(refused)}`); } captures.push(await capture(`${capturePrefix}-detail-refresh-error`)); @@ -286,6 +286,7 @@ try { await assertBody('Release workflow'); await assertBody('(unnamed template)'); await assertBody('Ship a version'); + await assertBody('1 stage'); const gallery = await state(); if (gallery?.listCalls !== 2) { throw new Error(`Retry did not re-read exactly once: ${JSON.stringify(gallery)}`); @@ -299,7 +300,7 @@ try { await assertBody('Includes a schedule'); await assertBody('Create pipeline from this template'); const detail = await state(); - if (detail?.validates !== 0 || detail?.submits !== 0) { + if (detail?.validates !== 2 || detail?.submits !== 0) { throw new Error(`Opening a template sent a write: ${JSON.stringify(detail)}`); } captures.push(await capture(`${capturePrefix}-detail`)); @@ -310,7 +311,7 @@ try { await clickLabel('Refresh'); await assertBody('Added after refresh'); const refreshed = await state(); - if (refreshed?.listCalls !== 3) { + if (refreshed?.listCalls !== 3 || refreshed?.validates !== 5) { throw new Error(`Refresh made unexpected requests: ${JSON.stringify(refreshed)}`); } captures.push(await capture(`${capturePrefix}-refreshed`)); @@ -326,7 +327,7 @@ try { await clickLabel('Cancel'); await waitFor(async () => !(await semanticText()).includes('Create from Release workflow'), 'the prompt to close'); const cancelled = await state(); - if (cancelled?.validates !== 0 || cancelled?.submits !== 0 || cancelled?.created?.length !== 0) { + if (cancelled?.validates !== 5 || cancelled?.submits !== 0 || cancelled?.created?.length !== 0) { throw new Error(`Cancel sent a request: ${JSON.stringify(cancelled)}`); } checks.push({ check: 'Cancel sent nothing', state: cancelled }); @@ -341,7 +342,7 @@ try { await clickLabel('Create'); await assertBody('Created "Probe created". It is in the Pipelines list.'); const created = await state(); - if (created?.validates !== 1 || created?.submits !== 1 || created?.created?.length !== 1) { + if (created?.validates !== 6 || created?.submits !== 1 || created?.created?.length !== 1) { throw new Error(`The accepted create was not reported exactly once: ${JSON.stringify(created)}`); } if (created.lastSubmit?.name !== 'Probe release' || created.lastSubmit?.create_only !== true) {