Files
jiu/client/test/auth_state_test.dart
T
wangjia 37112d6599 feat(client): 登录历史下拉、macOS 兼容修复、UI 优化
- 替换 flutter_secure_storage 为 shared_preferences,解决 macOS Keychain 签名报错
- 登录页门店编号/用户名支持历史下拉(最多5条,LRU 淘汰),使用 Overlay + CompositedTransformFollower 实现浮层
- logout 仅删除 auth token,保留登录历史
- AppShell 时钟抽为独立 _ClockWidget,避免每秒 setState 导致 TabBarView 叠影
- ShellRoute 子路由改用 NoTransitionPage,消除切换页面时的过渡动画叠影
- 新增 auth_state_test、auth_repository_test、login_screen_test 共 14 个单元/Widget 测试

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-05 01:21:40 +08:00

106 lines
3.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jiu_client/core/auth/auth_state.dart';
void main() {
setUp(() {
SharedPreferences.setMockInitialValues({});
});
group('AuthNotifier', () {
late AuthNotifier notifier;
setUp(() {
notifier = AuthNotifier();
});
test('initial state is not logged in and not initialized', () {
expect(notifier.state.isLoggedIn, false);
expect(notifier.state.initialized, false);
});
test('restore() sets initialized=true when no stored tokens', () async {
await notifier.restore();
expect(notifier.state.initialized, true);
expect(notifier.state.isLoggedIn, false);
});
test('restore() restores user from stored tokens', () async {
SharedPreferences.setMockInitialValues({
'access_token': 'test-access-token',
'refresh_token': 'test-refresh-token',
'username': 'admin',
'real_name': '管理员',
'hotel_no': 'H001',
'hotel_id': '1',
});
await notifier.restore();
expect(notifier.state.initialized, true);
expect(notifier.state.isLoggedIn, true);
expect(notifier.state.user!.username, 'admin');
expect(notifier.state.user!.hotelNo, 'H001');
expect(notifier.state.user!.accessToken, 'test-access-token');
});
test('login() saves user and updates state', () async {
const user = AuthUser(
accessToken: 'at',
refreshToken: 'rt',
username: 'admin',
realName: '管理员',
hotelNo: 'H001',
hotelId: 1,
);
await notifier.login(user);
expect(notifier.state.isLoggedIn, true);
expect(notifier.state.user!.username, 'admin');
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('access_token'), 'at');
expect(prefs.getString('refresh_token'), 'rt');
});
test('logout() removes auth keys and clears state', () async {
SharedPreferences.setMockInitialValues({
'access_token': 'token',
'refresh_token': 'rt',
'username': 'admin',
'login_history_hotels': ['H001'],
});
await notifier.restore();
expect(notifier.state.isLoggedIn, true);
await notifier.logout();
expect(notifier.state.isLoggedIn, false);
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('access_token'), null);
// Login history must NOT be cleared on logout
expect(prefs.getStringList('login_history_hotels'), ['H001']);
});
test('updateAccessToken() updates token without changing other fields',
() async {
await notifier.login(const AuthUser(
accessToken: 'old-token',
refreshToken: 'rt',
username: 'admin',
realName: '管理员',
hotelNo: 'H001',
hotelId: 1,
));
notifier.updateAccessToken('new-token');
expect(notifier.state.user!.accessToken, 'new-token');
expect(notifier.state.user!.refreshToken, 'rt');
expect(notifier.state.user!.username, 'admin');
});
});
}