Files
pipelines/app/test/features/search/search_screen_test.dart
T
alan 51140cd3b7 feat(app): add native omni-search screen over the search gateway
Pure request/parse/row logic in lib/features/search/domain, a
gateway-backed repository, and Material plus Cupertino bodies.
The header and rows share one scroll view, and the server's
truncation, scan and cap facts stay visible even when a bounded
scan returns no matches.

Tests cover the exact search envelope, token parsing, stale replies,
retry, truncation bounds and platform layouts. A Web-only fixture
and CDP probe support rendered verification; the screen is not yet
registered in the shell.
2026-10-21 16:41:30 +01:00

224 lines
6.4 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/search/domain/search_models.dart';
import 'package:pipelines_app/features/search/search_screen.dart';
class ImmediateGateway implements ApiGateway {
ImmediateGateway(this.reply);
final JsonMap Function(String query, String kind) reply;
final calls = <JsonMap>[];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add(Map.of(fields));
final search = Map<String, dynamic>.from(fields['search'] as Map);
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 {
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,
cupertino: false,
),
),
),
);
expect(find.byType(SearchBar), findsOneWidget);
expect(find.byType(DropdownButton<SearchKind>), findsOneWidget);
expect(find.text('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(
'Cupertino search uses native controls and opens the real target',
(tester) async {
useNarrowSurface(tester);
final gateway = ImmediateGateway((_, _) => response([pipeline]));
JsonMap? opened;
await tester.pumpWidget(
CupertinoApp(
home: largeText(
SearchScreen(
gateway: gateway,
onOpen: (value) => opened = value,
cupertino: true,
),
),
),
);
expect(find.byType(CupertinoSearchTextField), findsOneWidget);
await search(tester, 'release');
expect(find.text('Release planner'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.tap(find.byKey(const ValueKey('search.result.pipeline:7')));
expect(opened?['target'], 'pipeline:7');
},
);
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,
cupertino: false,
),
),
);
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('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: (_) {}, cupertino: false),
),
);
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);
});
}