Chat: find in chat (Ctrl+F / Cmd+F)
A find bar over the transcript: query, Match case, n of N (counted from the newest), Enter for an older match and Shift+Enter for a newer one. Matches are painted over the rendered text, code blocks included, without restyling it; the current match is scrolled into view. Matches in history not loaded yet come from the server's chat:<id>-scoped search and are reached by loading older pages. Esc closes and returns to the composer. The key is the pane's, so a focused terminal keeps Ctrl+F. Fixture answers scoped searches for captures; cheatsheet lists the key. The scoped search matches the query as typed, spaces included, like the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
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 = <JsonMap>[];
|
||||
List<JsonMap> searchHits = const [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
if (type == 'search') {
|
||||
searches.add(Map<String, dynamic>.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<void> _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<Text>(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<ChatFindScope>(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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_find.dart';
|
||||
|
||||
void main() {
|
||||
final rows = [
|
||||
{
|
||||
'kind': 'user',
|
||||
'key': 'u1',
|
||||
'message': {
|
||||
'id': 'u1',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Find the Needle'},
|
||||
{'type': 'text', 'text': 'and another needle'},
|
||||
],
|
||||
},
|
||||
},
|
||||
{'kind': 'activity', 'key': 'x', 'label': 'needle in activity'},
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'key': 'a1',
|
||||
'messageIds': ['a1', 'a2'],
|
||||
'parts': [
|
||||
{'type': 'reasoning', 'text': 'needle thoughts'},
|
||||
{'type': 'text', 'text': 'A **needle** here, needle there'},
|
||||
{'type': 'text', 'text': ' '},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
test('units are user turns and answer text parts as rendered', () {
|
||||
final units = chatFindUnits(rows, ['k0', 'k1', 'k2']);
|
||||
expect(units.map((u) => u.text), [
|
||||
'Find the Needle\nand another needle',
|
||||
'A **needle** here, needle there',
|
||||
]);
|
||||
expect(units.map((u) => u.rowKey), ['k0', 'k2']);
|
||||
expect(units.first.messageIds, ['u1']);
|
||||
expect(units.last.messageIds, ['a1', 'a2']);
|
||||
});
|
||||
|
||||
test('matches fold case unless asked, in display order', () {
|
||||
final units = chatFindUnits(rows, ['k0', 'k1', 'k2']);
|
||||
final folded = chatFindMatches(units, 'needle', caseSensitive: false);
|
||||
expect(folded.length, 4);
|
||||
expect(folded.first.start, 'Find the '.length);
|
||||
expect(folded.map((m) => m.ordinal), [0, 1, 0, 1]);
|
||||
final exact = chatFindMatches(units, 'Needle', caseSensitive: true);
|
||||
expect(exact.length, 1);
|
||||
});
|
||||
|
||||
test('regex characters in the query are literal', () {
|
||||
expect(chatFindRanges('a.b axb (c)', '.', caseSensitive: false), [
|
||||
(start: 1, end: 2),
|
||||
]);
|
||||
expect(chatFindRanges('a.b axb (c)', '(c)', caseSensitive: false), [
|
||||
(start: 8, end: 11),
|
||||
]);
|
||||
});
|
||||
|
||||
test('server hits parse from a scoped reply', () {
|
||||
final hits = ChatFindRemoteHit.fromReply({
|
||||
'search_results': {
|
||||
'results': [
|
||||
{
|
||||
'chat': {'message_id': 'm9', 'part': 1, 'offset': 4},
|
||||
},
|
||||
{
|
||||
'chat': {'message_id': 'm8'},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(hits.map((h) => (h.messageId, h.part, h.offset)), [
|
||||
('m9', 1, 4),
|
||||
('m8', 0, 0),
|
||||
]);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user