import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/search/data/search_repository.dart'; import 'package:pipelines_app/features/search/domain/search_models.dart'; class RecordingGateway implements ApiGateway { String? type; JsonMap fields = {}; JsonMap reply = {}; Object? failure; @override Future call(String type, [JsonMap fields = const {}]) async { this.type = type; this.fields = Map.of(fields); if (failure case final error?) throw error; return reply; } @override Stream get frames => const Stream.empty(); } void main() { test( 'search sends exactly search/query/kind/limit and parses bounds', () async { final gateway = RecordingGateway() ..reply = { 'search_results': { 'query': 'plan', 'kind': 'run', 'results': [ { 'kind': 'run', 'target': 'run:11', 'run': {'id': 11}, }, ], 'truncated': true, 'scanned_steps': 27, 'scanned_bytes': 4096, 'cap': 50, 'scan_bound_bytes': 1048576, }, }; final repository = SearchRepository(gateway); final response = await repository.search( query: 'plan', kind: SearchKind.run, ); expect(gateway.type, 'search'); expect(gateway.fields, { 'search': {'query': 'plan', 'kind': 'run', 'limit': 0}, }); expect(response, isNotNull); expect(response!.query, 'plan'); expect(response.kind, SearchKind.run); expect(response.results.single['target'], 'run:11'); expect(response.truncated, isTrue); expect(response.scannedSteps, 27); expect(response.scannedBytes, 4096); expect(response.cap, 50); expect(response.scanBoundBytes, 1048576); }, ); test('all-kinds search still sends the empty kind field', () async { final gateway = RecordingGateway() ..reply = { 'search_results': {'results': []}, }; await SearchRepository(gateway).search(query: 'plan', kind: SearchKind.all); expect(gateway.fields, { 'search': {'query': 'plan', 'kind': '', 'limit': 0}, }); }); test( 'a reply without search_results is null, not an empty success', () async { final gateway = RecordingGateway()..reply = {'type': 'pong'}; final response = await SearchRepository( gateway, ).search(query: 'plan', kind: SearchKind.all); expect(response, isNull); }, ); test('server refusals propagate to the caller unchanged', () async { final gateway = RecordingGateway() ..failure = StateError('permission denied'); await expectLater( SearchRepository(gateway).search(query: 'plan', kind: SearchKind.all), throwsA( isA().having( (error) => error.message, 'message', 'permission denied', ), ), ); }); }