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 = []; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add(Map.of(fields)); final search = Map.from(fields['search'] as Map); return reply(search['query'] as String, search['kind'] as String); } @override Stream get frames => const Stream.empty(); } class FailingThenSucceedingGateway implements ApiGateway { FailingThenSucceedingGateway(this.success); final JsonMap success; var calls = 0; @override Future call(String type, [JsonMap fields = const {}]) async { calls++; if (calls == 1) throw StateError('unknown command: search'); return success; } @override Stream get frames => const Stream.empty(); } JsonMap hit({ required String kind, required String target, required Map payload, }) => {'kind': kind, 'target': target, ...payload}; JsonMap response(List 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 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), 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); }); }