b34697660d
- 新增 AuthSessionInvalidated 事件处理 token 失效场景 - ApiInterceptor 新增 authFailureCallback 单飞机制 - AuthBloc 区分 manual logout 与 auto expiry 语义 - 新增 startup recovery fallback 防止启动卡死 feat: 重构 Calendar DayWeek 视图事件布局引擎 - 新增 DayEventLayoutEngine 解耦事件计算与渲染 - 新增 DayTimelineMetrics 统一时间轴常量 - 新增 DayViewScale 支持捏合缩放 feat: 新增 Settings 页面共享 UI 组件 - 新增 BackTitlePageHeader 统一页面 header - 新增 DetailHeaderActionMenu 统一操作菜单 - 新增 DestructiveActionSheet 统一删除确认 - 新增 AppToggleSwitch 统一开关组件 feat: Chat UI Schema 支持导航操作 - 支持 navigation 类型 action 触发内部路由跳转 - 新增路径验证与参数处理 chore: 更新相关测试覆盖 auth 失效路径
153 lines
4.7 KiB
Dart
153 lines
4.7 KiB
Dart
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()),
|
|
expect: () => [
|
|
AuthLoading(),
|
|
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
|
|
],
|
|
);
|
|
|
|
blocTest<AuthBloc, AuthState>(
|
|
'emits [AuthLoading, AuthAuthenticated] when AuthStarted with valid refresh token',
|
|
build: () {
|
|
when(
|
|
() => mockRepository.getRefreshToken(),
|
|
).thenAnswer((_) async => 'valid_refresh');
|
|
when(() => mockRepository.refreshSession('valid_refresh')).thenAnswer(
|
|
(_) 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(
|
|
() => mockRepository.refreshSession('expired_refresh'),
|
|
).thenThrow(Exception('Invalid refresh token'));
|
|
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 {});
|
|
return authBloc;
|
|
},
|
|
act: (bloc) => bloc.add(AuthStarted()),
|
|
expect: () => [
|
|
AuthLoading(),
|
|
const AuthUnauthenticated(
|
|
reason: AuthUnauthenticatedReason.startupRecoveryFailed,
|
|
),
|
|
],
|
|
);
|
|
|
|
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: () {
|
|
when(() => mockRepository.deleteSession()).thenAnswer((_) async {});
|
|
return authBloc;
|
|
},
|
|
seed: () => AuthAuthenticated(
|
|
user: const AuthUser(id: '1', email: 'test@example.com'),
|
|
),
|
|
act: (bloc) => bloc.add(AuthLoggedOut()),
|
|
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),
|
|
],
|
|
);
|
|
});
|
|
}
|