import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/pipelines/pipeline_repository.dart'; import 'package:pipelines_app/features/pipelines/pipelines_controller.dart'; import 'fixtures.dart'; PipelinesController controllerFor(FakeGateway gateway) => PipelinesController(PipelinesRepository(gateway)); void main() { test( 'opening a pipeline requests the row version and parses detail', () async { final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => pipelineDetailReply( fixture.detailFor( fields['pipeline_id'] as int, fields['version'] as int, )!, ), _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); await controller.openPipeline(1, version: 2); expect(gateway.callsOf('get_pipeline').single.fields, { 'pipeline_id': 1, 'version': 2, }); expect(controller.detail?.id, 1); expect(controller.detail?.resolvedVersion, 2); expect(controller.selectedVersion, 2); }, ); test('a stale detail reply cannot replace the newer selection', () async { final fixture = PipelineFixture(); final first = Completer>(); final second = Completer>(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => fields['pipeline_id'] == 1 ? first.future : second.future, _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); final firstRequest = controller.openPipeline(1, version: 2); final secondRequest = controller.openPipeline(2, version: 1); expect(controller.selectedId, 2); first.complete(pipelineDetailReply(fixture.details[0])); await firstRequest; expect( controller.detail, isNull, reason: 'the late reply belongs to the discarded selection', ); expect(controller.detailLoading, isTrue); second.complete(pipelineDetailReply(fixture.details[1])); await secondRequest; expect(controller.detail?.id, 2); expect(controller.detail?.name, 'Beta deploy'); }); test('a late reply after Back cannot reopen the cleared detail', () async { final fixture = PipelineFixture(); final reply = Completer>(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => reply.future, _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); final request = controller.openPipeline(1, version: 2); controller.clearSelection(); reply.complete(pipelineDetailReply(fixture.details[0])); await request; expect(controller.detail, isNull); expect(controller.selectedId, isNull); }); test( 'version selection requests that exact version and latest again', () async { final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => pipelineDetailReply( fixture.detailFor( fields['pipeline_id'] as int, fields['version'] as int, )!, ), _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); await controller.openPipeline(1, version: 2); await controller.selectVersion(1); expect(controller.selectedVersion, 1); expect(controller.detail?.source, contains('# selected version 1')); expect(gateway.callsOf('get_pipeline').last.fields, { 'pipeline_id': 1, 'version': 1, }); await controller.selectVersion(0); expect(controller.selectedVersion, 0); expect(gateway.callsOf('get_pipeline').last.fields, { 'pipeline_id': 1, 'version': 0, }); }, ); test('a detail refusal keeps the selection and names the reason', () async { final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => errorReply('pipeline 9 was not found'), _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); await controller.openPipeline(9); expect(controller.detailError, 'pipeline 9 was not found'); expect(controller.selectedId, 9); }); }