Search: find operator chats by title, directory and message text

A search with kind chat matches the operator-session registry's titles and
directories and the newest messages of recent transcripts through the chat
broker (bounded count, concurrency and one-second budget), answered off the
connection's read loop. The all-kind search is unchanged for older clients.

The app sends the chat search after its run search and shows a Chats group
with title, harness, directory and snippet; a chat hit opens that chat in the
Chat tab. The probe fixture answers chat searches over ?chats=N.
This commit is contained in:
2026-11-03 13:01:53 +00:00
parent d34185b56c
commit bff7115e4e
31 changed files with 1659 additions and 118 deletions
@@ -0,0 +1,131 @@
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<String, dynamic> _results(String kind, List<Map<String, dynamic>> 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<ChatScreen>(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();
});
}
}
+67 -10
View File
@@ -50,6 +50,63 @@ Future<Map<String, dynamic>> waitForRequest(
fail('Timed out waiting for $type #$index');
}
/// The search requests the app sent: the run/artifact searches, or with
/// [chats] the `kind: chat` searches it sends after each of them.
List<Map<String, dynamic>> searchRequestsOf(
FakeChannel channel, {
bool chats = false,
}) => requestsOf(channel, 'search')
.where(
(request) => ((request['search'] as Map?)?['kind'] == 'chat') == chats,
)
.toList();
Future<Map<String, dynamic>> waitForSearch(
WidgetTester tester,
FakeChannel channel, {
int index = 0,
bool chats = false,
}) async {
for (var attempt = 0; attempt < 80; attempt++) {
final requests = searchRequestsOf(channel, chats: chats);
if (index < requests.length) return requests[index];
await tester.pump(const Duration(milliseconds: 5));
}
fail('Timed out waiting for ${chats ? 'chat ' : ''}search #$index');
}
/// Answers the [index]th chat search (sent after each run search's reply)
/// with no chats, so the app's serialized requests continue.
Future<void> replyChatSearch(
WidgetTester tester,
FakeChannel channel, {
int index = 0,
}) async {
final request = await waitForSearch(
tester,
channel,
index: index,
chats: true,
);
channel.incoming.add(
jsonEncode({
'id': request['id'],
'type': 'search_results',
'search_results': {
'query': (request['search'] as Map)['query'],
'kind': 'chat',
'results': <JsonMap>[],
'cap': 50,
'scanned_steps': 0,
'scanned_bytes': 0,
'scan_bound_bytes': 1 << 20,
},
}),
);
await tester.pump();
await tester.pump();
}
Future<void> reply(
WidgetTester tester,
FakeChannel channel,
@@ -106,7 +163,7 @@ Future<void> searchArtifactHit(
await tester.pump(const Duration(milliseconds: 100));
await tester.enterText(find.byKey(const ValueKey('search.field')), 'report');
await tester.pump(const Duration(milliseconds: 300));
final request = await waitForRequest(tester, channel, 'search', index: index);
final request = await waitForSearch(tester, channel, index: index);
channel.incoming.add(
jsonEncode({
'id': request['id'],
@@ -125,6 +182,7 @@ Future<void> searchArtifactHit(
);
await tester.pump();
await tester.pump();
await replyChatSearch(tester, channel, index: index);
}
/// Selects the artifact hit, leaving run 7's `get_run` request unanswered.
@@ -401,7 +459,7 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 300));
final request = await waitForRequest(tester, channel, 'search');
final request = await waitForSearch(tester, channel);
expect(request['search'], {'query': 'release', 'kind': '', 'limit': 0});
channel.incoming.add(
jsonEncode({
@@ -421,6 +479,7 @@ void main() {
);
await tester.pump();
await tester.pump();
await replyChatSearch(tester, channel);
expect(find.text('Run 7 · Preserved run'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('search.result.run:7')));
@@ -437,7 +496,8 @@ void main() {
expect(find.text('Run 7'), findsWidgets);
// One app socket carried the search and the run reads.
expect(sockets, 1);
expect(requestsOf(channel, 'search'), hasLength(1));
expect(searchRequestsOf(channel), hasLength(1));
expect(searchRequestsOf(channel, chats: true), hasLength(1));
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
@@ -480,12 +540,7 @@ void main() {
await tester.pump(const Duration(milliseconds: 100));
await tester.enterText(find.byKey(const ValueKey('search.field')), query);
await tester.pump(const Duration(milliseconds: 300));
final request = await waitForRequest(
tester,
channel,
'search',
index: index,
);
final request = await waitForSearch(tester, channel, index: index);
channel.incoming.add(
jsonEncode({
'id': request['id'],
@@ -504,6 +559,7 @@ void main() {
);
await tester.pump();
await tester.pump();
await replyChatSearch(tester, channel, index: index);
}
// An artifact hit opens the run's Artifacts tab and that artifact's viewer.
@@ -592,7 +648,7 @@ void main() {
'report',
);
await tester.pump(const Duration(milliseconds: 300));
final request = await waitForRequest(tester, channel, 'search');
final request = await waitForSearch(tester, channel);
channel.incoming.add(
jsonEncode({
'id': request['id'],
@@ -611,6 +667,7 @@ void main() {
);
await tester.pump();
await tester.pump();
await replyChatSearch(tester, channel);
await tester.tap(
find.byKey(const ValueKey('search.result./artifacts/7/2/report.txt')),