forked from eatvio/eatvio_app
feat(diary): tests
This commit is contained in:
262
test/core/interceptors/offline_error_interceptor_test.dart
Normal file
262
test/core/interceptors/offline_error_interceptor_test.dart
Normal file
@@ -0,0 +1,262 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/interceptors/offline_error_interceptor.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('OfflineOperationException', () {
|
||||
test('stores fields correctly', () {
|
||||
const e = OfflineOperationException(
|
||||
message: 'Test message',
|
||||
code: 'test_code',
|
||||
retryable: true,
|
||||
);
|
||||
expect(e.message, 'Test message');
|
||||
expect(e.code, 'test_code');
|
||||
expect(e.retryable, true);
|
||||
});
|
||||
|
||||
test('code can be null', () {
|
||||
const e = OfflineOperationException(message: 'msg', retryable: false);
|
||||
expect(e.code, isNull);
|
||||
});
|
||||
|
||||
test('toString contains all fields', () {
|
||||
const e = OfflineOperationException(
|
||||
message: 'Test',
|
||||
code: 'code',
|
||||
retryable: true,
|
||||
);
|
||||
final str = e.toString();
|
||||
expect(str, contains('retryable: true'));
|
||||
expect(str, contains('code: code'));
|
||||
expect(str, contains('message: Test'));
|
||||
});
|
||||
|
||||
test('retryable false', () {
|
||||
const e = OfflineOperationException(
|
||||
message: 'fail',
|
||||
code: 'err',
|
||||
retryable: false,
|
||||
);
|
||||
expect(e.retryable, false);
|
||||
expect(e.toString(), contains('retryable: false'));
|
||||
});
|
||||
|
||||
test('implements Exception', () {
|
||||
const e = OfflineOperationException(message: 'm', retryable: true);
|
||||
expect(e, isA<Exception>());
|
||||
});
|
||||
});
|
||||
|
||||
group('OfflineErrorInterceptor', () {
|
||||
late OfflineErrorInterceptor interceptor;
|
||||
late _TestErrorHandler handler;
|
||||
|
||||
setUp(() {
|
||||
interceptor = OfflineErrorInterceptor();
|
||||
handler = _TestErrorHandler();
|
||||
});
|
||||
|
||||
test('wraps connectionTimeout as retryable OfflineOperationException', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.connectionTimeout,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
expect(handler.nextCalled, true);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
expect(wrappedError.code, 'connection_error');
|
||||
});
|
||||
|
||||
test('wraps receiveTimeout as retryable', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.receiveTimeout,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
expect(handler.nextCalled, true);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
});
|
||||
|
||||
test('wraps sendTimeout as retryable', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.sendTimeout,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
});
|
||||
|
||||
test('wraps connectionError as retryable', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.connectionError,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
});
|
||||
|
||||
test('wraps 5xx as retryable server error', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 500,
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
expect(wrappedError.code, 'server_error');
|
||||
});
|
||||
|
||||
test('wraps 503 as retryable server error', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 503,
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
});
|
||||
|
||||
test('parses 4xx error envelope with retryable=false', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 400,
|
||||
data: {
|
||||
'message': 'Bad request',
|
||||
'error': {
|
||||
'code': 'validation_error',
|
||||
'message': 'Invalid field',
|
||||
'retryable': false,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, false);
|
||||
expect(wrappedError.code, 'validation_error');
|
||||
expect(wrappedError.message, 'Invalid field');
|
||||
});
|
||||
|
||||
test('parses 4xx error envelope with retryable=true', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 429,
|
||||
data: {
|
||||
'message': 'Rate limited',
|
||||
'error': {
|
||||
'code': 'rate_limit',
|
||||
'message': 'Too many requests',
|
||||
'retryable': true,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, true);
|
||||
});
|
||||
|
||||
test('passes through 4xx without error envelope', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 404,
|
||||
data: 'Not found',
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
expect(handler.nextCalled, true);
|
||||
// Should pass through as-is, no OfflineOperationException wrapping
|
||||
expect(handler.lastError!.error, isNot(isA<OfflineOperationException>()));
|
||||
});
|
||||
|
||||
test('passes through when response is null', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.unknown,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
expect(handler.nextCalled, true);
|
||||
});
|
||||
|
||||
test('parses envelope with missing retryable field (defaults to false)', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 422,
|
||||
data: {
|
||||
'message': 'Unprocessable',
|
||||
'error': {
|
||||
'code': 'validation',
|
||||
'message': 'Field required',
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
final wrappedError = handler.lastError!.error;
|
||||
expect(wrappedError, isA<OfflineOperationException>());
|
||||
expect((wrappedError as OfflineOperationException).retryable, false);
|
||||
});
|
||||
|
||||
test('handles 4xx with error field but malformed data', () {
|
||||
final err = DioException(
|
||||
type: DioExceptionType.badResponse,
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/test'),
|
||||
statusCode: 400,
|
||||
data: {
|
||||
'error': 'just a string, not a map',
|
||||
},
|
||||
),
|
||||
);
|
||||
interceptor.onError(err, handler);
|
||||
expect(handler.nextCalled, true);
|
||||
// Error caught, passed through as-is
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Custom test handler that captures the error without throwing
|
||||
class _TestErrorHandler extends ErrorInterceptorHandler {
|
||||
DioException? lastError;
|
||||
bool nextCalled = false;
|
||||
|
||||
@override
|
||||
void next(DioException err) {
|
||||
lastError = err;
|
||||
nextCalled = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user