Files
pipelines/app/test/features/runs/domain/run_deletion_test.dart
T
alan 266916f92a Runs: multi-select delete, run sizes and Largest-first sort
- Select mode (bar Select or long-press): per-row checkboxes, per-day
  Select all/Deselect all over finished runs only, "N selected" with the
  selection size, Delete and Done. Active runs get a disabled checkbox
  whose semantics give the reason.
- Native destructive confirmation, then delete_runs {run_ids} (batched by
  1000); the nested runs_deleted reply removes deleted rows and reports
  "Deleted N runs, freed X" plus refusals verbatim. runs_removed pushes
  remove rows and close an open detail; a deleted open run closes too.
  Delete run in the row and detail menus.
- size_bytes shown compactly in rows (0/absent = unknown, nothing shown),
  updated from run_event frames that carry it; Largest first sort (flat,
  unknowns last); the sort choice persists.
- Shared AppListRow gains selectionControl and onLongPress.
- Browser probe tool/runs_delete_probe.mjs; fixture server implements
  delete_runs/runs_removed.
2026-10-31 22:19:36 +00:00

169 lines
5.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
},
};
}
}