import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/search/domain/search_controller.dart'; import 'package:pipelines_app/features/search/domain/search_query.dart'; import 'search_test_support.dart'; void main() { test('a server error is shown, and retry issues a fresh request', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('plan'); await pumpEventQueue(); gateway.fail(0, StateError('server is down')); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.error); expect(controller.error, contains('server is down')); final retry = controller.retry(); await pumpEventQueue(); expect(gateway.calls, hasLength(2)); expect(gateway.lastSearch, {'query': 'plan', 'kind': '', 'limit': 0}); gateway.succeed( 1, envelope([ { 'kind': 'pipeline', 'target': 'pipeline:7', 'pipeline': {'id': 7}, }, ]), ); await retry; expect(controller.error, isNull); expect(controller.results, hasLength(1)); expect(controller.bodyKind, SearchBodyKind.results); }); test('a stale failure never replaces the newest query state', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('first'); await pumpEventQueue(); controller.updateQuery('second'); await pumpEventQueue(); gateway.fail(0, StateError('stale failure')); await pumpEventQueue(); expect(controller.error, isNull); expect(controller.loading, isTrue); expect(controller.bodyKind, SearchBodyKind.searching); gateway.succeed( 1, envelope([ { 'kind': 'pipeline', 'target': 'pipeline:9', 'pipeline': {'id': 9}, }, ]), ); await pumpEventQueue(); expect(controller.error, isNull); expect(controller.results.single['target'], 'pipeline:9'); expect(controller.bodyKind, SearchBodyKind.results); }); test( 'a missing search_results payload reports the unsupported copy', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('plan'); await pumpEventQueue(); gateway.succeed(0, {'type': 'pong'}); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.error); expect(controller.error, unsupportedSearchCopy); }, ); }