Files
pipelines/app/test/features/runs/ui/runs_controller_test.dart
T

464 lines
15 KiB
Dart

import 'dart:async';
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';
class FakeGateway implements ApiGateway {
final framesController = StreamController<JsonMap>.broadcast();
final lists = <Completer<JsonMap>>[];
final details = <num, List<Completer<JsonMap>>>{};
int listCalls = 0;
@override
Stream<JsonMap> get frames => framesController.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'list_runs') {
listCalls++;
final request = Completer<JsonMap>();
lists.add(request);
return request.future;
}
final id = fields['run_id'] as num;
final request = Completer<JsonMap>();
details.putIfAbsent(id, () => []).add(request);
return request.future;
}
void listReply(int index, List<JsonMap> runs) =>
lists[index].complete({'runs': runs});
void detailReply(num id, int index, JsonMap value) =>
details[id]![index].complete({'run_detail': value});
}
void main() {
late FakeGateway gateway;
late RunsController controller;
var disposed = false;
setUp(() {
gateway = FakeGateway();
controller = RunsController(RunsRepository(gateway));
disposed = false;
});
tearDown(() {
if (!disposed) controller.dispose();
gateway.framesController.close();
});
test(
'wire envelopes update new rows, progress and selected completion',
() async {
controller.listen(gateway.frames);
final load = controller.load();
gateway.listReply(0, [
{'id': 1, 'pipeline_id': 7, 'name': 'test', 'status': 'running'},
]);
await load;
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.setPipeline(7);
controller.setQuery('test');
void event(JsonMap value) => gateway.framesController.add({
'type': 'run_event',
'run_event': value,
});
event({
'run_id': 2,
'pipeline_id': 7,
'name': 'test new',
'status': 'running',
'completed_steps': 0,
});
event({'run_id': 1, 'status': 'running', 'completed_steps': 3});
await Future<void>.delayed(Duration.zero);
expect(controller.runs, hasLength(2));
expect(
controller.runs.firstWhere((run) => run['id'] == 1)['completed_steps'],
3,
);
expect(controller.detail?['completed_steps'], 3);
event({'run_id': 1, 'status': 'succeeded', 'completed_steps': 4});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
expect(
controller.runs.firstWhere((run) => run['id'] == 1)['status'],
'succeeded',
);
expect(controller.selectedRunId, 1);
expect(controller.pipelineId, 7);
expect(controller.query, 'test');
expect(
gateway.listCalls,
1,
reason: 'valid deltas require no list reload',
);
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {
'id': 1,
'status': 'succeeded',
'completed_steps': 4,
'summary': 'Fresh completion evidence',
});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['summary'], 'Fresh completion evidence');
},
);
test('late selection response cannot replace latest selection', () async {
final first = controller.select(1);
final second = controller.select(2);
gateway.detailReply(2, 0, {'id': 2});
await second;
gateway.detailReply(1, 0, {'id': 1});
await first;
expect(controller.detail?['id'], 2);
});
test('pending load and selection futures are safe after dispose', () async {
final load = controller.load();
final select = controller.select(1);
controller.dispose();
disposed = true;
gateway.listReply(0, []);
gateway.detailReply(1, 0, {'id': 1});
await Future.wait([load, select]);
});
test('Back cancels scheduled detail refresh', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
controller.back();
await Future<void>.delayed(const Duration(milliseconds: 220));
expect(gateway.details[1], hasLength(1));
expect(controller.detail, isNull);
});
test('unrelated run event does not refresh selected detail', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 2,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 220));
expect(gateway.details[1], hasLength(1));
});
test('identity-less event reloads list', () async {
controller.listen(gateway.frames);
gateway.framesController.add({'type': 'run_event', 'status': 'running'});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 3},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.runs.single['id'], 3);
expect(controller.loading, isFalse);
});
test(
'full refresh is coalesced and keeps current detail until reply',
() async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(controller.detail?['id'], 1);
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
},
);
test('load with initial id clears list and detail loading', () async {
final load = controller.load(7);
expect(controller.loading, isTrue);
gateway.listReply(0, [
{'id': 7},
{'id': 8},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.details[7], hasLength(1));
expect(controller.loading, isFalse);
expect(controller.detailLoading, isTrue);
gateway.detailReply(7, 0, {'id': 7});
await load;
expect(controller.loading, isFalse);
expect(controller.detailLoading, isFalse);
expect(controller.detail?['id'], 7);
expect(controller.selectedRunId, 7);
});
test('newer list load wins when replies complete out of order', () async {
final first = controller.load();
final second = controller.load();
gateway.listReply(1, [
{'id': 2},
]);
await second;
gateway.listReply(0, [
{'id': 1},
]);
await first;
expect(controller.runs.single['id'], 2);
expect(controller.loading, isFalse);
});
test('list resync leaves the selected detail request valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({'type': 'run_event', 'status': 'running'});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 1, 'status': 'running'},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
expect(controller.selectedRunId, 1);
expect(gateway.details[1], hasLength(1));
});
test('manual list load keeps an in-flight detail refresh valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
final load = controller.load();
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
expect(controller.detailLoading, isFalse);
gateway.listReply(0, [
{'id': 1},
]);
});
test('event during a superseded resync still runs one followup', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
final load = controller.load();
expect(gateway.listCalls, 2);
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
gateway.listReply(0, [
{'id': 1},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 3);
gateway.listReply(2, [
{'id': 1},
{'id': 2},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.runs, hasLength(2));
expect(controller.loading, isFalse);
});
test('unknown run events coalesce into one followup resync', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
expect(controller.loading, isTrue);
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 3},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isTrue);
gateway.listReply(1, [
{'id': 3},
{'id': 4},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isFalse);
expect(controller.runs, hasLength(2));
});
test(
'selected refresh coalesces and Back ignores the pending reply',
() async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'failed',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(3));
controller.back();
gateway.detailReply(1, 2, {'id': 1, 'status': 'failed'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail, isNull);
expect(controller.detailLoading, isFalse);
expect(controller.error, isNull);
},
);
test('choosing another run does not display the previous detail', () async {
final first = controller.select(1);
gateway.detailReply(1, 0, {'id': 1});
await first;
final second = controller.select(2);
expect(controller.detail, isNull);
expect(controller.selectedRunId, 2);
expect(controller.detailLoading, isTrue);
gateway.detailReply(2, 0, {'id': 2});
await second;
expect(controller.detail?['id'], 2);
expect(controller.detailLoading, isFalse);
});
test('pending detail refresh is safe after dispose', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
controller.dispose();
disposed = true;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
});
test(
'an accepted rename folds into the selected detail and its list row',
() async {
final load = controller.load();
gateway.listReply(0, [
{'id': 1, 'title': 'Old title', 'status': 'running', 'name': 'one'},
{'id': 2, 'title': 'Other run', 'status': 'running'},
]);
await load;
final first = controller.select(1);
gateway.detailReply(1, 0, {
'id': 1,
'title': 'Old title',
'input': {'prompt': 'stored'},
});
await first;
controller.acceptRunDetail({
'id': 1,
'title': 'Renamed',
'status': 'succeeded',
});
expect(controller.detail?['title'], 'Renamed');
expect(controller.runs.first['title'], 'Renamed');
expect(controller.runs.first['name'], 'one');
expect(controller.runs[1]['title'], 'Other run');
final second = controller.select(2);
gateway.detailReply(2, 0, {'id': 2, 'title': 'Other run'});
await second;
controller.acceptRunDetail({'id': 1, 'title': 'Late rename'});
expect(controller.detail?['id'], 2);
expect(controller.detail?['title'], 'Other run');
expect(controller.runs.first['title'], 'Late rename');
},
);
test('active filter count treats each facet once and clears all', () {
expect(controller.activeFilterCount, 0);
controller.setStatus('failed');
controller.setStatus('running');
expect(controller.activeFilterCount, 1);
controller.setScheduled('scheduled');
controller.setPipeline(4);
expect(controller.activeFilterCount, 3);
controller.clearFilters();
expect(controller.activeFilterCount, 0);
expect(controller.statuses, isEmpty);
expect(controller.pipelineId, isNull);
expect(controller.scheduled, 'any');
expect(controller.timeRange, 'any_time');
});
}