import 'package:cutdex/errors/app_failure.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex/shared/widgets/error_notification_host.dart'; import 'package:cutdex_ui/icons.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { Future mount(WidgetTester tester) async { await tester.pumpWidget( MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, builder: (context, child) => ErrorNotificationHost(child: child!), home: const Scaffold(body: Text('Workspace')), ), ); return tester.state( find.byType(ErrorNotificationHost), ); } testWidgets('top error copies complete details and closes', (tester) async { String? copied; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method == 'Clipboard.setData') { copied = (call.arguments as Map)['text'] as String; } return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, null, ), ); final host = await mount(tester); host.showFailure( const AppFailure( AppErrorCode.resourceDownloadHttpFailed, httpStatusCode: 503, diagnostic: 'service unavailable', ), 'Download failed', ); await tester.pump(); final notice = find.byKey(const Key('global-error-notification')); expect(tester.getRect(notice).top, 48); expect(find.byType(SnackBar), findsNothing); await tester.tap(find.byKey(const Key('copy-error-button'))); await tester.pump(); expect(copied, contains('Download failed')); expect(copied, contains('code: resourceDownloadHttpFailed')); expect(copied, contains('HTTP: 503')); expect(copied, contains('service unavailable')); expect(find.byIcon(AppIcons.check), findsOneWidget); await tester.tap(find.byKey(const Key('close-error-button'))); await tester.pump(); expect(notice, findsNothing); }); testWidgets('errors queue and pause dismissal while hovered', (tester) async { final host = await mount(tester); host.show(const ErrorNotification(content: Text('First'))); host.show(const ErrorNotification(content: Text('Second'))); await tester.pump(); expect(find.text('First'), findsOneWidget); expect(find.text('Second'), findsNothing); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); await mouse.moveTo( tester.getCenter(find.byKey(const Key('global-error-notification'))), ); await tester.pump(const Duration(seconds: 9)); expect(find.text('First'), findsOneWidget); await mouse.moveTo(const Offset(1, 500)); await tester.pump(const Duration(seconds: 8)); expect(find.text('Second'), findsOneWidget); await tester.pump(const Duration(seconds: 8)); expect(find.byKey(const Key('global-error-notification')), findsNothing); await mouse.removePointer(); }); testWidgets('error remains above an open dialog', (tester) async { final host = await mount(tester); final context = tester.element(find.text('Workspace')); showDialog( context: context, builder: (_) => const AlertDialog(content: Text('Dialog')), ); await tester.pumpAndSettle(); host.show(const ErrorNotification(content: Text('Dialog error'))); await tester.pump(); expect(find.text('Dialog'), findsOneWidget); await tester.tap(find.byKey(const Key('close-error-button'))); await tester.pump(); expect(find.text('Dialog error'), findsNothing); expect(find.text('Dialog'), findsOneWidget); }); testWidgets('top message keeps undo available above dialogs', (tester) async { final host = await mount(tester); showDialog( context: tester.element(find.text('Workspace')), builder: (_) => const AlertDialog(content: Text('Dialog')), ); await tester.pumpAndSettle(); var restored = false; host.showMessage( content: const Text('Project removed'), actionLabel: 'Undo', onAction: () => restored = true, ); await tester.pump(); final notice = find.byKey(const Key('global-message-notification')); expect(tester.getRect(notice).top, 48); expect(find.byType(SnackBar), findsNothing); expect(find.byKey(const Key('copy-error-button')), findsNothing); await tester.tap(find.text('Undo')); await tester.pump(); expect(restored, isTrue); expect(notice, findsNothing); expect(find.text('Dialog'), findsOneWidget); }); testWidgets('messages and errors share a queue and dismiss automatically', ( tester, ) async { final host = await mount(tester); host.showMessage(content: const Text('Task started')); host.show(const ErrorNotification(content: Text('Task failed'))); host.showMessage(content: const Text('Token copied')); await tester.pump(); expect(find.text('Task started'), findsOneWidget); expect(find.text('Task failed'), findsNothing); await tester.pump(const Duration(seconds: 4)); expect(find.text('Task started'), findsNothing); expect(find.text('Task failed'), findsOneWidget); await tester.pump(const Duration(seconds: 8)); expect(find.text('Task failed'), findsNothing); expect(find.text('Token copied'), findsOneWidget); await tester.tap(find.byKey(const Key('close-message-button'))); await tester.pump(); expect(find.text('Token copied'), findsNothing); }); }