import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/search/domain/search_chat_slice.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'; const runHit = { 'kind': 'run', 'target': 'run:7', 'run': {'id': 7}, }; void main() { test('an all-kind search sends the run search, then chats as their own ' 'request, and shows runs before chats answer', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('flaky'); await pumpEventQueue(); expect(gateway.lastSearch, {'query': 'flaky', 'kind': '', 'limit': 0}); expect(gateway.chatCalls.single['search'], { 'query': 'flaky', 'kind': 'chat', 'limit': 0, }); gateway.succeed(0, envelope([runHit])); await pumpEventQueue(); expect(controller.shownResults.single['target'], 'run:7'); expect(controller.bodyKind, SearchBodyKind.results); expect(controller.shownChats, isEmpty); expect(controller.showsChatSection, isFalse); gateway.chatPending.single.complete( envelope([ chatHit('ses_a', snippet: 'the flaky retry'), // A non-chat row in a chat reply is not a chat. runHit, ]), ); await pumpEventQueue(); expect(controller.shownChats.single['target'], 'chat:ses_a'); expect(controller.showsChatSection, isTrue); expect(controller.shownResults.single['target'], 'run:7'); expect(controller.chatsNote, isNull); }); test('chat hits alone keep the body out of its empty state', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('fix'); await pumpEventQueue(); gateway.succeed(0, envelope(const [])); await pumpEventQueue(); // Runs answered empty while chats still load: still searching. expect(controller.bodyKind, SearchBodyKind.searching); expect(controller.emptyMessage, isNull); gateway.chatPending.single.complete(envelope([chatHit('ses_a')])); await pumpEventQueue(); expect(controller.bodyKind, SearchBodyKind.results); expect(controller.emptyMessage, isNull); }); test( 'a narrowed kind sends no chat search; Chats alone sends only chats', () async { final gateway = QueuedGateway(); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('t:run flaky'); await pumpEventQueue(); expect(gateway.calls, hasLength(1)); expect(gateway.chatCalls, isEmpty); controller.selectKind(SearchKind.chat); await pumpEventQueue(); expect(gateway.calls, hasLength(1)); expect(gateway.chatCalls.single['search'], { 'query': 'flaky', 'kind': 'chat', 'limit': 0, }); expect(controller.shownResults, isEmpty); expect(controller.showsChatSection, isFalse); }, ); test('a stale chat reply never replaces the newest query', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('first'); await pumpEventQueue(); controller.updateQuery('second'); await pumpEventQueue(); expect(gateway.chatCalls, hasLength(2)); gateway.chatPending[0].complete(envelope([chatHit('ses_old')])); await pumpEventQueue(); expect(controller.shownChats, isEmpty); gateway.chatPending[1].complete(envelope([chatHit('ses_new')])); await pumpEventQueue(); expect(controller.shownChats.single['target'], 'chat:ses_new'); }); test('a server without chat search says so beside its run results, and ' 'is the error with Chats selected', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('flaky'); await pumpEventQueue(); gateway.succeed(0, envelope([runHit])); gateway.chatPending.single.completeError( StateError( 'unknown search kind "chat"; use one of pipeline, version, run, ' 'step, field, artifact', ), ); await pumpEventQueue(); expect(controller.error, isNull); expect(controller.shownResults, hasLength(1)); expect(controller.chatsNote, unsupportedChatSearchCopy); expect(controller.showsChatSection, isTrue); controller.selectKind(SearchKind.chat); await pumpEventQueue(); gateway.chatPending.last.completeError( StateError('unknown search kind "chat"'), ); await pumpEventQueue(); expect(controller.error, unsupportedChatSearchCopy); expect(controller.bodyKind, SearchBodyKind.error); }); test('a truncated chat reply carries a bounds note', () async { final gateway = QueuedGateway(holdChats: true); final controller = controllerFor(gateway); addTearDown(controller.dispose); controller.updateQuery('flaky'); await pumpEventQueue(); gateway.succeed(0, envelope(const [])); gateway.chatPending.single.complete( envelope([chatHit('ses_a')], truncated: true), ); await pumpEventQueue(); expect(controller.chatsNote, contains('not searched')); }); }