import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/pipelines/pipeline_query.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)); Future settle() => Future.delayed(Duration.zero); void main() { test( 'load sends exact list and runs requests and parses the envelopes', () async { final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); expect(gateway.calls.map((call) => call.type).toList(), [ 'list_pipelines', 'list_runs', ]); expect(gateway.calls[0].fields, isEmpty); expect(gateway.calls[1].fields, isEmpty); expect(controller.pipelines.map((pipeline) => pipeline.id), [1, 2, 3]); expect(controller.pipelinesLoaded, isTrue); expect(controller.runsLoaded, isTrue); expect(controller.latestRuns[1]?.id, 42); expect(controller.listError, isNull); expect(controller.runsError, isNull); }, ); test('refresh keeps the selected pipeline and requested version', () async { final fixture = PipelineFixture(); final summaries = List>.of(fixture.summaries); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(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(2, version: 1); summaries[1] = pipelineSummary(id: 2, name: 'Beta deploy renamed'); await controller.load(); expect(controller.selectedId, 2); expect(controller.selectedVersion, 1); expect(controller.detail?.id, 2); expect(controller.pipelines[1].name, 'Beta deploy renamed'); expect(gateway.callsOf('get_pipeline').last.fields, { 'pipeline_id': 2, 'version': 1, }); }); test( 'server refusals are shown verbatim and a list failure is retryable', () async { final gateway = FakeGateway( (type, fields) => errorReply('server offline'), ); final controller = controllerFor(gateway); await controller.load(); expect(controller.listError, 'server offline'); expect(controller.pipelinesLoaded, isFalse); expect(controller.runsError, 'server offline'); }, ); test('a runs failure is truthful without discarding pipelines', () async { final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => errorReply('runs unavailable'), _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); await controller.load(); expect(controller.pipelinesLoaded, isTrue); expect(controller.pipelines, isNotEmpty); expect(controller.runsLoaded, isFalse); expect(controller.runsError, 'runs unavailable'); }); test('run facets wait for a known run history', () async { final fixture = PipelineFixture(); final runsReply = Completer>(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runsReply.future, _ => errorReply('unexpected $type'), }, ); final controller = controllerFor(gateway); controller.setFilters(const PipelineFilters(presence: RunPresence.noRuns)); final loading = controller.load(); await settle(); expect(controller.pipelinesLoaded, isTrue); expect(controller.runsLoaded, isFalse); expect(controller.visiblePipelines.length, 3); runsReply.complete(runListReply(fixture.runs)); await loading; expect(controller.runsLoaded, isTrue); expect(controller.visiblePipelines.map((pipeline) => pipeline.id), [3]); }); test('a stale list refresh cannot overwrite newer rows', () async { final first = Completer>(); final second = Completer>(); var listCalls = 0; final gateway = FakeGateway((type, fields) { if (type == 'list_runs') return runListReply(const []); if (type == 'list_pipelines') { listCalls++; return listCalls == 1 ? first.future : second.future; } return errorReply('unexpected $type'); }); final controller = controllerFor(gateway); final firstLoad = controller.load(); final secondLoad = controller.load(); second.complete(pipelineListReply([pipelineSummary(id: 2, name: 'Newer')])); await secondLoad; first.complete(pipelineListReply([pipelineSummary(id: 1, name: 'Older')])); await firstLoad; expect(controller.pipelines.single.name, 'Newer'); expect(controller.listLoading, isFalse); }); test('pipeline errors keep a readable message', () { expect( pipelineErrorMessage(const PipelineRequestException('exact refusal')), 'exact refusal', ); expect(pipelineErrorMessage(Exception('broken')), 'broken'); }); }