feat(test): 70% tests coverage

This commit is contained in:
misha
2026-05-06 05:27:25 +03:00
parent 56313a4fcc
commit fe243965e3
12 changed files with 862 additions and 87 deletions

View File

@@ -110,5 +110,92 @@ void main() {
final event = SentryEvent(throwable: const SocketException('x'));
expect(ErrorReportPolicy.allowSentryEvent(event, hint), false);
});
test('allows event when throwable is null', () {
expect(ErrorReportPolicy.allowSentryEvent(SentryEvent(), hint), true);
});
test('delegates non-Dio throwable to shouldReport', () {
final event = SentryEvent(throwable: StateError('x'));
expect(ErrorReportPolicy.allowSentryEvent(event, hint), true);
});
test('drops cancelled Dio from SDK', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.cancel,
);
expect(ErrorReportPolicy.allowSentryEvent(SentryEvent(throwable: e), hint), false);
});
test('drops sendTimeout Dio from SDK', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.sendTimeout,
);
expect(ErrorReportPolicy.allowSentryEvent(SentryEvent(throwable: e), hint), false);
});
test('drops receiveTimeout Dio from SDK', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.receiveTimeout,
);
expect(ErrorReportPolicy.allowSentryEvent(SentryEvent(throwable: e), hint), false);
});
test('allows badResponse Dio from SDK when statusCode is missing', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.badResponse,
);
expect(ErrorReportPolicy.allowSentryEvent(SentryEvent(throwable: e), hint), true);
});
});
group('ErrorReportPolicy.shouldReport extra Dio branches', () {
test('returns true for Dio unknown when inner error is not SocketException', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.unknown,
error: 'not a socket',
);
expect(ErrorReportPolicy.shouldReport(e), true);
});
test('returns false for Dio unknown when inner error is SocketException', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.unknown,
error: const SocketException('offline'),
);
expect(ErrorReportPolicy.shouldReport(e), false);
});
test('returns true for badCertificate when inner error is not SocketException', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.badCertificate,
error: 'cert problem',
);
expect(ErrorReportPolicy.shouldReport(e), true);
});
test('returns false for badCertificate when inner error is SocketException', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.badCertificate,
error: const SocketException('bad'),
);
expect(ErrorReportPolicy.shouldReport(e), false);
});
test('returns true for badResponse without HTTP status', () {
final e = DioException(
requestOptions: RequestOptions(path: '/x'),
type: DioExceptionType.badResponse,
);
expect(ErrorReportPolicy.shouldReport(e), true);
});
});
}

View File

@@ -0,0 +1,55 @@
import 'package:eatvio_app/core/error_reporting/error_reporter.dart';
import 'package:eatvio_app/core/error_reporting/report_from.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:flutter_test/flutter_test.dart';
class _RecordingReporter implements ErrorReporter {
Object? lastError;
StackTrace? lastStack;
String? lastOrigin;
@override
Future<void> report(
Object error,
StackTrace? stackTrace, {
String? origin,
Map<String, Object?>? extras,
}) async {
lastError = error;
lastStack = stackTrace;
lastOrigin = origin;
}
}
class _OriginMarker {}
void main() {
late _RecordingReporter reporter;
setUp(() async {
await sl.reset();
reporter = _RecordingReporter();
sl.registerSingleton<ErrorReporter>(reporter);
});
tearDown(() async {
await sl.reset();
});
test('reportFrom forwards to registered ErrorReporter with origin from context runtimeType', () async {
final err = StateError('bad');
final st = StackTrace.current;
await reportFrom(err, st, _OriginMarker());
expect(reporter.lastError, same(err));
expect(reporter.lastStack, st);
expect(reporter.lastOrigin, '_OriginMarker');
});
test('reportFrom completes when ErrorReporter is not registered (NoOp path)', () async {
await sl.reset();
await reportFrom(Exception('x'), null, Object());
});
}

View File

@@ -0,0 +1,16 @@
import 'package:eatvio_app/core/error_reporting/sentry_error_reporter.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('SentryErrorReporter', () {
test('report returns immediately when ErrorReportPolicy filters the error', () async {
final reporter = SentryErrorReporter();
await reporter.report(
'user-facing string',
StackTrace.empty,
origin: 'unit',
);
// Does not throw; policy drops String errors before Sentry.
});
});
}

View File

@@ -6,23 +6,58 @@ import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/custom_popup.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
class _FakeAssetBundle extends CachingAssetBundle {
_FakeAssetBundle(this.assets);
final Map<String, String> assets;
@override
Future<String> loadString(String key, {bool cache = true}) async {
final value = assets[key];
if (value == null) {
throw FlutterError('Asset not found: $key');
}
return value;
}
@override
Future<ByteData> load(String key) async {
final value = assets[key];
if (value == null) {
throw FlutterError('Asset not found: $key');
}
final bytes = Uint8List.fromList(value.codeUnits);
return ByteData.sublistView(bytes);
}
}
void main() {
group('CustomPopup smoke', () {
setUpAll(() async {
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
});
const fakeSvg = '''
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10">
<rect width="10" height="10" fill="red"/>
</svg>
''';
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
final fakeBundle = _FakeAssetBundle({
'assets/icons/copy2.svg': fakeSvg,
'assets/icons/share2.svg': fakeSvg,
'assets/icons/edit_paint.svg': fakeSvg,
'assets/icons/red_trash.svg': fakeSvg,
'assets/icons/complain.svg': fakeSvg,
});
Widget wrap(Widget child) {
return MaterialApp(
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
Widget wrap(Widget child) {
return DefaultAssetBundle(
bundle: fakeBundle,
child: MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
@@ -31,87 +66,52 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
home: Scaffold(body: child),
);
}
home: Scaffold(body: Center(child: child)),
),
);
}
testWidgets('renders custom popup for author', (tester) async {
await tester.pumpWidget(wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user')));
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
testWidgets('CustomPopup for author shows edit and delete actions for posts', (tester) async {
await tester.pumpWidget(
wrap(
const CustomPopup(
isAuthor: true,
complainedContentType: ComplainedContent.posts,
path: 'posts/1',
id: 1,
userSlug: 'author',
),
),
);
testWidgets('renders custom popup for non-author', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.posts)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
await tester.tap(find.byIcon(Icons.more_horiz_rounded));
await tester.pumpAndSettle();
testWidgets('renders custom popup for non-author with recipe', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.recipe)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
expect(find.text('Скопировать ссылку'), findsOneWidget);
expect(find.text('Поделиться'), findsOneWidget);
expect(find.text('Редактировать'), findsOneWidget);
expect(find.text('Удалить'), findsOneWidget);
});
testWidgets('renders custom popup for non-author with diet', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.diet)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
testWidgets('CustomPopup for non-author shows complain and block actions', (tester) async {
await tester.pumpWidget(
wrap(
const CustomPopup(
isAuthor: false,
complainedContentType: ComplainedContent.recipe,
path: 'recipes/slug',
slug: 'slug',
userSlug: 'user-2',
),
),
);
testWidgets('renders custom popup for non-author with plan', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.plan)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
await tester.tap(find.byIcon(Icons.more_horiz_rounded));
await tester.pumpAndSettle();
testWidgets('renders custom popup for author with posts', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.posts)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
testWidgets('renders custom popup for author with recipe', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.recipe, slug: 'test-recipe')),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
testWidgets('renders custom popup for author with diet', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.diet, id: 1)),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
testWidgets('renders custom popup for author with plan', (tester) async {
await tester.pumpWidget(
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.plan, slug: 'test-plan')),
);
await tester.pump();
expect(find.byType(CustomPopup), findsOneWidget);
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
});
expect(find.text('Скопировать ссылку'), findsOneWidget);
expect(find.text('Поделиться'), findsOneWidget);
expect(find.text('Пожаловаться'), findsOneWidget);
expect(find.text('Заблокировать автора'), findsOneWidget);
});
}