games_app/prediqo_mobile/test/notifications_section_test.dart

225 lines
6.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/core/config/app_config.dart';
import 'package:prediqo_mobile/core/di/injection.dart';
import 'package:prediqo_mobile/core/network/api_client.dart';
import 'package:prediqo_mobile/core/network/connectivity_controller.dart';
import 'package:prediqo_mobile/core/theme/app_theme.dart';
import 'package:prediqo_mobile/data/models/notification_models.dart';
import 'package:prediqo_mobile/data/repositories/notification_repository_impl.dart';
import 'package:prediqo_mobile/domain/repositories/notification_repository.dart';
import 'package:prediqo_mobile/features/notifications/presentation/notifications_screen.dart';
import 'package:prediqo_mobile/l10n/locale_controller.dart';
import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart';
import 'package:prediqo_mobile/shared/widgets/section_error.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'support/e2e_fixtures.dart';
SecurityNotificationModel _n(String id, {String title = 'Alert'}) =>
SecurityNotificationModel(
id: id,
eventType: 'login',
title: title,
body: 'Body $id',
createdAt: '2026-09-24T10:00:00.000Z',
);
class _ControllableNotifications implements NotificationRepository {
Completer<SecurityNotificationsPage>? hold;
SecurityNotificationsPage? cached;
Object? failWith;
var listCalls = 0;
@override
SecurityNotificationsPage? peekLast() => cached;
@override
SecurityNotificationsPage? peekFresh() => cached;
@override
void invalidate() => cached = null;
@override
Future<SecurityNotificationsPage> list({
int limit = 30,
int offset = 0,
bool unreadOnly = false,
bool force = false,
}) async {
listCalls++;
if (failWith != null) throw failWith!;
if (hold != null) return hold!.future;
final page = SecurityNotificationsPage(
items: [_n('n1', title: 'Welcome')],
total: 1,
unreadCount: 1,
);
cached = page;
return page;
}
@override
Future<void> markRead(String notificationId) async {}
@override
Future<void> markAllRead() async {}
}
Future<void> _boot(NotificationRepository repo) async {
configureE2eTestEnvironment();
if (sl.isRegistered<AppConfig>()) await sl.reset();
sl.registerSingleton<AppConfig>(
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
);
sl.registerSingleton<ConnectivityController>(ConnectivityController());
final locale = LocaleController();
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
await locale.load();
sl.registerSingleton<LocaleController>(locale);
sl.registerLazySingleton<ApiClient>(() => ApiClient(config: sl()));
sl.registerLazySingleton<NotificationRepository>(() => repo);
}
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) await sl.reset();
});
test('NotificationListStore peekLast under 100ms', () {
final store = NotificationListStore();
final sw = Stopwatch()..start();
for (var i = 0; i < 100; i++) {
store.peekLast();
store.peekFresh();
}
sw.stop();
expect(sw.elapsedMilliseconds, lessThan(100));
});
test('store TTL skips a second first-page fetch within window', () async {
final store = NotificationListStore();
var fetches = 0;
Future<SecurityNotificationsPage> fetch() async {
fetches++;
return SecurityNotificationsPage(
items: [_n('a')],
total: 1,
unreadCount: 1,
);
}
await store.get(fetch);
await store.get(fetch);
expect(fetches, 1);
expect(store.peekFresh(), isNotNull);
});
testWidgets('keeps AppBar + skeletons while list loads', (tester) async {
final hold = Completer<SecurityNotificationsPage>();
final repo = _ControllableNotifications()..hold = hold;
await _boot(repo);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.dark,
home: const NotificationsScreen(),
),
);
await tester.pump();
expect(find.text('Notifications'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsWidgets);
expect(find.byType(CircularProgressIndicator), findsNothing);
hold.complete(
SecurityNotificationsPage(
items: [_n('n1', title: 'New login')],
total: 1,
unreadCount: 1,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.text('New login'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsNothing);
});
testWidgets('paints cached list immediately then refreshes', (tester) async {
final repo = _ControllableNotifications()
..cached = SecurityNotificationsPage(
items: [_n('cached', title: 'Cached alert')],
total: 1,
unreadCount: 0,
);
repo.hold = Completer<SecurityNotificationsPage>();
await _boot(repo);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.dark,
home: const NotificationsScreen(),
),
);
await tester.pump();
expect(find.text('Cached alert'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsNothing);
expect(repo.listCalls, 1);
repo.hold!.complete(
SecurityNotificationsPage(
items: [_n('fresh', title: 'Fresh alert')],
total: 1,
unreadCount: 0,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.text('Fresh alert'), findsOneWidget);
});
testWidgets('SectionError keeps AppBar and retries once', (tester) async {
final repo = _ControllableNotifications()..failWith = Exception('offline');
await _boot(repo);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.dark,
home: const NotificationsScreen(),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.byType(SectionError), findsOneWidget);
expect(find.text('Notifications'), findsOneWidget);
final before = repo.listCalls;
repo.failWith = null;
repo.hold = Completer<SecurityNotificationsPage>();
final retry = find.descendant(
of: find.byType(SectionError),
matching: find.byType(IconButton),
);
await tester.tap(retry);
await tester.pump();
expect(repo.listCalls, before + 1);
repo.hold!.complete(
SecurityNotificationsPage(
items: [_n('ok', title: 'Recovered')],
total: 1,
unreadCount: 0,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.text('Recovered'), findsOneWidget);
expect(find.byType(SectionError), findsNothing);
});
}