import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/chat/screen.dart'; import 'package:pipelines_app/features/search/search_screen.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import 'search_wiring_test.dart' show pumpApp, reply, replyRunList, tapTab, waitForSearch; const _session = 'ses_chatsearch1'; Map _results(String kind, List> hits) => { 'type': 'search_results', 'search_results': { 'query': 'flaky', 'kind': kind, 'results': hits, 'truncated': false, 'scanned_steps': 0, 'scanned_bytes': 0, 'cap': 50, 'scan_bound_bytes': 1 << 20, }, }; /// The whole path from the Search tab: a typed query, the run search and the /// chat search the app sends after it, then a tap on the chat hit opens that /// chat in the Chat tab with its session selected. void main() { for (final cupertino in [false, true]) { testWidgets('${cupertino ? 'Cupertino' : 'Material'}: a chat hit opens ' 'its chat in the Chat tab', (tester) async { tester.view.physicalSize = const Size(900, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final channel = FakeChannel()..readyCompleter.complete(); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')), ); addTearDown(provider.dispose); await pumpApp( tester, channel: channel, client: PipelinesClient(channelFactory: (_) => channel), provider: provider, cupertino: cupertino, ); await replyRunList(tester, channel); await tapTab(tester, 'Search'); await tester.pump(); await tester.pump(const Duration(milliseconds: 600)); await tester.pump(const Duration(milliseconds: 100)); await tester.enterText( find.byKey(const ValueKey('search.field')), 'flaky', ); await tester.pump(const Duration(milliseconds: 300)); final runs = await waitForSearch(tester, channel); channel.incoming.add(jsonEncode({'id': runs['id'], ..._results('', [])})); await tester.pump(); await tester.pump(); final chats = await waitForSearch(tester, channel, chats: true); expect(chats['search'], {'query': 'flaky', 'kind': 'chat', 'limit': 0}); channel.incoming.add( jsonEncode({ 'id': chats['id'], ..._results('chat', [ { 'kind': 'chat', 'target': 'chat:$_session', 'score': 1, 'matched_field': 'message', 'chat': { 'session_id': _session, 'title': 'Fix CI', 'directory': '/work/api', 'backend': 'claude', 'message_id': 'm2', 'snippet': 'the flaky retry loop', }, }, ]), }), ); await tester.pump(); await tester.pump(); expect(find.text('Chats (1)'), findsOneWidget); expect(find.textContaining('the flaky retry loop'), findsOneWidget); await tester.tap( find.byKey(const ValueKey('search.result.chat:$_session')), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); expect(provider.value.uri.path, '/chat/$_session'); expect(find.byType(SearchScreen), findsNothing); final screen = tester.widget(find.byType(ChatScreen)); expect(screen.initialSessionId, _session); await reply( tester, channel, 'chat_sessions', body: { 'type': 'chat_sessions', 'chat_sessions': [ { 'id': _session, 'title': 'Fix CI', 'directory': '/work/api', 'backend': 'claude', }, ], }, ); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Fix CI'), findsWidgets); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); } }