为什么:权威文档(AGENTS.md / CODING_STANDARDS.md)强制网络层用 dio,差异清单 A 组第 1 题
用户裁定选 A;原来用官方 http 是在无文档状态下按口头要求选的。
- ApiClient 改用 dio + BaseOptions(validateStatus 恒真、8s 超时、ResponseType.bytes 自行 UTF-8 解码)
- 新增 AuthHeaderInterceptor(Authorization / X-Device-Id)与 EnvelopeInterceptor
(解包 {code,message,data} + 错误码映射 + 401 判定)
- 401 → 刷新 → 重发一次在 ApiClient 里编排,刻意不放在拦截器内重发:dio 的排队拦截器
在回调里再发请求会自锁
- 接口层(auth_api/catalog_api)签名、全部 controller/logic/page 未改动
- 测试:新增假 dio adapter 替代 MockClient,断言口径一条未改;用例名改为 should_<期望>_when_<条件>;
新增 api_client_test(解包/错误映射/请求头/查询参数/跳过重试)与 live_auth_test(真实后端 POST 链路)
- pubspec:加 dio、移除已无引用的 http
- 实测:dart analyze lib test 零告警;flutter test 155 全过;真实联调(8081)GET/POST 链路均通
82 lines
3.0 KiB
Dart
82 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
|
|
|
import 'package:nontonki/core/data/remote/auth_api.dart';
|
|
import 'package:nontonki/core/data/remote/api_client.dart';
|
|
import 'package:nontonki/core/data/services/auth_controller.dart';
|
|
import 'package:nontonki/core/data/services/auth_session_store.dart';
|
|
import 'package:nontonki/l10n/app_localizations.dart';
|
|
import 'package:nontonki/pages/auth/auth_view.dart';
|
|
|
|
import '../support/fake_http_adapter.dart';
|
|
|
|
/// 登录/注册页的可读证据:校验提示 + 登录/注册切换 + 未登录时不会去发请求。
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
});
|
|
|
|
Future<void> pumpAuthPage(WidgetTester tester, {bool signUp = false}) async {
|
|
final auth = AuthController(
|
|
AuthApi(
|
|
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
|
|
ApiClient(
|
|
baseUrl: 'http://127.0.0.1:1',
|
|
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
|
|
),
|
|
),
|
|
AuthSessionStore(),
|
|
);
|
|
await tester.pumpWidget(
|
|
ChangeNotifierProvider<AuthController>.value(
|
|
value: auth,
|
|
child: MaterialApp(
|
|
locale: const Locale('en'),
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
home: AuthPage(signUpMode: signUp),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('should show the email error and stay on the page when the form is empty', (tester) async {
|
|
await pumpAuthPage(tester);
|
|
|
|
expect(find.text('Welcome back'), findsOneWidget);
|
|
|
|
await tester.tap(find.byType(FilledButton));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Enter your email'), findsOneWidget);
|
|
expect(find.text('Welcome back'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('should reject a weak password locally when only letters are typed', (tester) async {
|
|
await pumpAuthPage(tester);
|
|
|
|
await tester.enterText(find.byType(TextField).first, 'a@b.com');
|
|
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
|
|
await tester.tap(find.byType(FilledButton));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Password needs both letters and numbers'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('should flip between sign in and sign up when the switch link is tapped', (tester) async {
|
|
await pumpAuthPage(tester);
|
|
expect(find.text('Welcome back'), findsOneWidget);
|
|
|
|
await tester.tap(find.text('New here? Create an account'));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Create your account'), findsOneWidget);
|
|
expect(find.text('Already have an account? Sign in'), findsOneWidget);
|
|
});
|
|
}
|