diff --git a/app/lib/features/runs/ui/runs_list.dart b/app/lib/features/runs/ui/runs_list.dart index 63435f27..714d2d54 100644 --- a/app/lib/features/runs/ui/runs_list.dart +++ b/app/lib/features/runs/ui/runs_list.dart @@ -1,84 +1,247 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'runs_controller.dart'; + +import '../../../core/api_gateway.dart'; import '../domain/run_models.dart'; +import 'runs_controller.dart'; class RunsList extends StatelessWidget { - const RunsList({super.key, required this.controller, required this.onSelected}); + const RunsList({ + super.key, + required this.controller, + required this.onSelected, + }); + final RunsController controller; final ValueChanged onSelected; + + bool get _isCupertino => + defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS; + @override Widget build(BuildContext context) { if (controller.loading && controller.runs.isEmpty) { - return const Center(child: CircularProgressIndicator()); - } - if (controller.error != null && controller.runs.isEmpty) { return Center( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Text(controller.error!), - const SizedBox(height: 8), - FilledButton( - onPressed: controller.load, - child: const Text('Retry'), - ), - ], - ), + child: _isCupertino + ? const CupertinoActivityIndicator() + : const CircularProgressIndicator(), ); } + if (controller.error != null && controller.runs.isEmpty) { + return _emptyError(context); + } return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (controller.usage != null) - Padding(padding: const EdgeInsets.symmetric(horizontal: 16), child: Text('Usage: ${controller.usage}')), - if (controller.error != null) - MaterialBanner( - content: Text(controller.error!), - actions: [ - TextButton(onPressed: controller.load, child: const Text('Retry')), - ], + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Text('Usage: ${controller.usage}'), ), + if (controller.error != null) _inlineError(context), Padding( padding: const EdgeInsets.all(16), - child: TextField( - decoration: const InputDecoration( - labelText: 'Filter runs', - prefixIcon: Icon(Icons.search), - ), - onChanged: controller.setQuery, - ), + child: _isCupertino + ? CupertinoSearchTextField( + placeholder: 'Filter runs', + onChanged: controller.setQuery, + ) + : TextField( + decoration: const InputDecoration( + labelText: 'Filter runs', + prefixIcon: Icon(Icons.search), + ), + onChanged: controller.setQuery, + ), ), - Wrap(spacing: 8, children: [ - DropdownButton(value: controller.sort, onChanged: (v) { if (v != null) controller.setSort(v); }, items: const [DropdownMenuItem(value: 'newest_first', child: Text('Newest')), DropdownMenuItem(value: 'oldest_first', child: Text('Oldest')), DropdownMenuItem(value: 'pipeline_name', child: Text('Pipeline')), DropdownMenuItem(value: 'status', child: Text('Status'))]), - DropdownButton(value: controller.scheduled, onChanged: (v) { if (v != null) controller.setScheduled(v); }, items: const [DropdownMenuItem(value: 'any', child: Text('Any schedule')), DropdownMenuItem(value: 'scheduled', child: Text('Scheduled')), DropdownMenuItem(value: 'unscheduled', child: Text('Unscheduled'))]), - for (final status in ['queued', 'running', 'waiting', 'succeeded', 'failed']) - FilterChip(label: Text(status), selected: controller.statuses.contains(status), onSelected: (_) => controller.setStatus(status)), - ]), + _selectors(context), Expanded( child: controller.visibleRuns.isEmpty ? const Center(child: Text('No runs found.')) : ListView.builder( itemCount: controller.visibleRuns.length, - itemBuilder: (context, index) { - final run = controller.visibleRuns[index]; - final identity = runIdentity(run['title'], run['name']); - final created = - run['created_at']?.toString() ?? 'Time unavailable'; - final id = run['id']; - return ListTile( - title: Text( - identity.label.isEmpty ? 'Run $id' : identity.label, - ), - subtitle: Text( - '${run['name'] ?? 'Pipeline unavailable'} · $created${run['scheduled'] == true ? ' · Scheduled' : ''}', - ), - leading: Chip(label: Text(runStatusLabel(run))), - trailing: const Icon(Icons.chevron_right), - onTap: id is num ? () => onSelected(id) : null, - ); - }, + itemBuilder: (context, index) => + _runTile(context, controller.visibleRuns[index]), ), ), ], ); } + + Widget _emptyError(BuildContext context) => Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text(controller.error!), + const SizedBox(height: 8), + _isCupertino + ? CupertinoButton( + onPressed: controller.load, + child: const Text('Retry'), + ) + : FilledButton( + onPressed: controller.load, + child: const Text('Retry'), + ), + ], + ), + ); + + Widget _inlineError(BuildContext context) => Padding( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Row( + children: [ + Expanded(child: Text(controller.error!)), + if (_isCupertino) + CupertinoButton( + onPressed: controller.load, + child: const Text('Retry'), + ) + else + TextButton(onPressed: controller.load, child: const Text('Retry')), + ], + ), + ); + + Widget _selectors(BuildContext context) { + if (_isCupertino) { + return Wrap( + spacing: 8, + children: [ + CupertinoButton( + onPressed: () => _choose( + context, + 'Sort runs', + _sorts, + controller.sort, + controller.setSort, + ), + child: Text('Sort: ${_sortLabel(controller.sort)}'), + ), + CupertinoButton( + onPressed: () => _choose( + context, + 'Schedule', + _schedules, + controller.scheduled, + controller.setScheduled, + ), + child: Text('Schedule: ${_scheduleLabel(controller.scheduled)}'), + ), + for (final status in _statuses) + CupertinoButton( + onPressed: () => controller.setStatus(status), + child: Text( + '${controller.statuses.contains(status) ? '✓ ' : ''}${_title(status)}', + ), + ), + ], + ); + } + return Wrap( + spacing: 8, + children: [ + DropdownButton( + value: controller.sort, + isExpanded: true, + onChanged: (v) { + if (v != null) controller.setSort(v); + }, + items: _sorts + .map((e) => DropdownMenuItem(value: e.$1, child: Text(e.$2))) + .toList(), + ), + DropdownButton( + value: controller.scheduled, + isExpanded: true, + onChanged: (v) { + if (v != null) controller.setScheduled(v); + }, + items: _schedules + .map((e) => DropdownMenuItem(value: e.$1, child: Text(e.$2))) + .toList(), + ), + for (final status in _statuses) + FilterChip( + label: Text(_title(status)), + selected: controller.statuses.contains(status), + onSelected: (_) => controller.setStatus(status), + ), + ], + ); + } + + Future _choose( + BuildContext context, + String title, + List<(String, String)> values, + String selected, + ValueChanged onSelected, + ) async { + final value = await showCupertinoModalPopup( + context: context, + builder: (context) => CupertinoActionSheet( + title: Text(title), + actions: [ + for (final option in values) + CupertinoActionSheetAction( + onPressed: () => Navigator.pop(context, option.$1), + child: Text(option.$2), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.pop(context), + child: const Text('Cancel'), + ), + ), + ); + if (value != null && value != selected) onSelected(value); + } + + Widget _runTile(BuildContext context, JsonMap run) { + final identity = runIdentity(run['title'], run['name']); + final id = run['id']; + final title = identity.label.isEmpty ? 'Run $id' : identity.label; + final subtitle = + '${run['name'] ?? 'Pipeline unavailable'} · ${run['created_at'] ?? 'Time unavailable'}${run['scheduled'] == true ? ' · Scheduled' : ''}'; + if (_isCupertino) { + return CupertinoListTile( + title: Text(title), + subtitle: Text(subtitle), + additionalInfo: Text(runStatusLabel(run)), + trailing: const CupertinoListTileChevron(), + onTap: id is num ? () => onSelected(id) : null, + ); + } + return ListTile( + title: Text(title), + subtitle: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [Text(subtitle), Text(runStatusLabel(run))], + ), + trailing: const Icon(Icons.chevron_right), + onTap: id is num ? () => onSelected(id) : null, + ); + } } + +const _sorts = [ + ('newest_first', 'Newest'), + ('oldest_first', 'Oldest'), + ('pipeline_name', 'Pipeline'), + ('status', 'Status'), +]; +const _schedules = [ + ('any', 'Any schedule'), + ('scheduled', 'Scheduled'), + ('unscheduled', 'Unscheduled'), +]; +const _statuses = ['queued', 'running', 'waiting', 'succeeded', 'failed']; +String _sortLabel(String value) => + _sorts.firstWhere((e) => e.$1 == value, orElse: () => _sorts.first).$2; +String _scheduleLabel(String value) => _schedules + .firstWhere((e) => e.$1 == value, orElse: () => _schedules.first) + .$2; +String _title(String value) => '${value[0].toUpperCase()}${value.substring(1)}'; diff --git a/app/test/features/runs/ui/runs_list_widget_test.dart b/app/test/features/runs/ui/runs_list_widget_test.dart new file mode 100644 index 00000000..2414cf78 --- /dev/null +++ b/app/test/features/runs/ui/runs_list_widget_test.dart @@ -0,0 +1,180 @@ +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/runs/data/runs_repository.dart'; +import 'package:pipelines_app/features/runs/ui/runs_controller.dart'; +import 'package:pipelines_app/features/runs/ui/runs_list.dart'; + +class _Gateway implements ApiGateway { + final framesController = StreamController.broadcast(); + JsonMap? reply; + Object? failure; + Completer? gate; + + @override + Stream get frames => framesController.stream; + + @override + Future call(String type, [JsonMap fields = const {}]) async { + await gate?.future; + if (failure != null) throw failure!; + return reply ?? {'runs': []}; + } +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + late _Gateway gateway; + late RunsController controller; + + setUp(() { + gateway = _Gateway(); + controller = RunsController(RunsRepository(gateway)); + }); + + tearDown(() async { + controller.dispose(); + await gateway.framesController.close(); + }); + + Future mount( + WidgetTester tester, { + required bool cupertino, + ValueChanged? onSelected, + }) async { + gateway.reply = { + 'runs': [ + {'id': 1, 'name': 'Alpha', 'status': 'running', 'scheduled': true}, + {'id': 2, 'name': 'Beta', 'status': 'failed', 'scheduled': false}, + ], + }; + await controller.load(); + final list = AnimatedBuilder( + animation: controller, + builder: (context, _) => + RunsList(controller: controller, onSelected: onSelected ?? (_) {}), + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: list)) + : MaterialApp(home: Scaffold(body: list)), + ); + } + + testWidgets( + 'uses native search and filters runs in CupertinoApp', + (tester) async { + num? selectedRun; + await mount( + tester, + cupertino: true, + onSelected: (id) => selectedRun = id, + ); + + expect(find.byType(CupertinoSearchTextField), findsOneWidget); + expect(find.text('Alpha'), findsOneWidget); + await tester.enterText(find.byType(CupertinoSearchTextField), 'Beta'); + await tester.pumpAndSettle(); + expect(find.text('Beta'), findsNWidgets(2)); + expect(find.text('Alpha'), findsNothing); + await tester.tap(find.byType(CupertinoListTile)); + expect(selectedRun, 2); + await tester.enterText(find.byType(CupertinoSearchTextField), 'missing'); + await tester.pumpAndSettle(); + expect(find.text('No runs found.'), findsOneWidget); + await tester.enterText(find.byType(CupertinoSearchTextField), ''); + await tester.pumpAndSettle(); + await tester.tap(find.textContaining('Schedule:')); + await tester.pumpAndSettle(); + expect(find.text('Scheduled'), findsWidgets); + await tester.tap(find.text('Scheduled').last); + await tester.pumpAndSettle(); + expect(controller.scheduled, 'scheduled'); + }, + variant: TargetPlatformVariant.only(TargetPlatform.iOS), + ); + + testWidgets( + 'retains Material controls and handles large text on narrow layout', + (tester) async { + tester.view.devicePixelRatio = 2; + tester.view.physicalSize = const Size(780, 1560); + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + await tester.pumpWidget( + MaterialApp( + builder: (context, child) => MediaQuery( + data: MediaQuery.of( + context, + ).copyWith(textScaler: const TextScaler.linear(2)), + child: child!, + ), + home: Scaffold( + body: AnimatedBuilder( + animation: controller, + builder: (_, _) => + RunsList(controller: controller, onSelected: (_) {}), + ), + ), + ), + ); + gateway.reply = { + 'runs': [ + {'id': 1, 'name': 'Alpha', 'status': 'running', 'scheduled': true}, + {'id': 2, 'name': 'Beta', 'status': 'failed', 'scheduled': false}, + ], + }; + await controller.load(); + await tester.pumpAndSettle(); + expect(find.byType(TextField), findsOneWidget); + final layoutError = tester.takeException(); + expect( + layoutError, + isNull, + reason: layoutError is FlutterError + ? layoutError.toStringDeep() + : '$layoutError', + ); + }, + variant: TargetPlatformVariant.only(TargetPlatform.android), + ); + + testWidgets( + 'shows loading, error and server-empty states distinctly', + (tester) async { + gateway.failure = StateError('offline'); + gateway.gate = Completer(); + final loading = controller.load(); + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + child: AnimatedBuilder( + animation: controller, + builder: (_, child) => + RunsList(controller: controller, onSelected: (_) {}), + ), + ), + ), + ); + await tester.pump(); + expect(find.byType(CupertinoActivityIndicator), findsOneWidget); + gateway.gate!.complete(); + await loading; + await tester.pumpAndSettle(); + expect(find.textContaining('offline'), findsOneWidget); + expect(tester.takeException(), isNull); + gateway.failure = null; + gateway.gate = null; + gateway.reply = {'runs': []}; + await controller.load(); + await tester.pumpAndSettle(); + expect(find.text('No runs found.'), findsOneWidget); + expect(find.textContaining('offline'), findsNothing); + expect(tester.takeException(), isNull); + }, + variant: TargetPlatformVariant.only(TargetPlatform.iOS), + ); +}