import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:lifetimer/features/auth/application/auth_controller.dart'; import 'package:lifetimer/features/auth/presentation/auth_gate.dart'; import 'package:lifetimer/features/auth/presentation/auth_showcase_screen.dart'; import 'package:lifetimer/features/onboarding/presentation/onboarding_intro_screen.dart'; import 'package:lifetimer/features/onboarding/application/onboarding_controller.dart'; import 'package:lifetimer/data/models/user_model.dart'; import 'package:lifetimer/data/repositories/auth_repository.dart'; import 'package:lifetimer/core/utils/unit_conversion_utils.dart'; class MockAuthRepository extends AuthRepository { bool _isAuthenticated = false; MockAuthRepository() : super(null); @override User? get currentUser => _isAuthenticated ? TestData.createTestUser() : null; @override Stream get authStateChanges => Stream.value(currentUser); @override bool get isAuthenticated => _isAuthenticated; @override String? get currentUserId => currentUser?.id; @override Future signInWithEmail(String email, String password) async {} @override Future signUpWithEmail(String email, String password, String username, {double? heightCm, double? weightKg, int? age, Gender? gender, HeightUnit? heightUnit, WeightUnit? weightUnit}) async {} @override Future signInWithGoogle() async {} Future signInWithApple() async { // Mock implementation } @override Future signOut() async {} @override Future resetPassword(String email) async {} @override Future isSessionValid() async => true; @override Future refreshSession() async {} @override Future updateProfile({ String? username, String? bio, String? avatarUrl, bool? isPublicProfile, double? heightCm, double? weightKg, int? age, Gender? gender, HeightUnit? heightUnit, WeightUnit? weightUnit, }) async {} @override void dispose() {} } class MockOnboardingController extends OnboardingController { MockOnboardingController() : super(); @override Future loadOnboardingStatus() async { // Do nothing in test } } class TestData { static User createTestUser() { return User( id: 'test-user-id', username: 'testuser', email: 'test@example.com', createdAt: DateTime.now(), updatedAt: DateTime.now(), ); } } void main() { group('AuthGate Widget', () { testWidgets('should continue to onboarding when backend is unavailable and user is not authenticated', (WidgetTester tester) async { await tester.pumpWidget( ProviderScope( overrides: [ authRepositoryProvider.overrideWithValue(MockAuthRepository()), onboardingControllerProvider.overrideWith((ref) => MockOnboardingController()), ], child: MaterialApp( home: const AuthGate(), builder: (context, child) => MediaQuery( data: const MediaQueryData(size: Size(800, 600)), child: child!, ), ), ), ); await tester.pumpAndSettle(); expect(find.byType(OnboardingIntroScreen), findsOneWidget); expect(find.byType(AuthShowcaseScreen), findsNothing); }); testWidgets('should show OnboardingIntroScreen when user is authenticated', (WidgetTester tester) async { final mockRepo = MockAuthRepository(); mockRepo._isAuthenticated = true; await tester.pumpWidget( ProviderScope( overrides: [ authRepositoryProvider.overrideWithValue(mockRepo), onboardingControllerProvider.overrideWith((ref) => MockOnboardingController()), ], child: MaterialApp( home: const AuthGate(), builder: (context, child) => MediaQuery( data: const MediaQueryData(size: Size(800, 600)), child: child!, ), ), ), ); await tester.pumpAndSettle(); expect(find.byType(OnboardingIntroScreen), findsOneWidget); expect(find.byType(AuthShowcaseScreen), findsNothing); }); }); }