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'; import 'package:pipelines_app/features/runs/ui/runs_stats_page.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': []}; } 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}) async { gateway.reply = { 'runs': [ { 'id': 1, 'pipeline_id': 9, 'name': 'Alpha', 'status': 'running', 'created_at': DateTime.now().toIso8601String(), 'scheduled': true, }, { 'id': 2, 'pipeline_id': 10, 'name': 'Beta', 'status': 'failed', 'created_at': DateTime.now().toIso8601String(), }, ], }; 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(); } Future reveal(WidgetTester tester, Finder target) => tester.scrollUntilVisible( target, 80, // The sheet's own list, not the search field's inner scrollable. scrollable: find .descendant( of: find.byKey(const ValueKey('runs.filter.sheet')), matching: find.byType(Scrollable), ) .first, ); Future closeSheet(WidgetTester tester) async { await tester.tapAt(const Offset(4, 4)); await tester.pumpAndSettle(); } testWidgets( 'material: filter and sort are icons; the filter sheet holds the search', (tester) async { await mount(tester, cupertino: false); expect(find.byType(TextField), findsNothing); expect(find.byKey(const ValueKey('runs.filter')), findsOneWidget); expect(find.byKey(const ValueKey('runs.sort')), findsOneWidget); expect(find.byKey(const ValueKey('runs.stats')), findsOneWidget); await tester.tap(find.byKey(const ValueKey('runs.filter'))); await tester.pumpAndSettle(); final sheet = find.byKey(const ValueKey('runs.filter.sheet')); expect( find.descendant(of: sheet, matching: find.byType(TextField)), findsOneWidget, ); await tester.enterText( find.byKey(const ValueKey('runs.filter.query')), 'Beta', ); await tester.pump(); expect(controller.query, 'Beta'); final alpha = find.descendant(of: sheet, matching: find.text('Alpha')); await reveal(tester, alpha); await tester.tap(alpha); await tester.pump(); expect(controller.pipelineId, 9); final week = find.descendant( of: sheet, matching: find.text('Last 7 days'), ); await reveal(tester, week); await tester.tap(week); await tester.pump(); expect(controller.timeRange, 'last_7_days'); await closeSheet(tester); // Applied filters stay visible and clearable under the bar. expect(find.byTooltip('Clear filter: Last 7 days'), findsOneWidget); await tester.tap(find.byTooltip('Clear filter: Last 7 days')); await tester.pumpAndSettle(); expect(controller.timeRange, 'any_time'); await tester.tap(find.byKey(const ValueKey('runs.sort'))); await tester.pumpAndSettle(); await tester.tap(find.text('Oldest first')); await tester.pumpAndSettle(); expect(controller.sort, 'oldest_first'); }, variant: TargetPlatformVariant.only(TargetPlatform.android), ); testWidgets( 'cupertino: the filter sheet filters and its chip clears the facet', (tester) async { await mount(tester, cupertino: true); await tester.tap(find.byKey(const ValueKey('runs.filter'))); await tester.pumpAndSettle(); expect(find.byType(CupertinoSearchTextField), findsOneWidget); final failed = find.widgetWithText(CupertinoListTile, 'failed'); await reveal(tester, failed); await tester.pumpAndSettle(); await tester.tap(failed); await tester.pumpAndSettle(); expect(controller.statuses, {'failed'}); await closeSheet(tester); final failedChip = find.widgetWithText(CupertinoButton, 'failed'); expect(failedChip, findsOneWidget); expect(find.text('Alpha'), findsNothing); await tester.tap(failedChip); await tester.pumpAndSettle(); expect(controller.statuses, isEmpty); expect(find.text('Alpha'), findsOneWidget); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS), ); testWidgets('the stats page shows usage, status pie and runs per day', ( tester, ) async { await mount(tester, cupertino: false); await tester.tap(find.byKey(const ValueKey('runs.stats'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('runs.stats.page')), findsOneWidget); expect(find.byKey(const ValueKey('runs.stats.pie')), findsOneWidget); expect(find.byKey(const ValueKey('runs.stats.days')), findsOneWidget); expect(find.text('Runs by status'), findsOneWidget); }); test('runs per day counts the last seven local days', () { final now = DateTime(2026, 10, 23, 12); final days = runsPerDay([ {'created_at': DateTime(2026, 10, 23, 9).toUtc().toIso8601String()}, {'created_at': DateTime(2026, 10, 23, 10).toUtc().toIso8601String()}, {'created_at': DateTime(2026, 10, 20, 9).toUtc().toIso8601String()}, {'created_at': DateTime(2026, 10, 1, 9).toUtc().toIso8601String()}, ], now); expect(days, hasLength(7)); expect(days.last.count, 2); expect(days[3].count, 1); expect(days.fold(0, (sum, day) => sum + day.count), 3); }); }