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.broadcast(); final lists = >[]; final details = >>{}; int listCalls = 0; @override Stream get frames => framesController.stream; @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'list_runs') { listCalls++; final request = Completer(); lists.add(request); return request.future; } final id = fields['run_id'] as num; final request = Completer(); details.putIfAbsent(id, () => []).add(request); return request.future; } void listReply(int index, List 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('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.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.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.delayed(Duration.zero); expect(gateway.listCalls, 1); gateway.listReply(0, [ {'id': 3}, ]); await Future.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.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.delayed(Duration.zero); expect(controller.detail?['status'], 'succeeded'); }, ); }