Files
jiu/client/test/auth_state_test.dart
wangjia d717fb3735 feat(client): 全模块 API 对接 + 修复登陆跳转 + 菜单 UI 优化
API 对接:
- 入库/出库/库存/财务/往来单位/基础数据全部对接后端 REST API
- 新增 repositories、providers、models 层,统一分层架构
- auth 从 flutter_secure_storage 迁移到 shared_preferences

登陆跳转修复:
- 将 _RouterNotifier 提取为独立 Riverpod provider,appRouterProvider
  使用 ref.read 避免依赖链导致 router 重建后跳回 /login
- redirect 函数新增 initialized 守卫,防止 auth 未恢复时误重定向
- 添加调试日志(Router/Auth/ApiClient)定位 401 触发的 logout 链路

退出菜单 UI:
- 去掉 ListTile,改用 Row + 自定义 padding,文字左对齐
- MouseRegion + AnimatedContainer 实现 hover 高亮(普通项蓝底/退出红底)
- 菜单圆角 6px,elevation 8,分割线高度 1px

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-07 22:20:28 +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': '管理员',
'shop_no': 'S001',
'shop_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!.shopNo, 'S001');
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: '管理员',
shopNo: 'S001',
shopId: 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': ['S001'],
});
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'), ['S001']);
});
test('updateAccessToken() updates token without changing other fields',
() async {
await notifier.login(const AuthUser(
accessToken: 'old-token',
refreshToken: 'rt',
username: 'admin',
realName: '管理员',
shopNo: 'S001',
shopId: 1,
));
notifier.updateAccessToken('new-token');
expect(notifier.state.user!.accessToken, 'new-token');
expect(notifier.state.user!.refreshToken, 'rt');
expect(notifier.state.user!.username, 'admin');
});
});
}