Merge: runs multi-select, delete and size sort

This commit is contained in:
2026-10-31 22:20:06 +00:00
22 changed files with 1928 additions and 142 deletions
@@ -0,0 +1,168 @@
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/domain/run_deletion.dart';
import 'package:pipelines_app/features/runs/domain/run_events.dart';
import 'package:pipelines_app/features/runs/domain/run_models.dart';
void main() {
test('formats sizes compactly', () {
expect(formatByteSize(0), '0 B');
expect(formatByteSize(512), '512 B');
expect(formatByteSize(4300), '4.2 KB');
expect(formatByteSize(1024 * 1024), '1 MB');
expect(formatByteSize(340 * 1024 * 1024), '340 MB');
expect(formatByteSize((1.2 * 1024 * 1024 * 1024).round()), '1.2 GB');
expect(formatByteSize(1024 * 1024 * 1024 - 1), '1 GB');
expect(formatByteSize(12 * 1024 * 1024 * 1024), '12 GB');
});
test('a missing or invalid size is unknown, not zero', () {
expect(runSizeBytes({'id': 1}), isNull);
expect(runSizeBytes({'size_bytes': -1}), isNull);
expect(runSizeBytes({'size_bytes': 'big'}), isNull);
expect(runSizeBytes({'size_bytes': 0}), isNull, reason: 'not measured');
expect(runSizeBytes({'size_bytes': 1}), 1);
expect(
runsSizeLabel([
{'id': 1},
]),
isNull,
);
expect(
runsSizeLabel([
{'size_bytes': 2048},
{'id': 2},
]),
'2 KB + unknown',
);
});
test('only finished runs are deletable', () {
for (final status in ['succeeded', 'failed', 'canceled', 'interrupted']) {
expect(
runIsDeletable({'id': 1, 'status': status}),
isTrue,
reason: status,
);
}
for (final status in ['running', 'waiting', 'queued', 'pending', '']) {
expect(
runIsDeletable({'id': 1, 'status': status}),
isFalse,
reason: status,
);
}
expect(
runIsDeletable({'id': 1, 'status': 'failed', 'executing': true}),
isFalse,
);
});
test('largest first sorts by size with unknown sizes last', () {
final sorted = sortRuns([
{'id': 1, 'created_at': '2026-10-30T10:00:00Z'},
{'id': 2, 'size_bytes': 10, 'created_at': '2026-10-30T10:00:00Z'},
{'id': 3, 'size_bytes': 5000, 'created_at': '2026-10-29T10:00:00Z'},
{'id': 4, 'size_bytes': 0, 'created_at': '2026-10-31T10:00:00Z'},
{'id': 5, 'size_bytes': 700, 'created_at': '2026-10-31T10:00:00Z'},
], sort: 'largest_first');
expect([for (final r in sorted) r['id']], [3, 5, 2, 4, 1]);
});
test('parses runs_deleted and words the report', () {
final deletion = RunsDeletion.fromReply({
'type': 'runs_deleted',
'id': '1',
'runs_deleted': {
'deleted': [3, 4],
'refused': [
{'run_id': 5, 'reason': 'run is active'},
],
'freed_bytes': 1288490189,
},
})!;
expect(deletion.deleted, [3, 4]);
expect(deletion.refused.single.runId, 5);
expect(
deletion.report,
'Deleted 2 runs, freed 1.2 GB.\n1 run was not deleted:\n'
'Run 5: run is active',
);
expect(RunsDeletion.fromReply({'type': 'error'}), isNull);
expect(
RunsDeletion.fromReply({
'deleted': [1],
}),
isNull,
reason: 'the result is nested under runs_deleted',
);
expect(
removedRunIds({
'run_ids': [1, 2.0, 'x'],
}),
[1, 2],
);
});
test('confirmation wording', () {
expect(deleteRunsTitle(1), 'Delete 1 run?');
expect(
deleteRunsMessage(3, size: '1.2 GB'),
'This removes their artifacts, documents and history and frees about '
'1.2 GB. This can’t be undone.',
);
expect(
deleteRunsMessage(1),
'This removes its artifacts, documents and history. '
'This can’t be undone.',
);
});
test('run events update the size only when they carry one', () {
final rows = <JsonMap>[
{'id': 1, 'status': 'running', 'size_bytes': 0},
];
final progress = foldRunEvent(rows, {'run_id': 1, 'status': 'running'});
expect(progress.runs.single['size_bytes'], 0);
final done = foldRunEvent(progress.runs, {
'run_id': 1,
'status': 'succeeded',
'size_bytes': 4096,
});
expect(runSizeBytes(done.runs.single), 4096);
});
test('more than 1000 ids are sent in batches and combined', () async {
final gateway = _BatchGateway();
final ids = [for (var id = 1; id <= 2500; id++) id];
final result = await RunsRepository(gateway).deleteRuns(ids);
expect(
[for (final batch in gateway.batches) batch.length],
[1000, 1000, 500],
);
expect(result.deleted, ids);
expect(result.freedBytes, 2500);
});
}
class _BatchGateway implements ApiGateway {
final batches = <List<int>>[];
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
final ids = (fields['run_ids'] as List).cast<int>();
batches.add(ids);
return {
'type': 'runs_deleted',
'runs_deleted': {
'deleted': ids,
'refused': <Object>[],
'freed_bytes': ids.length,
},
};
}
}
@@ -0,0 +1,288 @@
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/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<JsonMap> runs;
final frames$ = StreamController<JsonMap>.broadcast();
final deletes = <JsonMap>[];
JsonMap deleteReply = const {};
@override
Stream<JsonMap> get frames => frames$.stream;
@override
Future<JsonMap> 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<JsonMap> _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<void> _mount(
WidgetTester tester,
_Gateway gateway, {
bool cupertino = false,
int? initialRunId,
RunsSortStore? store,
ValueChanged<int?>? 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(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: screen))
: MaterialApp(home: Scaffold(body: screen)),
);
await tester.pumpAndSettle();
}
Checkbox _box(WidgetTester tester, int id) => tester.widget<Checkbox>(
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, cupertino: true);
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(CupertinoAlertDialog), 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 = <int?>[];
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 = <int?>[];
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);
});
}