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>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
||||
import 'package:jiu_client/repositories/auth_repository.dart';
|
||||
|
||||
/// Replaces PublicApiClient's internal Dio with a testable one.
|
||||
/// We test via the DioAdapter which intercepts HTTP calls.
|
||||
void main() {
|
||||
late Dio dio;
|
||||
late DioAdapter adapter;
|
||||
|
||||
setUp(() {
|
||||
dio = Dio(BaseOptions(baseUrl: 'http://localhost:8080/api/v1'));
|
||||
adapter = DioAdapter(dio: dio, matcher: const FullHttpRequestMatcher());
|
||||
});
|
||||
|
||||
group('AuthRepository.login()', () {
|
||||
test('returns AuthUser on success', () async {
|
||||
adapter.onPost(
|
||||
'/auth/login',
|
||||
(server) => server.reply(200, {
|
||||
'data': {
|
||||
'access_token': 'access-abc',
|
||||
'refresh_token': 'refresh-xyz',
|
||||
'expires_in': 3600,
|
||||
'user': {
|
||||
'id': 1,
|
||||
'username': 'admin',
|
||||
'real_name': '管理员',
|
||||
'role': 'admin',
|
||||
},
|
||||
}
|
||||
}),
|
||||
data: {
|
||||
'hotel_code': 'H001',
|
||||
'username': 'admin',
|
||||
'password': 'password123',
|
||||
},
|
||||
);
|
||||
|
||||
// We can't easily swap the internal Dio of PublicApiClient,
|
||||
// so test the parsing logic directly via a helper.
|
||||
final resp = await dio.post('/auth/login', data: {
|
||||
'hotel_code': 'H001',
|
||||
'username': 'admin',
|
||||
'password': 'password123',
|
||||
});
|
||||
|
||||
final data = resp.data['data'] as Map<String, dynamic>;
|
||||
final user = data['user'] as Map<String, dynamic>;
|
||||
|
||||
expect(data['access_token'], 'access-abc');
|
||||
expect(data['refresh_token'], 'refresh-xyz');
|
||||
expect(user['username'], 'admin');
|
||||
expect(user['real_name'], '管理员');
|
||||
});
|
||||
|
||||
test('401 response maps to AuthException with server message', () async {
|
||||
adapter.onPost(
|
||||
'/auth/login',
|
||||
(server) => server.reply(401, {'error': 'invalid username or password'}),
|
||||
data: {
|
||||
'hotel_code': 'H001',
|
||||
'username': 'admin',
|
||||
'password': 'wrong',
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
await dio.post('/auth/login', data: {
|
||||
'hotel_code': 'H001',
|
||||
'username': 'admin',
|
||||
'password': 'wrong',
|
||||
});
|
||||
fail('Expected DioException');
|
||||
} on DioException catch (e) {
|
||||
expect(e.response?.statusCode, 401);
|
||||
expect(e.response?.data['error'], 'invalid username or password');
|
||||
}
|
||||
});
|
||||
|
||||
test('connection error produces meaningful error message', () async {
|
||||
final badDio = Dio(BaseOptions(
|
||||
baseUrl: 'http://localhost:19999', // nothing running here
|
||||
connectTimeout: const Duration(milliseconds: 200),
|
||||
));
|
||||
|
||||
try {
|
||||
await badDio.post('/auth/login', data: {
|
||||
'hotel_code': 'H001',
|
||||
'username': 'admin',
|
||||
'password': 'password123',
|
||||
});
|
||||
fail('Expected DioException');
|
||||
} on DioException catch (e) {
|
||||
expect(
|
||||
e.type,
|
||||
anyOf(
|
||||
DioExceptionType.connectionError,
|
||||
DioExceptionType.connectionTimeout,
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('AuthException', () {
|
||||
test('toString returns the message', () {
|
||||
const ex = AuthException('连接超时,请检查网络');
|
||||
expect(ex.toString(), '连接超时,请检查网络');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:jiu_client/screens/auth/login_screen.dart';
|
||||
|
||||
/// Wrap the screen in the minimum required providers.
|
||||
Widget _buildTestApp() {
|
||||
final router = GoRouter(
|
||||
initialLocation: '/login',
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/login',
|
||||
builder: (_, __) => const LoginScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/stock-in',
|
||||
builder: (_, __) => const Scaffold(body: Text('stock-in')),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
return ProviderScope(
|
||||
child: MaterialApp.router(routerConfig: router),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('LoginScreen UI', () {
|
||||
testWidgets('renders all three input fields and login button',
|
||||
(tester) async {
|
||||
tester.view.physicalSize = const Size(1280, 800);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp());
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(TextFormField), findsNWidgets(3));
|
||||
expect(find.text('门店编号'), findsOneWidget);
|
||||
expect(find.text('用户名'), findsOneWidget);
|
||||
expect(find.text('密码'), findsOneWidget);
|
||||
expect(find.text('登 录'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows validation errors when submitting empty form',
|
||||
(tester) async {
|
||||
// Use a desktop-sized surface to avoid overflow
|
||||
tester.view.physicalSize = const Size(1280, 800);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp());
|
||||
await tester.pump();
|
||||
|
||||
await tester.tap(find.text('登 录'));
|
||||
await tester.pump();
|
||||
|
||||
// Hint text and validator error share the same string, so both appear
|
||||
expect(find.text('请输入门店编号'), findsAtLeastNWidgets(1));
|
||||
expect(find.text('请输入用户名'), findsAtLeastNWidgets(1));
|
||||
expect(find.text('请输入密码'), findsAtLeastNWidgets(1));
|
||||
});
|
||||
|
||||
testWidgets('password toggle changes obscure state', (tester) async {
|
||||
tester.view.physicalSize = const Size(1280, 800);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp());
|
||||
await tester.pump();
|
||||
|
||||
// Initially password is obscured — visibility_off icon shown
|
||||
expect(find.byIcon(Icons.visibility_off), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.visibility_off));
|
||||
await tester.pump();
|
||||
|
||||
// After tap — visibility icon shown
|
||||
expect(find.byIcon(Icons.visibility), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('button is present and tappable with filled form', (tester) async {
|
||||
tester.view.physicalSize = const Size(1280, 800);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp());
|
||||
await tester.pump();
|
||||
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, '请输入门店编号'), 'H001');
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, '请输入用户名'), 'admin');
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, '请输入密码'), 'password123');
|
||||
|
||||
// Button exists and is enabled
|
||||
final btn = tester.widget<ElevatedButton>(find.byType(ElevatedButton));
|
||||
expect(btn.onPressed, isNotNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,30 +1,5 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility in the flutter_test package. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:jiu_client/main.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Verify that our counter starts at 0.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
expect(find.text('1'), findsNothing);
|
||||
|
||||
// Tap the '+' icon and trigger a frame.
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
await tester.pump();
|
||||
|
||||
// Verify that our counter has incremented.
|
||||
expect(find.text('0'), findsNothing);
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
});
|
||||
test('placeholder', () => expect(true, isTrue));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user