|
| 1 | +import 'package:algolia_helper_flutter/src/exception.dart'; |
| 2 | +import 'package:algoliasearch/algoliasearch.dart' as algolia; |
| 3 | +import 'package:algolia_helper_flutter/src/service/algolia_client_extensions.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('SearchError', () { |
| 8 | + test('should preserve original exception as cause', () { |
| 9 | + final originalError = Exception('Original error message'); |
| 10 | + final searchError = SearchError( |
| 11 | + {'message': 'Search failed'}, |
| 12 | + 500, |
| 13 | + originalError, |
| 14 | + ); |
| 15 | + |
| 16 | + expect(searchError.error, {'message': 'Search failed'}); |
| 17 | + expect(searchError.statusCode, 500); |
| 18 | + expect(searchError.cause, originalError); |
| 19 | + expect(identical(searchError.cause, originalError), true); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should allow null cause', () { |
| 23 | + final searchError = SearchError( |
| 24 | + {'message': 'Search failed'}, |
| 25 | + 404, |
| 26 | + ); |
| 27 | + |
| 28 | + expect(searchError.error, {'message': 'Search failed'}); |
| 29 | + expect(searchError.statusCode, 404); |
| 30 | + expect(searchError.cause, isNull); |
| 31 | + }); |
| 32 | + |
| 33 | + test('toString should include cause', () { |
| 34 | + final originalError = Exception('Network timeout'); |
| 35 | + final searchError = SearchError( |
| 36 | + {'message': 'Request failed'}, |
| 37 | + 503, |
| 38 | + originalError, |
| 39 | + ); |
| 40 | + |
| 41 | + final stringRepresentation = searchError.toString(); |
| 42 | + expect(stringRepresentation, contains('error:')); |
| 43 | + expect(stringRepresentation, contains('statusCode:')); |
| 44 | + expect(stringRepresentation, contains('cause:')); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + group('launderException', () { |
| 49 | + // Create a dummy SearchClient instance for testing the extension method |
| 50 | + late algolia.SearchClient client; |
| 51 | + |
| 52 | + setUp(() { |
| 53 | + // Create a minimal SearchClient instance |
| 54 | + // Note: In real usage, this would be properly configured |
| 55 | + client = algolia.SearchClient( |
| 56 | + appId: 'testAppId', |
| 57 | + apiKey: 'testApiKey', |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should preserve non-AlgoliaApiException as cause', () { |
| 62 | + final originalError = Exception('Custom error'); |
| 63 | + final launderedError = client.launderException(originalError); |
| 64 | + |
| 65 | + expect(launderedError, isA<SearchError>()); |
| 66 | + final searchError = launderedError as SearchError; |
| 67 | + expect(searchError.cause, originalError); |
| 68 | + expect(identical(searchError.cause, originalError), true); |
| 69 | + expect(searchError.error['message'], contains('Custom error')); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should preserve AlgoliaApiException as cause', () { |
| 73 | + final originalError = algolia.AlgoliaApiException( |
| 74 | + 401, |
| 75 | + 'Invalid API key', |
| 76 | + ); |
| 77 | + final launderedError = client.launderException(originalError); |
| 78 | + |
| 79 | + expect(launderedError, isA<SearchError>()); |
| 80 | + final searchError = launderedError as SearchError; |
| 81 | + expect(searchError.cause, originalError); |
| 82 | + expect(identical(searchError.cause, originalError), true); |
| 83 | + expect(searchError.statusCode, 401); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should preserve any object type as cause', () { |
| 87 | + final originalError = StateError('Invalid state'); |
| 88 | + final launderedError = client.launderException(originalError); |
| 89 | + |
| 90 | + expect(launderedError, isA<SearchError>()); |
| 91 | + final searchError = launderedError as SearchError; |
| 92 | + expect(searchError.cause, isA<StateError>()); |
| 93 | + expect(identical(searchError.cause, originalError), true); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should preserve string errors as cause', () { |
| 97 | + const originalError = 'Something went wrong'; |
| 98 | + final launderedError = client.launderException(originalError); |
| 99 | + |
| 100 | + expect(launderedError, isA<SearchError>()); |
| 101 | + final searchError = launderedError as SearchError; |
| 102 | + expect(searchError.cause, originalError); |
| 103 | + expect(searchError.error['message'], contains(originalError)); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + group('AlgoliaExceptionExt', () { |
| 108 | + test('toSearchError should preserve original AlgoliaApiException', () { |
| 109 | + final apiException = algolia.AlgoliaApiException( |
| 110 | + 404, |
| 111 | + 'IndexNotFound', |
| 112 | + ); |
| 113 | + |
| 114 | + final searchError = apiException.toSearchError(); |
| 115 | + |
| 116 | + expect(searchError.statusCode, 404); |
| 117 | + expect(searchError.cause, apiException); |
| 118 | + expect(identical(searchError.cause, apiException), true); |
| 119 | + expect(searchError.error['message'], contains('IndexNotFound')); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
0 commit comments