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_models.dart'; import 'search_test_support.dart'; void main() { test('empty query with no kind is idle and sends no request', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery(' '); await pumpEventQueue(); expect(gateway.calls, isEmpty); expect(controller.bodyKind, SearchBodyKind.idle); expect(controller.results, isEmpty); expect(controller.loaded, isFalse); expect(controller.loading, isFalse); }); test('the request carries the parsed query, kind and limit 0', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('t:run Plan TYPE:artifact'); await pumpEventQueue(); expect(gateway.calls, hasLength(1)); expect(gateway.lastSearch, { 'query': 'Plan', 'kind': 'artifact', 'limit': 0, }); }); test( 'a kind selector change removes type tokens and sends at once', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('plan t:run'); await pumpEventQueue(); expect(gateway.lastSearch['kind'], 'run'); controller.selectKind(SearchKind.pipeline); await pumpEventQueue(); expect(controller.query.trim(), 'plan'); expect(controller.query, isNot(contains('t:run'))); expect(controller.selectedKind, SearchKind.pipeline); expect(gateway.calls, hasLength(2)); expect(gateway.lastSearch, { 'query': 'plan', 'kind': 'pipeline', 'limit': 0, }); }, ); test( 'kind-only browsing sends a blank query and reports the empty copy', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.selectKind(SearchKind.pipeline); await pumpEventQueue(); expect(gateway.lastSearch, {'query': '', 'kind': 'pipeline', 'limit': 0}); expect(controller.bodyKind, SearchBodyKind.searching); gateway.succeed(0, envelope(const [])); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.empty); expect(controller.emptyMessage, 'No pipelines yet.'); }, ); test('the latest query wins and a stale reply is dropped', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('first'); await pumpEventQueue(); controller.updateQuery('second'); await pumpEventQueue(); expect(gateway.calls, hasLength(2)); gateway.succeed( 0, envelope([ { 'kind': 'run', 'target': 'run:1', 'run': {'id': 1}, }, ]), ); await pumpEventQueue(); expect(controller.results, isEmpty); expect(controller.loading, isTrue); expect(controller.bodyKind, SearchBodyKind.searching); gateway.succeed( 1, envelope([ { 'kind': 'run', 'target': 'run:2', 'run': {'id': 2}, }, ]), ); await pumpEventQueue(); expect(controller.results.single['target'], 'run:2'); expect(controller.loading, isFalse); expect(controller.bodyKind, SearchBodyKind.results); }); test('truncation and scan bounds come from the server envelope', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('plan'); await pumpEventQueue(); gateway.succeed( 0, envelope( [ { 'kind': 'field', 'target': 'run:11:4:output', 'field': {'run_id': 11}, }, ], truncated: true, scannedSteps: 2000, scannedBytes: 1048576, cap: 50, ), ); await pumpEventQueue(); expect(controller.truncated, isTrue); expect(controller.scannedSteps, 2000); expect(controller.scannedBytes, 1048576); expect(controller.cap, 50); expect(controller.boundsText, contains('truncated')); expect(controller.boundsText, contains('cap 50')); expect(controller.boundsText, contains('2000 steps')); }); test( 'a truncated empty reply reports the scan bound instead of a complete empty', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('needle'); await pumpEventQueue(); gateway.succeed( 0, envelope( const [], truncated: true, scannedSteps: 2000, scannedBytes: 1048576, cap: 50, ), ); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.empty); expect( controller.emptyMessage, 'No matches for "needle" within the server\'s scan limit.', ); expect(controller.boundsText, contains('Results truncated')); expect(controller.boundsText, contains('0 results')); expect(controller.boundsText, contains('2000 steps')); expect(controller.boundsText, contains('scan limit 1.0 MiB')); }, ); test( 'a complete empty reply stays a plain empty with no bounds line', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('needle'); await pumpEventQueue(); gateway.succeed(0, envelope(const [])); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.empty); expect(controller.emptyMessage, 'No matches for "needle".'); expect(controller.boundsText, isNull); }, ); test('in-hand kind narrowing keeps the list honest while loading', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('plan'); await pumpEventQueue(); gateway.succeed( 0, envelope([ { 'kind': 'run', 'target': 'run:1', 'run': {'id': 1}, }, { 'kind': 'pipeline', 'target': 'pipeline:7', 'pipeline': {'id': 7}, }, ]), ); await pumpEventQueue(); expect(controller.shownResults, hasLength(2)); controller.selectKind(SearchKind.run); await pumpEventQueue(); expect(controller.shownResults, hasLength(1)); expect(controller.shownResults.single['target'], 'run:1'); }); }