2026-02-25 14:59:20 +08:00
|
|
|
import 'package:bloc_test/bloc_test.dart';
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
import 'package:mocktail/mocktail.dart';
|
|
|
|
|
import 'package:social_app/features/auth/data/auth_repository.dart';
|
|
|
|
|
import 'package:social_app/features/auth/data/models/auth_response.dart';
|
|
|
|
|
import 'package:social_app/features/auth/presentation/bloc/auth_bloc.dart';
|
|
|
|
|
import 'package:social_app/features/auth/presentation/bloc/auth_event.dart';
|
|
|
|
|
import 'package:social_app/features/auth/presentation/bloc/auth_state.dart';
|
|
|
|
|
|
|
|
|
|
class MockAuthRepository extends Mock implements AuthRepository {}
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
late AuthBloc authBloc;
|
|
|
|
|
late MockAuthRepository mockRepository;
|
|
|
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
|
mockRepository = MockAuthRepository();
|
|
|
|
|
authBloc = AuthBloc(mockRepository);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
|
authBloc.close();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
group('AuthBloc', () {
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits [AuthLoading, AuthUnauthenticated] when AuthStarted and no refresh token',
|
|
|
|
|
build: () {
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.getRefreshToken(),
|
|
|
|
|
).thenAnswer((_) async => null);
|
|
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
act: (bloc) => bloc.add(AuthStarted()),
|
2026-03-18 13:35:25 +08:00
|
|
|
expect: () => [
|
|
|
|
|
AuthLoading(),
|
|
|
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
|
|
|
|
],
|
2026-02-25 14:59:20 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits [AuthLoading, AuthAuthenticated] when AuthStarted with valid refresh token',
|
|
|
|
|
build: () {
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.getRefreshToken(),
|
|
|
|
|
).thenAnswer((_) async => 'valid_refresh');
|
2026-02-26 14:28:58 +08:00
|
|
|
when(() => mockRepository.refreshSession('valid_refresh')).thenAnswer(
|
2026-02-25 14:59:20 +08:00
|
|
|
(_) async => AuthResponse(
|
|
|
|
|
accessToken: 'new_access',
|
|
|
|
|
refreshToken: 'new_refresh',
|
|
|
|
|
expiresIn: 3600,
|
|
|
|
|
tokenType: 'bearer',
|
|
|
|
|
user: const AuthUser(id: '123', email: 'test@example.com'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
act: (bloc) => bloc.add(AuthStarted()),
|
|
|
|
|
expect: () => [AuthLoading(), isA<AuthAuthenticated>()],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits [AuthLoading, AuthUnauthenticated] when refresh token expired',
|
|
|
|
|
build: () {
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.getRefreshToken(),
|
|
|
|
|
).thenAnswer((_) async => 'expired_refresh');
|
|
|
|
|
when(
|
2026-02-26 14:28:58 +08:00
|
|
|
() => mockRepository.refreshSession('expired_refresh'),
|
2026-02-25 14:59:20 +08:00
|
|
|
).thenThrow(Exception('Invalid refresh token'));
|
2026-03-18 13:35:25 +08:00
|
|
|
when(
|
|
|
|
|
() => mockRepository.clearSessionLocalOnly(),
|
|
|
|
|
).thenAnswer((_) async {});
|
|
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
act: (bloc) => bloc.add(AuthStarted()),
|
|
|
|
|
expect: () => [
|
|
|
|
|
AuthLoading(),
|
|
|
|
|
const AuthUnauthenticated(
|
|
|
|
|
reason: AuthUnauthenticatedReason.startupRecoveryFailed,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits startupRecoveryFailed when storage read throws',
|
|
|
|
|
build: () {
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.getRefreshToken(),
|
|
|
|
|
).thenThrow(Exception('storage failed'));
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.clearSessionLocalOnly(),
|
|
|
|
|
).thenAnswer((_) async {});
|
2026-02-25 14:59:20 +08:00
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
act: (bloc) => bloc.add(AuthStarted()),
|
2026-03-18 13:35:25 +08:00
|
|
|
expect: () => [
|
|
|
|
|
AuthLoading(),
|
|
|
|
|
const AuthUnauthenticated(
|
|
|
|
|
reason: AuthUnauthenticatedReason.startupRecoveryFailed,
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-02-25 14:59:20 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits [AuthAuthenticated] when AuthLoggedIn',
|
|
|
|
|
build: () => authBloc,
|
|
|
|
|
act: (bloc) => bloc.add(
|
|
|
|
|
AuthLoggedIn(
|
|
|
|
|
user: const AuthUser(id: '1', email: 'test@example.com'),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
expect: () => [isA<AuthAuthenticated>()],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits [AuthUnauthenticated] when AuthLoggedOut',
|
|
|
|
|
build: () {
|
2026-02-26 14:28:58 +08:00
|
|
|
when(() => mockRepository.deleteSession()).thenAnswer((_) async {});
|
2026-02-25 14:59:20 +08:00
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
seed: () => AuthAuthenticated(
|
|
|
|
|
user: const AuthUser(id: '1', email: 'test@example.com'),
|
|
|
|
|
),
|
|
|
|
|
act: (bloc) => bloc.add(AuthLoggedOut()),
|
2026-03-18 13:35:25 +08:00
|
|
|
expect: () => [
|
|
|
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
|
|
|
'emits expired unauthenticated when session invalidated',
|
|
|
|
|
build: () {
|
|
|
|
|
when(
|
|
|
|
|
() => mockRepository.clearSessionLocalOnly(),
|
|
|
|
|
).thenAnswer((_) async {});
|
|
|
|
|
return authBloc;
|
|
|
|
|
},
|
|
|
|
|
seed: () => AuthAuthenticated(
|
|
|
|
|
user: const AuthUser(id: '1', email: 'test@example.com'),
|
|
|
|
|
),
|
|
|
|
|
act: (bloc) => bloc.add(
|
|
|
|
|
const AuthSessionInvalidated(
|
|
|
|
|
source: AuthInvalidationSource.unauthorized401,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
expect: () => [
|
|
|
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.expired),
|
|
|
|
|
],
|
2026-02-25 14:59:20 +08:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|