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 = [ {'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 = >[]; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { final ids = (fields['run_ids'] as List).cast(); batches.add(ids); return { 'type': 'runs_deleted', 'runs_deleted': { 'deleted': ids, 'refused': [], 'freed_bytes': ids.length, }, }; } }