import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_find_highlight.dart'; import 'chat_thread_fixtures.dart'; JsonMap _text(String text) => {'type': 'text', 'text': text}; /// Answers scoped searches with [searchHits] for history older than the /// loaded page. class _FindGateway extends FakeChatGateway { final searches = []; List searchHits = const []; @override Future call(String type, [JsonMap fields = const {}]) async { if (type == 'search') { searches.add(Map.from(fields['search'] as Map)); return { 'type': 'search_results', 'search_results': {'results': searchHits}, }; } return super.call(type, fields); } } final _newest = [ for (var i = 0; i < 30; i++) chatMessage( 'n$i', i.isEven ? 'user' : 'assistant', parts: [ _text(i == 2 || i == 27 ? 'Needle at $i, and needle' : 'filler $i'), ], ), ]; final _older = [ chatMessage('o1', 'user', parts: [_text('an old needle')]), chatMessage('o2', 'assistant', parts: [_text('nothing')]), ]; Future<_FindGateway> _host(WidgetTester tester, {bool older = false}) async { tester.view.physicalSize = const Size(1000, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _FindGateway() ..history = (fields) async { final query = fields['chat_query'] as Map; if (query['cursor'] == 'c1') return chatPage(_older); return chatPage(_newest, cursor: older ? 'c1' : '', hasMore: older); }; addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, drafts: MemoryChatDraftStore(), newOperationId: () => 'op', ), ), ), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); await tester.pump(); return gateway; } Future _ctrlF(WidgetTester tester) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyF); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pump(); await tester.pump(); } String _count(WidgetTester tester) => tester.widget(find.byKey(const ValueKey('chat-find-count'))).data!; void main() { testWidgets('Ctrl+F finds in the chat, steps older with Enter and Esc ' 'returns to the composer', (tester) async { await _host(tester); await tester.tap(find.byType(EditableText).last); await tester.pump(); await _ctrlF(tester); expect(find.byKey(const ValueKey('chat-find-field')), findsOneWidget); await tester.enterText( find.byKey(const ValueKey('chat-find-field')), 'needle', ); await tester.pumpAndSettle(); expect(_count(tester), '1 of 4'); // Each built text with a match registers where it draws. final scope = tester.widget(find.byType(ChatFindScope)); expect(scope.controller, isNotNull); expect(scope.targets.texts, isNotEmpty); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(_count(tester), '2 of 4'); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(_count(tester), '3 of 4'); // The match in message n2 is near the top of history: it is built now. expect( find.textContaining('Needle at 2', findRichText: true), findsWidgets, ); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.pumpAndSettle(); expect(_count(tester), '2 of 4'); await tester.tap(find.byKey(const ValueKey('chat-find-case'))); await tester.pumpAndSettle(); expect(_count(tester), '1 of 2'); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('chat-find-field')), findsNothing); }); testWidgets('matches in unloaded history are counted from the server and ' 'reached by loading older pages', (tester) async { final gateway = await _host(tester, older: true); gateway.searchHits = [ { 'chat': {'message_id': 'o1', 'part': 0, 'offset': 7}, }, ]; await tester.tap(find.byType(EditableText).last); await tester.pump(); await _ctrlF(tester); await tester.enterText( find.byKey(const ValueKey('chat-find-field')), 'needle', ); await tester.pump(const Duration(milliseconds: 300)); await tester.pumpAndSettle(); expect(gateway.searches.single['scope'], 'chat:s'); expect(gateway.searches.single['cursor'], '0|c1'); expect(_count(tester), '1 of 5'); // Step to the oldest: it is only on the server, so the older page loads. for (var i = 0; i < 4; i++) { await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); } expect(_count(tester), '5 of 5'); expect( gateway.calls.where( (call) => call.type == 'chat_messages' && (call.fields['chat_query'] as Map)['cursor'] == 'c1', ), isNotEmpty, ); expect( find.textContaining('an old needle', findRichText: true), findsWidgets, ); }); testWidgets('Ctrl+F with no focus in the chat opens nothing', (tester) async { await _host(tester); expect(find.byKey(const ValueKey('chat-find-field')), findsNothing); // No focus in the chat at all: the pane's handler never sees the key. await _ctrlF(tester); expect(find.byKey(const ValueKey('chat-find-field')), findsNothing); }); }