Files
pipelines/app/test/features/search/search_screen_test.dart
T
alan ccc80a9a8d Add keyboard navigation to search results
Keep the search field focused while selecting results with Control or arrow keys, and open the selected result with Enter. Document the field-scoped shortcuts and cover navigation, reset, submission, and typing behavior with regression tests.
2026-11-13 15:51:00 +00:00

388 lines
11 KiB
Dart

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/search/search_screen.dart';
import 'package:pipelines_app/ui/layout/app_list_row.dart';
class ImmediateGateway implements ApiGateway {
ImmediateGateway(this.reply);
final JsonMap Function(String query, String kind) reply;
final calls = <JsonMap>[];
/// The `kind: chat` searches sent beside the others.
final chatCalls = <JsonMap>[];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
final search = Map<String, dynamic>.from(fields['search'] as Map);
(search['kind'] == 'chat' ? chatCalls : calls).add(Map.of(fields));
return reply(search['query'] as String, search['kind'] as String);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
class FailingThenSucceedingGateway implements ApiGateway {
FailingThenSucceedingGateway(this.success);
final JsonMap success;
var calls = 0;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
final search = fields['search'];
if (search is Map && search['kind'] == 'chat') return success;
calls++;
if (calls == 1) throw StateError('unknown command: search');
return success;
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
JsonMap hit({
required String kind,
required String target,
required Map<String, dynamic> payload,
}) => {'kind': kind, 'target': target, ...payload};
JsonMap response(List<JsonMap> results) => {
'search_results': {
'results': results,
'truncated': false,
'scanned_steps': 4,
'scanned_bytes': 512,
'cap': 50,
'scan_bound_bytes': 1 << 20,
},
};
final pipeline = hit(
kind: 'pipeline',
target: 'pipeline:7',
payload: {
'pipeline': {'id': 7, 'name': 'Release planner', 'version': 3, 'stages': 4},
},
);
final field = hit(
kind: 'field',
target: 'run:11:4:output',
payload: {
'matched_field': 'output',
'field': {
'run_id': 11,
'sequence': 4,
'field': 'output',
'snippet': 'the release is ready',
},
},
);
void useNarrowSurface(WidgetTester tester) {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
}
Widget largeText(Widget child) => MediaQuery(
data: const MediaQueryData(textScaler: TextScaler.linear(2)),
child: child,
);
Future<void> search(WidgetTester tester, String query) async {
await tester.enterText(find.byKey(const ValueKey('search.field')), query);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
}
void main() {
for (final width in [390.0, 1000.0]) {
testWidgets(
'Material search is native and usable at width $width and 2x text',
(tester) async {
useNarrowSurface(tester);
tester.view.physicalSize = Size(width, 844);
final gateway = ImmediateGateway((_, _) => response([pipeline, field]));
JsonMap? opened;
await tester.pumpWidget(
MaterialApp(
home: largeText(
SearchScreen(gateway: gateway, onOpen: (value) => opened = value),
),
),
);
expect(find.byType(SearchBar), findsNothing);
expect(find.byType(TextField), findsOneWidget);
expect(find.byKey(const ValueKey('search.kind')), findsOneWidget);
expect(find.byTooltip('Kind: All kinds'), findsOneWidget);
expect(gateway.calls, isEmpty);
await search(tester, 'release');
expect(gateway.calls.single, {
'search': {'query': 'release', 'kind': '', 'limit': 0},
});
expect(find.text('Release planner'), findsOneWidget);
expect(find.textContaining('the release is ready'), findsOneWidget);
expect(find.textContaining('4 results'), findsNothing);
expect(tester.takeException(), isNull);
await tester.tap(
find.byKey(const ValueKey('search.result.pipeline:7')),
);
expect(opened?['target'], 'pipeline:7');
expect(opened?['kind'], 'pipeline');
},
);
}
testWidgets('a read-only hit cannot be opened', (tester) async {
useNarrowSurface(tester);
final unknown = hit(
kind: 'future_kind',
target: 'widget:7',
payload: const {},
);
final gateway = ImmediateGateway((_, _) => response([unknown]));
JsonMap? opened;
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (value) => opened = value),
),
);
await search(tester, 'widget');
expect(find.textContaining('read-only'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('search.result.widget:7')));
expect(opened, isNull);
});
testWidgets('field keys select results, retain focus, and Enter opens', (
tester,
) async {
useNarrowSurface(tester);
final second = hit(
kind: 'field',
target: 'run:11:4:output',
payload: {
'matched_field': 'output',
'field': {
'run_id': 11,
'sequence': 4,
'field': 'output',
'snippet': 'the release is ready',
},
},
);
final gateway = ImmediateGateway((_, _) => response([pipeline, second]));
JsonMap? opened;
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (value) => opened = value),
),
);
await search(tester, 'release');
final fieldFinder = find.byKey(const ValueKey('search.field'));
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
expect(FocusManager.instance.primaryFocus?.debugLabel, 'omni-search');
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyP);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyN);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.tap(fieldFinder);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
expect(opened?['target'], 'run:11:4:output');
});
testWidgets(
'selection resets when result data changes and typing remains active',
(tester) async {
useNarrowSurface(tester);
final changed = hit(
kind: 'pipeline',
target: 'pipeline:7',
payload: {
'pipeline': {
'id': 7,
'name': 'Updated release',
'version': 4,
'stages': 5,
},
},
);
final gateway = ImmediateGateway(
(query, _) =>
response(query == 'updated' ? [changed, field] : [pipeline, field]),
);
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (_) {}),
),
);
await search(tester, 'release');
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.enterText(
find.byKey(const ValueKey('search.field')),
'updated',
);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('search.field')))
.controller!
.text,
'updated',
);
},
);
testWidgets(
'Enter submits with no results and targetless rows have unique keys',
(tester) async {
useNarrowSurface(tester);
tester.view.physicalSize = const Size(390, 2000);
final gateway = ImmediateGateway(
(query, _) => query == 'empty'
? response([])
: response([
hit(kind: 'future_kind', target: '', payload: const {}),
hit(kind: 'future_kind', target: '', payload: const {}),
]),
);
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (_) {}),
),
);
await search(tester, 'empty');
final callsBeforeSubmit = gateway.calls.length;
await tester.tap(find.byKey(const ValueKey('search.field')));
await tester.testTextInput.receiveAction(TextInputAction.search);
await tester.pumpAndSettle();
expect(gateway.calls.length, callsBeforeSubmit + 1);
await search(tester, 'rows');
expect(find.byKey(const ValueKey('search.result..0')), findsOneWidget);
expect(find.byKey(const ValueKey('search.result..1')), findsOneWidget);
expect(tester.takeException(), isNull);
},
);
testWidgets('an unsupported server shows its sentence and retry recovers', (
tester,
) async {
useNarrowSurface(tester);
final gateway = FailingThenSucceedingGateway(response([pipeline]));
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (_) {}),
),
);
await search(tester, 'release');
expect(
find.text(
'This server does not support search. Update the Pipelines server, then try again.',
),
findsOneWidget,
);
expect(find.byKey(const ValueKey('search.retry')), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('search.retry')));
await tester.pumpAndSettle();
expect(gateway.calls, 2);
expect(find.text('Release planner'), findsOneWidget);
expect(find.byKey(const ValueKey('search.retry')), findsNothing);
});
}