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_filter_sheet.dart'; import 'package:pipelines_app/features/runs/ui/runs_filters.dart'; import 'package:pipelines_app/features/runs/ui/runs_list.dart'; import 'package:pipelines_app/ui/layout/app_sheet_header.dart'; class _Gateway implements ApiGateway { final framesController = StreamController.broadcast(); JsonMap? reply; @override Stream get frames => framesController.stream; @override Future call(String type, [JsonMap fields = const {}]) async => reply ?? {'runs': []}; } /// The Runs sheets share one header (title and Done), single-choice groups /// use check rows, statuses read in sentence case, and the usage page neither /// repeats its title nor draws a chart of zeros. void main() { 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, String? createdAt, }) async { tester.view.physicalSize = const Size(800, 1400); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final at = createdAt ?? DateTime.now().toIso8601String(); gateway.reply = { 'runs': [ { 'id': 1, 'pipeline_id': 9, 'name': 'Alpha', 'status': 'running', 'created_at': at, 'scheduled': true, }, ], }; await controller.load(); final list = AnimatedBuilder( animation: controller, builder: (context, _) => RunsList(controller: controller, onSelected: (_) {}), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: list)) : MaterialApp(home: Scaffold(body: list)), ); await tester.pumpAndSettle(); } for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; testWidgets('$host filter sheet has a header whose Done closes it', ( tester, ) async { await mount(tester, cupertino: cupertino); await tester.tap(find.byKey(const ValueKey('runs.filter'))); await tester.pumpAndSettle(); expect(find.byType(AppSheetHeader), findsOneWidget); expect( find.descendant( of: find.byType(AppSheetHeader), matching: find.text(runsFilterTitle), ), findsOneWidget, ); expect(find.text('Queued'), findsOneWidget); expect(find.text('queued'), findsNothing); expect(find.byKey(runsFilterClearKey), findsNothing); await tester.tap(find.text('Last 7 days')); await tester.pumpAndSettle(); expect(controller.timeRange, 'last_7_days'); // An active filter offers Clear in the header. expect(find.byKey(runsFilterClearKey), findsOneWidget); await tester.tap(find.byKey(runsFilterDoneKey)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('runs.filter.sheet')), findsNothing); // The applied chip reads in sentence case too. expect(find.textContaining('Last 7 days'), findsOneWidget); }); } testWidgets('Material single-choice groups are check rows, not checkboxes', ( tester, ) async { await mount(tester, cupertino: false); await tester.tap(find.byKey(const ValueKey('runs.filter'))); await tester.pumpAndSettle(); // Seven statuses may be combined: those keep their checkboxes. expect(find.byType(CheckboxListTile), findsNWidgets(7)); final anyTime = find.widgetWithText(ListTile, 'Any time'); expect(anyTime, findsOneWidget); expect( find.descendant(of: anyTime, matching: find.byIcon(Icons.check)), findsOneWidget, ); expect( find.descendant(of: anyTime, matching: find.byType(Checkbox)), findsNothing, ); final today = find.widgetWithText(ListTile, 'Today'); expect( find.descendant(of: today, matching: find.byIcon(Icons.check)), findsNothing, ); expect(tester.widget(anyTime).selected, isTrue); }); testWidgets('Material sort sheet has the same header', (tester) async { await mount(tester, cupertino: false); await tester.tap(find.byKey(const ValueKey('runs.sort'))); await tester.pumpAndSettle(); expect(find.text(runsSortTitle), findsOneWidget); await tester.tap(find.byKey(runsSortDoneKey)); await tester.pumpAndSettle(); expect(find.text(runsSortTitle), findsNothing); expect(controller.sort, 'newest_first'); }); testWidgets('Cupertino sort sheet marks the selected order with an icon', ( tester, ) async { await mount(tester, cupertino: true); await tester.tap(find.byKey(const ValueKey('runs.sort'))); await tester.pumpAndSettle(); expect(find.text('✓ Newest first'), findsNothing); final newestFirst = find.widgetWithText( CupertinoActionSheetAction, 'Newest first', ); expect(newestFirst, findsOneWidget); expect( find.descendant( of: newestFirst, matching: find.byIcon(CupertinoIcons.checkmark), ), findsOneWidget, ); final selected = find.descendant( of: newestFirst, matching: find.byWidgetPredicate( (widget) => widget is Semantics && widget.properties.selected == true, ), ); expect(selected, findsOneWidget); }); testWidgets('the usage page is honest when nothing was reported', ( tester, ) async { await mount( tester, cupertino: false, createdAt: DateTime(2020, 1, 1).toIso8601String(), ); await tester.tap(find.byKey(const ValueKey('runs.stats'))); await tester.pumpAndSettle(); // The bar says Usage; the body does not repeat it. expect(find.text('Usage'), findsOneWidget); expect(find.byKey(const ValueKey('runs.stats.usage.empty')), findsOne); expect(find.byKey(const ValueKey('runs.stats.days')), findsNothing); expect(find.byKey(const ValueKey('runs.stats.days.empty')), findsOne); expect(find.text('Running'), findsOneWidget); expect(find.text('0'), findsNothing); }); testWidgets('the 7-day chart shows bars over an axis when runs started', ( tester, ) async { await mount(tester, cupertino: true); await tester.tap(find.byKey(const ValueKey('runs.stats'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('runs.stats.days')), findsOneWidget); expect(find.byKey(const ValueKey('runs.stats.days.empty')), findsNothing); // Only the day with a run is labelled with its count. final days = find.byKey(const ValueKey('runs.stats.days')); expect(find.descendant(of: days, matching: find.text('1')), findsOne); expect(find.text('0'), findsNothing); expect(tester.takeException(), isNull); }); }