import 'dart:async'; 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/actions/delete_runs_dialog.dart'; import 'package:pipelines_app/features/runs/data/runs_sort_store.dart'; import 'package:pipelines_app/features/runs/screen.dart'; import 'package:pipelines_app/features/runs/ui/run_more_menu.dart'; import 'package:pipelines_app/features/runs/ui/runs_selection_bar.dart'; class _Gateway implements ApiGateway { _Gateway(this.runs); final List runs; final frames$ = StreamController.broadcast(); final deletes = []; JsonMap deleteReply = const {}; @override Stream get frames => frames$.stream; @override Future call(String type, [JsonMap fields = const {}]) async { switch (type) { case 'list_runs': return {'runs': runs}; case 'get_run': final id = fields['run_id']; return {'run_detail': runs.firstWhere((run) => run['id'] == id)}; case 'delete_runs': deletes.add(fields); return {'type': 'runs_deleted', 'runs_deleted': deleteReply}; } throw UnsupportedError(type); } } String _stamp(DateTime day, int hour) => DateTime(day.year, day.month, day.day, hour).toUtc().toIso8601String(); List _fixture() { final today = DateTime.now(); final yesterday = today.subtract(const Duration(days: 1)); return [ { 'id': 11, 'title': 'Today done', 'status': 'succeeded', 'created_at': _stamp(today, 1), 'size_bytes': 340 * 1024 * 1024, }, { 'id': 12, 'title': 'Today failed', 'status': 'failed', 'created_at': _stamp(today, 0), 'size_bytes': 1024 * 1024 * 1024, }, { 'id': 13, 'title': 'Today running', 'status': 'running', 'executing': true, 'created_at': _stamp(today, 0), }, { 'id': 21, 'title': 'Yesterday done', 'status': 'succeeded', 'created_at': _stamp(yesterday, 10), 'size_bytes': 0, }, ]; } Future _mount( WidgetTester tester, _Gateway gateway, { int? initialRunId, RunsSortStore? store, ValueChanged? onBack, }) async { tester.view.physicalSize = const Size(1100, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final screen = RunsScreen( gateway: gateway, initialRunId: initialRunId, onBack: onBack, sortStore: store ?? MemoryRunsSortStore(), ); await tester.pumpWidget(MaterialApp(home: Scaffold(body: screen))); await tester.pumpAndSettle(); } Checkbox _box(WidgetTester tester, int id) => tester.widget( find.descendant( of: find.byKey(ValueKey('run-select-$id')), matching: find.byType(Checkbox), ), ); void main() { testWidgets('day select-all selects only that day\'s finished runs', ( tester, ) async { final gateway = _Gateway(_fixture()); await _mount(tester, gateway); expect(find.textContaining('340 MB'), findsOneWidget); expect(find.textContaining('1 GB'), findsOneWidget); await tester.tap(find.byKey(runsSelectKey)); await tester.pumpAndSettle(); expect(find.text('0 selected'), findsOneWidget); // The active run's checkbox is disabled and says why. expect(_box(tester, 13).onChanged, isNull); expect( find.bySemanticsLabel(RegExp('Today running.*can’t be deleted')), findsOneWidget, ); await tester.tap(find.byKey(runsDaySelectKey('Today'))); await tester.pumpAndSettle(); expect(_box(tester, 11).value, isTrue); expect(_box(tester, 12).value, isTrue); expect(_box(tester, 13).value, isFalse); expect(_box(tester, 21).value, isFalse); expect(find.text('2 selected · 1.3 GB'), findsOneWidget); expect(find.text('Deselect all'), findsOneWidget); // Tapping a row toggles it instead of opening the run. await tester.tap(find.text('Yesterday done')); await tester.pumpAndSettle(); expect(_box(tester, 21).value, isTrue); expect(find.textContaining('3 selected'), findsOneWidget); await tester.tap(find.byKey(runsDaySelectKey('Today'))); await tester.pumpAndSettle(); expect(_box(tester, 11).value, isFalse); expect(_box(tester, 21).value, isTrue); await tester.tap(find.byKey(runsSelectDoneKey)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('run-select-11')), findsNothing); }); testWidgets('delete confirms, sends delete_runs and reports refusals', ( tester, ) async { final gateway = _Gateway(_fixture()) ..deleteReply = { 'deleted': [11], 'refused': [ {'run_id': 12, 'reason': 'run is being exported'}, ], 'freed_bytes': 340 * 1024 * 1024, }; await _mount(tester, gateway); await tester.tap(find.byKey(runsSelectKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(runsDaySelectKey('Today'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(runsDeleteSelectedKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Delete 2 runs?'), findsOneWidget); expect( find.text( 'This removes their artifacts, documents and history and frees about ' '1.3 GB. This can’t be undone.', ), findsOneWidget, ); expect(gateway.deletes, isEmpty, reason: 'nothing before confirming'); await tester.tap(find.byKey(deleteRunsConfirmKey)); await tester.pumpAndSettle(); expect(gateway.deletes.single['run_ids'], [11, 12]); expect(find.byType(AlertDialog), findsNothing); expect(find.text('Today done'), findsNothing); expect(find.text('Today failed'), findsOneWidget); expect( find.text( 'Deleted 1 run, freed 340 MB.\n1 run was not deleted:\n' 'Run 12: run is being exported', ), findsOneWidget, ); // The refused run stays selected. expect(find.text('1 selected · 1 GB'), findsOneWidget); }); testWidgets('Cancel in the prompt sends nothing', (tester) async { final gateway = _Gateway(_fixture()); await _mount(tester, gateway); await tester.tap(find.byKey(runsSelectKey)); await tester.pumpAndSettle(); await tester.tap(find.text('Yesterday done')); await tester.pumpAndSettle(); await tester.tap(find.byKey(runsDeleteSelectedKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Delete 1 run?'), findsOneWidget); await tester.tap(find.byKey(deleteRunsKeepKey)); await tester.pumpAndSettle(); expect(gateway.deletes, isEmpty); expect(find.text('Yesterday done'), findsOneWidget); }); testWidgets('runs_removed removes rows and closes the open detail', ( tester, ) async { final gateway = _Gateway(_fixture()); final backs = []; await _mount(tester, gateway, initialRunId: 12, onBack: backs.add); expect(find.byKey(const ValueKey('run-detail-pane')), findsOneWidget); gateway.frames$.add({ 'type': 'runs_removed', 'run_ids': [12, 21], }); await tester.pumpAndSettle(); expect(find.text('Today failed'), findsNothing); expect(find.text('Yesterday done'), findsNothing); expect(find.text('Today done'), findsOneWidget); expect(find.byKey(const ValueKey('run-detail-pane')), findsNothing); expect(backs, hasLength(1)); }); testWidgets('Delete run in the detail menu deletes the open run', ( tester, ) async { final gateway = _Gateway(_fixture()) ..deleteReply = { 'deleted': [21], 'refused': [], 'freed_bytes': 0, }; final backs = []; await _mount(tester, gateway, initialRunId: 21, onBack: backs.add); await tester.tap(find.byKey(runMoreMenuKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(deleteRunActionKey)); await tester.pumpAndSettle(); expect(find.text('Delete 1 run?'), findsOneWidget); await tester.tap(find.byKey(deleteRunsConfirmKey)); await tester.pumpAndSettle(); expect(gateway.deletes.single['run_ids'], [21]); expect(find.byKey(const ValueKey('run-detail-pane')), findsNothing); expect(find.text('Yesterday done'), findsNothing); expect(backs, hasLength(1)); }); testWidgets('Largest first is flat, unknown sizes last, and persists', ( tester, ) async { final store = MemoryRunsSortStore('largest_first'); await _mount(tester, _Gateway(_fixture()), store: store); expect(find.text('Today'), findsNothing, reason: 'no day headings'); final order = [ for (final title in [ 'Today failed', 'Today done', 'Today running', 'Yesterday done', ]) tester.getTopLeft(find.text(title)).dy, ]; expect(order, orderedEquals([...order]..sort())); // Unknown sizes come after every known size. expect(order[2], greaterThan(order[1])); await tester.tap(find.byKey(const ValueKey('runs.sort'))); await tester.pumpAndSettle(); await tester.tap(find.text('Newest first')); await tester.pumpAndSettle(); expect(store.value, 'newest_first'); expect(find.text('Today'), findsOneWidget); }); }