为什么:a) 应用对外名称要统一成 FruitShorts(产品名,nontonki 是原型代号); b) 首发市场是印尼,界面必须有印尼语;c) 归档要求"新增 id-ID 与改名合并生成一次 l10n,别分两次"。 改名: - pubspec name: nontonki → fruitshorts(Dart 包名),description 同步;28 个文件的 package: 导入前缀替换 - Android android:label、iOS CFBundleDisplayName/CFBundleName → FruitShorts - 图标资源 assets/icon/nontonki.png → fruitshorts.png(含 pubspec 两处引用) - README 同步(补印尼语说明,顺带修掉写错的模块名 reelshort-app-api → fruitshorts-app-api) 文案: - 三份 arb 的 appName → FruitShorts、brandMark → FRUITSHORTS、shareFooter → FruitShorts(不留 demo 标记) - 新增 lib/l10n/app_id.arb:174 个值键与 en/zh 完全一致,按印尼语习惯翻译(Coin→Koin、Episode→Episode) - 一次性 flutter gen-l10n 生成 app_localizations_id.dart;id 自动进入 supportedLocales,默认 locale 未改 - 新增 test/config/l10n_id_test.dart:钉住 id 文案、supportedLocales 含 id、品牌文案无 demo 标记、占位符渲染 不动:applicationId / bundle id(lumadrama 遗留,等裁定)、目录名、业务逻辑与页面结构、语言选择页(仍只有 en/zh) 门禁:dart analyze lib test 零告警;flutter test 159 全过(基线 155 + 新增 4);残留扫描 0 命中; 三份 arb 键集一致(174/174/174);真机(iPhone iOS 26.6.1)构建安装启动成功,devicectl 列出显示名 FruitShorts
169 lines
5.8 KiB
Dart
169 lines
5.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.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:fruitshorts/core/data/remote/api_client.dart';
|
|
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
|
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
|
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
|
|
|
import '../support/fake_http_adapter.dart';
|
|
|
|
/// 用假的 dio adapter 覆盖四条关键路径:注册成功、401 → 刷新 → 重试、刷新失败 → 清会话、登出。
|
|
///
|
|
/// 网络层从 http 换成 dio 后,这里的**断言口径一条没变**,只是注入方式从
|
|
/// `http.testing.MockClient` 换成 `FakeHttpAdapter`。
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
});
|
|
|
|
String tokenBody({
|
|
required String accessToken,
|
|
required String refreshToken,
|
|
String uid = 'U1',
|
|
}) {
|
|
return jsonEncode({
|
|
'code': 0,
|
|
'message': 'ok',
|
|
'data': {
|
|
'accessToken': accessToken,
|
|
'refreshToken': refreshToken,
|
|
'expiresIn': 7200,
|
|
'user': {'uid': uid, 'nickname': 'Tester', 'avatarUrl': ''},
|
|
},
|
|
});
|
|
}
|
|
|
|
ApiClient clientWith(
|
|
AuthController Function() auth, {
|
|
required FakeHttpAdapter adapter,
|
|
}) {
|
|
final store = AuthSessionStore();
|
|
return ApiClient(
|
|
baseUrl: 'http://127.0.0.1:8081',
|
|
adapter: adapter,
|
|
accessTokenReader: () => auth().accessToken,
|
|
tokenRefresher: () => auth().refresh(),
|
|
onUnauthorized: () => auth().clear(),
|
|
deviceIdReader: store.deviceId,
|
|
);
|
|
}
|
|
|
|
test('should_store_session_and_expose_signed_in_state_when_register_succeeds', () async {
|
|
late AuthController auth;
|
|
final adapter = FakeHttpAdapter((options) async {
|
|
expect(options.uri.path, '/app/auth/register');
|
|
expect(options.headers['X-Device-Id'], isNotEmpty);
|
|
expect(options.headers.containsKey('Authorization'), isFalse);
|
|
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
|
});
|
|
final store = AuthSessionStore();
|
|
auth = AuthController(
|
|
AuthApi(clientWith(() => auth, adapter: adapter)),
|
|
store,
|
|
);
|
|
|
|
expect(auth.isSignedIn, isFalse);
|
|
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
|
expect(auth.isSignedIn, isTrue);
|
|
expect(auth.accessToken, 'a1');
|
|
expect(auth.user?.nickname, 'Tester');
|
|
// 落盘:新建一个 store 也能读回来
|
|
expect((await AuthSessionStore().read())?.refreshToken, 'r1');
|
|
});
|
|
|
|
test('should_refresh_once_and_retry_with_new_token_when_response_code_is_401', () async {
|
|
var calls = 0;
|
|
late AuthController auth;
|
|
final adapter = FakeHttpAdapter((options) async {
|
|
calls++;
|
|
if (options.uri.path == '/app/auth/refresh') {
|
|
return FakeHttpResponse(tokenBody(accessToken: 'a2', refreshToken: 'r2'));
|
|
}
|
|
if (options.uri.path != '/app/me') {
|
|
// 注册等其它请求:直接发第一版令牌
|
|
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
|
}
|
|
final token = options.headers['Authorization'];
|
|
if (token == 'Bearer a1') {
|
|
return FakeHttpResponse(
|
|
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
|
);
|
|
}
|
|
expect(token, 'Bearer a2');
|
|
return FakeHttpResponse(
|
|
jsonEncode({
|
|
'code': 0,
|
|
'message': 'ok',
|
|
'data': {'hit': true},
|
|
}),
|
|
);
|
|
});
|
|
final store = AuthSessionStore();
|
|
auth = AuthController(
|
|
AuthApi(clientWith(() => auth, adapter: adapter)),
|
|
store,
|
|
);
|
|
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
|
calls = 0;
|
|
|
|
final api = clientWith(() => auth, adapter: adapter);
|
|
final data = await api.get('/app/me');
|
|
expect((data as Map)['hit'], isTrue);
|
|
expect(calls, 3); // 原请求(401) + 刷新 + 重试
|
|
expect(auth.accessToken, 'a2');
|
|
});
|
|
|
|
test('should_clear_session_when_refresh_itself_fails', () async {
|
|
late AuthController auth;
|
|
final adapter = FakeHttpAdapter((options) async {
|
|
if (options.uri.path == '/app/auth/refresh') {
|
|
return FakeHttpResponse(
|
|
jsonEncode({'code': 401, 'message': '登录已过期', 'data': null}),
|
|
);
|
|
}
|
|
if (options.uri.path == '/app/auth/register') {
|
|
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
|
}
|
|
return FakeHttpResponse(
|
|
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
|
);
|
|
});
|
|
final store = AuthSessionStore();
|
|
auth = AuthController(
|
|
AuthApi(clientWith(() => auth, adapter: adapter)),
|
|
store,
|
|
);
|
|
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
|
|
|
final api = clientWith(() => auth, adapter: adapter);
|
|
await expectLater(api.get('/app/me'), throwsA(isA<ApiException>()));
|
|
expect(auth.isSignedIn, isFalse);
|
|
expect(await AuthSessionStore().read(), isNull);
|
|
});
|
|
|
|
test('should_clear_local_session_when_sign_out_hits_server_error', () async {
|
|
late AuthController auth;
|
|
final adapter = FakeHttpAdapter((options) async {
|
|
if (options.uri.path == '/app/auth/logout') {
|
|
return FakeHttpResponse('{}', 500);
|
|
}
|
|
return FakeHttpResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
|
|
});
|
|
final store = AuthSessionStore();
|
|
auth = AuthController(
|
|
AuthApi(clientWith(() => auth, adapter: adapter)),
|
|
store,
|
|
);
|
|
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
|
|
|
await auth.signOut();
|
|
expect(auth.isSignedIn, isFalse);
|
|
expect(await AuthSessionStore().read(), isNull);
|
|
});
|
|
}
|