Files
pipelines/app/test/features/search/search_screen_states_test.dart
T

149 lines
4.6 KiB
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/search_screen.dart';
class RecordingGateway implements ApiGateway {
RecordingGateway(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();
}
JsonMap response(
List<JsonMap> results, {
bool truncated = false,
int scannedSteps = 2,
int scannedBytes = 128,
}) => {
'search_results': {
'results': results,
'truncated': truncated,
'scanned_steps': scannedSteps,
'scanned_bytes': scannedBytes,
'cap': 50,
'scan_bound_bytes': 1 << 20,
},
};
final pipeline = {
'kind': 'pipeline',
'target': 'pipeline:7',
'pipeline': {'id': 7, 'name': 'Release planner', 'version': 3, 'stages': 4},
};
Widget material(Widget child) => MaterialApp(home: child);
void main() {
testWidgets(
'a fresh screen sends no request and idle is not an empty result',
(tester) async {
final gateway = RecordingGateway((_, _) => response([pipeline]));
await tester.pumpWidget(
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
);
expect(gateway.calls, isEmpty);
expect(
find.text('Search pipelines, runs and artifacts.'),
findsOneWidget,
);
expect(find.textContaining('No matches'), findsNothing);
},
);
testWidgets('an empty server result shows the query-specific empty copy', (
tester,
) async {
final gateway = RecordingGateway((_, _) => response(const []));
await tester.pumpWidget(
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
);
await tester.enterText(find.byKey(const ValueKey('search.field')), 'zzz');
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(gateway.calls, hasLength(1));
expect(find.text('No matches for "zzz".'), findsOneWidget);
expect(find.text('Searching…'), findsNothing);
final message = tester.getRect(find.text('No matches for "zzz".'));
expect(message.top, lessThan(380));
});
testWidgets(
'a truncated empty reply keeps the server scan warning on screen',
(tester) async {
final gateway = RecordingGateway(
(_, _) => response(
const [],
truncated: true,
scannedSteps: 2000,
scannedBytes: 1048576,
),
);
await tester.pumpWidget(
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
);
await tester.enterText(
find.byKey(const ValueKey('search.field')),
'needle',
);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(
find.text('No matches for "needle" within the server\'s scan limit.'),
findsOneWidget,
);
expect(find.textContaining('Results truncated'), findsOneWidget);
expect(find.textContaining('scanned 2000 steps'), findsOneWidget);
expect(find.textContaining('scan limit 1.0 MiB'), findsOneWidget);
},
);
testWidgets('changing Kind clears a typed token and requests the new kind', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final gateway = RecordingGateway((_, _) => response([pipeline]));
await tester.pumpWidget(
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
);
await tester.enterText(
find.byKey(const ValueKey('search.field')),
'plan t:run',
);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect((gateway.calls.single['search'] as Map)['kind'], 'run');
await tester.tap(find.byKey(const ValueKey('search.kind')));
await tester.pumpAndSettle();
await tester.tap(find.text('Pipelines').last);
await tester.pumpAndSettle();
final field = tester.widget<SearchBar>(
find.byKey(const ValueKey('search.field')),
);
expect(field.controller!.text.trim(), 'plan');
expect(field.controller!.text, isNot(contains('t:run')));
expect((gateway.calls.last['search'] as Map)['kind'], 'pipeline');
});
}