新增:资料页真实化 + 编辑页——/me 读写、PATCH 三态、获赞与作品统计(T3.5-08)
资料页头部三项(展示名 / 头像 / 数字)自此全部来自服务端,176 行硬编码 demo(「萌宠新手(豆豆家长)」/ 24 / 1.8k / 2)退役。 - `PatchField<T>` 承载契约 v1.4.0 的 PATCH 三态(absent 不落键 / clear 落 显式 null / value 落值)。三态必须由类型承载而非 `T?` 加约定:把「不改」 也编码成 null,用户只改昵称就会连头像一起被服务端清掉。 - `UserProfile` 补 nickname / avatarUrl,展示回退 `nickname ?? username` **做在客户端展示层**——服务端 /me 刻意返回 DB 原值,编辑页因此只用原值 预填,避免把展示约定固化成真实昵称。 - `ProfileController`:`/me` 主链路四态 + 两块统计独立三态(统计失败只降级 这一块,不为两个数字丢掉整页);登出 reset 防跨账号泄漏。 - 编辑页维护三态意图而非「当前值整体提交」:昵称 / 头像各有显式清除入口, 未改字段的键根本不进 JSON;空 patch 直接短路(服务端对空 patch 答 400)。 - 昵称校验按**码点**计 1~32 且先 btrim,与 `ck_users_nickname` 对齐; 纯空白是校验失败而非隐式清空。 - `MediaUploader` 的 purpose 参数化 + 复用其六态编排的 `AvatarUploadSheet` (单图、预览确认后才交付 ready assetId,孤儿防护不变)。 - 修复 `/api/v1/me` 端口线路:该端点由 user 服务(:8082)提供,`ApiAuthRepository` 此前只挂 auth(:8081)会得到 404。此前无人消费 me(),故这条错线一直没被 触发;本单是第一个真实消费者,桌面实测即暴露。 测试 526 → 588(+62)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||
|
||||
import '../../helpers/auth_test_helpers.dart';
|
||||
import '../../helpers/community_test_helpers.dart';
|
||||
|
||||
void main() {
|
||||
late FakeCommunityRepository community;
|
||||
|
||||
setUp(() {
|
||||
community = FakeCommunityRepository();
|
||||
});
|
||||
|
||||
ProfileController build(FakeAuthRepository auth) =>
|
||||
ProfileController(authRepository: auth, communityRepository: community);
|
||||
|
||||
test('四态:initial → loading → ready(资料 + 两块统计齐备)', () async {
|
||||
final gate = Completer<UserProfile>();
|
||||
final controller = build(FakeAuthRepository(meHandler: () => gate.future));
|
||||
community.onGetMyCommunityStats = () async =>
|
||||
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||
community.onGetFollowStats = (_) async =>
|
||||
FollowStats.fromJson(sampleFollowStatsJson());
|
||||
|
||||
expect(controller.phase, ProfileLoadPhase.initial);
|
||||
final pending = controller.refresh();
|
||||
expect(controller.phase, ProfileLoadPhase.loading);
|
||||
|
||||
gate.complete(buildProfile(nickname: '小柴'));
|
||||
await pending;
|
||||
|
||||
expect(controller.phase, ProfileLoadPhase.ready);
|
||||
expect(controller.statsPhase, ProfileStatsPhase.ready);
|
||||
expect(controller.displayName, '小柴');
|
||||
expect(controller.communityStats!.receivedLikeCount, 128);
|
||||
expect(controller.followStats!.followerCount, 24);
|
||||
});
|
||||
|
||||
test('/me 失败且无副本 → error 态 + 可重试;不外抛', () async {
|
||||
var attempts = 0;
|
||||
final auth = FakeAuthRepository(
|
||||
meHandler: () async {
|
||||
attempts += 1;
|
||||
if (attempts == 1) throw const ApiNetworkException('断网');
|
||||
return buildProfile(nickname: '小柴');
|
||||
},
|
||||
);
|
||||
final controller = build(auth);
|
||||
|
||||
await controller.refresh();
|
||||
expect(controller.phase, ProfileLoadPhase.error);
|
||||
expect(controller.lastError, isA<ApiNetworkException>());
|
||||
expect(controller.profile, isNull);
|
||||
|
||||
await controller.refresh();
|
||||
expect(controller.phase, ProfileLoadPhase.ready);
|
||||
expect(controller.displayName, '小柴');
|
||||
});
|
||||
|
||||
test('已有副本时刷新失败保留副本(不打断阅读,停在 ready)', () async {
|
||||
var attempts = 0;
|
||||
final controller = build(
|
||||
FakeAuthRepository(
|
||||
meHandler: () async {
|
||||
attempts += 1;
|
||||
if (attempts == 1) return buildProfile(nickname: '小柴');
|
||||
throw const ApiNetworkException('断网');
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await controller.refresh();
|
||||
await controller.refresh();
|
||||
|
||||
expect(controller.phase, ProfileLoadPhase.ready);
|
||||
expect(controller.displayName, '小柴');
|
||||
expect(controller.lastError, isA<ApiNetworkException>());
|
||||
});
|
||||
|
||||
test('统计失败只降级统计块,资料仍 ready;refreshStats 可单独重试', () async {
|
||||
var statsAttempts = 0;
|
||||
community.onGetMyCommunityStats = () async {
|
||||
statsAttempts += 1;
|
||||
if (statsAttempts == 1) throw const ApiNetworkException('断网');
|
||||
return CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||
};
|
||||
community.onGetFollowStats = (_) async =>
|
||||
FollowStats.fromJson(sampleFollowStatsJson());
|
||||
final controller = build(FakeAuthRepository());
|
||||
|
||||
await controller.refresh();
|
||||
expect(controller.phase, ProfileLoadPhase.ready);
|
||||
expect(controller.statsPhase, ProfileStatsPhase.error);
|
||||
expect(controller.communityStats, isNull);
|
||||
|
||||
await controller.refreshStats();
|
||||
expect(controller.statsPhase, ProfileStatsPhase.ready);
|
||||
expect(controller.phase, ProfileLoadPhase.ready);
|
||||
});
|
||||
|
||||
test('空数据:两个数字为 0(不是 null、不是 404)', () async {
|
||||
community.onGetMyCommunityStats = () async => CommunityStats.fromJson(
|
||||
sampleCommunityStatsJson(receivedLikeCount: 0, publishedPostCount: 0),
|
||||
);
|
||||
community.onGetFollowStats = (_) async => FollowStats.fromJson(
|
||||
sampleFollowStatsJson(followerCount: 0, followingCount: 0),
|
||||
);
|
||||
final controller = build(FakeAuthRepository());
|
||||
|
||||
await controller.refresh();
|
||||
|
||||
expect(controller.communityStats!.receivedLikeCount, 0);
|
||||
expect(controller.communityStats!.publishedPostCount, 0);
|
||||
});
|
||||
|
||||
test('save:空 patch 直接短路,不发请求(服务端对空 patch 答 400)', () async {
|
||||
final auth = FakeAuthRepository(
|
||||
updateMeHandler: (_) async => buildProfile(nickname: '不该被调用'),
|
||||
);
|
||||
final controller = build(auth);
|
||||
await controller.refresh();
|
||||
|
||||
await controller.save(const UpdateMeRequest());
|
||||
|
||||
expect(auth.updateMePayloads, isEmpty);
|
||||
});
|
||||
|
||||
test('save 成功即以服务端回显替换副本(含 avatarUrl 现签值)', () async {
|
||||
final auth = FakeAuthRepository(
|
||||
updateMeHandler: (_) async => buildProfile(
|
||||
nickname: '小柴',
|
||||
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=new',
|
||||
),
|
||||
);
|
||||
final controller = build(auth);
|
||||
await controller.refresh();
|
||||
expect(controller.displayName, 'llx');
|
||||
|
||||
await controller.save(
|
||||
const UpdateMeRequest(nickname: PatchField<String>.value('小柴')),
|
||||
);
|
||||
|
||||
expect(controller.displayName, '小柴');
|
||||
expect(controller.profile!.avatarUrl, contains('X-Amz-Signature=new'));
|
||||
expect(auth.updateMePayloads.single, {'nickname': '小柴'});
|
||||
});
|
||||
|
||||
test('save 失败按类型化异常外抛(供编辑页分层呈现),副本不动', () async {
|
||||
final auth = FakeAuthRepository(
|
||||
updateMeHandler: (_) async => throw const ApiBusinessException(
|
||||
code: ApiCodes.mediaNotReady,
|
||||
message: 'not ready',
|
||||
),
|
||||
);
|
||||
final controller = build(auth);
|
||||
await controller.refresh();
|
||||
|
||||
await expectLater(
|
||||
controller.save(
|
||||
const UpdateMeRequest(avatarAssetId: PatchField<String>.value('a-1')),
|
||||
),
|
||||
throwsA(
|
||||
isA<ApiBusinessException>().having(
|
||||
(e) => e.code,
|
||||
'code',
|
||||
ApiCodes.mediaNotReady,
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(controller.displayName, 'llx');
|
||||
});
|
||||
|
||||
test('reset:登出清空资料与统计,回 initial(跨账号不泄漏)', () async {
|
||||
final controller = build(FakeAuthRepository());
|
||||
await controller.refresh();
|
||||
expect(controller.profile, isNotNull);
|
||||
|
||||
controller.reset();
|
||||
|
||||
expect(controller.phase, ProfileLoadPhase.initial);
|
||||
expect(controller.profile, isNull);
|
||||
expect(controller.displayName, isNull);
|
||||
expect(controller.communityStats, isNull);
|
||||
expect(controller.followStats, isNull);
|
||||
});
|
||||
|
||||
test('follow-stats 用的是本人 userId(路径主体正确)', () async {
|
||||
final controller = build(
|
||||
FakeAuthRepository(meHandler: () async => buildProfile(userId: 'u-42')),
|
||||
);
|
||||
await controller.refresh();
|
||||
expect(community.calls, contains('followStats:u-42'));
|
||||
expect(community.calls, contains('communityStats'));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||
|
||||
import '../../helpers/auth_test_helpers.dart';
|
||||
import '../../helpers/community_test_helpers.dart';
|
||||
import '../../helpers/media_test_helpers.dart';
|
||||
|
||||
/// T3.5-08 编辑页:**PATCH 三态载荷**是本文件的重点。
|
||||
///
|
||||
/// 每个成功用例都断言一次「未改字段的键不出现在 JSON 里」——这是三态与
|
||||
/// 两态的唯一可观测差别,也是「只改昵称却把头像清掉」这类事故的唯一防线。
|
||||
|
||||
/// 昵称输入框(AppTextField 内层 TextFormField)。
|
||||
Finder get nicknameField => find.byType(TextFormField);
|
||||
|
||||
void main() {
|
||||
late FakeCommunityRepository community;
|
||||
late FakeMediaImagePicker picker;
|
||||
late FakeMediaCompressor compressor;
|
||||
late FakeDirectUploadClient direct;
|
||||
|
||||
setUp(() {
|
||||
community = FakeCommunityRepository();
|
||||
community.onGetMyCommunityStats = () async =>
|
||||
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||
community.onGetFollowStats = (_) async =>
|
||||
FollowStats.fromJson(sampleFollowStatsJson());
|
||||
picker = FakeMediaImagePicker([decodablePickedImage()]);
|
||||
compressor = FakeMediaCompressor();
|
||||
direct = FakeDirectUploadClient();
|
||||
community.onCreateMediaUpload = (_) async => credentials();
|
||||
community.onCompleteMediaUpload = (assetId) async =>
|
||||
readyAsset(assetId: assetId);
|
||||
});
|
||||
|
||||
MediaUploader buildUploader(MediaPurpose purpose) => MediaUploader(
|
||||
repository: community,
|
||||
purpose: purpose,
|
||||
picker: picker,
|
||||
compressor: compressor,
|
||||
directUpload: direct,
|
||||
maxImages: 1,
|
||||
maxConcurrentUploads: 1,
|
||||
);
|
||||
|
||||
/// 直接 push 编辑页(资料页链路另有测试;这里只测编辑页本身)。
|
||||
Future<FakeAuthRepository> pumpEdit(
|
||||
WidgetTester tester, {
|
||||
String? nickname,
|
||||
String? avatarUrl,
|
||||
bool withUploader = true,
|
||||
ApiException? saveError,
|
||||
}) async {
|
||||
tester.view.physicalSize = const Size(700, 1800);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.reset);
|
||||
final auth = FakeAuthRepository(
|
||||
meHandler: () async => buildProfile(
|
||||
username: 'llx',
|
||||
nickname: nickname,
|
||||
avatarUrl: avatarUrl,
|
||||
),
|
||||
updateMeHandler: (_) async {
|
||||
if (saveError != null) throw saveError;
|
||||
return buildProfile(username: 'llx', nickname: nickname);
|
||||
},
|
||||
);
|
||||
final controller = ProfileController(
|
||||
authRepository: auth,
|
||||
communityRepository: community,
|
||||
);
|
||||
addTearDown(controller.dispose);
|
||||
await controller.refresh();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: buildAppTheme(),
|
||||
home: ProfileEditPage(
|
||||
controller: controller,
|
||||
avatarUploaderBuilder: withUploader ? buildUploader : null,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
return auth;
|
||||
}
|
||||
|
||||
group('三态载荷', () {
|
||||
testWidgets('只改昵称 → 载荷只有 nickname,avatarAssetId 键不出现', (tester) async {
|
||||
final auth = await pumpEdit(
|
||||
tester,
|
||||
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||
);
|
||||
|
||||
await tester.enterText(nicknameField, '小柴');
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final payload = auth.updateMePayloads.single;
|
||||
expect(payload, {'nickname': '小柴'});
|
||||
expect(
|
||||
payload.containsKey('avatarAssetId'),
|
||||
isFalse,
|
||||
reason: '发成 avatarAssetId: null 会把用户已有头像一起清掉',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('点「清除昵称」 → nickname 显式 null,头像键不出现', (tester) async {
|
||||
final auth = await pumpEdit(
|
||||
tester,
|
||||
nickname: '小柴',
|
||||
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||
);
|
||||
|
||||
await tester.tap(find.text('清除昵称'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('保存后将清除昵称,展示名回退为用户名'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final payload = auth.updateMePayloads.single;
|
||||
expect(payload.containsKey('nickname'), isTrue);
|
||||
expect(payload['nickname'], isNull);
|
||||
expect(payload.containsKey('avatarAssetId'), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('点「清除头像」 → avatarAssetId 显式 null,nickname 键不出现', (
|
||||
tester,
|
||||
) async {
|
||||
final auth = await pumpEdit(
|
||||
tester,
|
||||
nickname: '小柴',
|
||||
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||
);
|
||||
|
||||
await tester.tap(find.text('清除头像'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('保存后将移除头像'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final payload = auth.updateMePayloads.single;
|
||||
expect(payload.containsKey('avatarAssetId'), isTrue);
|
||||
expect(payload['avatarAssetId'], isNull);
|
||||
expect(
|
||||
payload.containsKey('nickname'),
|
||||
isFalse,
|
||||
reason: '昵称没碰过就不该出现在载荷里(更不该是 null)',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('输入与原昵称相同 → 视为未改,保存钮禁用、不发请求', (tester) async {
|
||||
final auth = await pumpEdit(tester, nickname: '小柴');
|
||||
|
||||
// 预填即原值。
|
||||
expect(find.text('小柴'), findsOneWidget);
|
||||
final button = tester.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, '保存'),
|
||||
);
|
||||
expect(button.onPressed, isNull);
|
||||
expect(auth.updateMePayloads, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('无昵称时预填空串(不预填 username,避免把回退固化成真昵称)', (tester) async {
|
||||
await pumpEdit(tester);
|
||||
|
||||
final field = tester.widget<TextFormField>(nicknameField);
|
||||
expect(field.controller!.text, '');
|
||||
expect(find.text('未设置,当前展示为用户名「llx」'), findsOneWidget);
|
||||
// 无昵称时不给「清除昵称」入口(清一个不存在的东西无意义)。
|
||||
expect(find.text('清除昵称'), findsNothing);
|
||||
});
|
||||
});
|
||||
|
||||
group('校验与失败语义', () {
|
||||
testWidgets('33 码点昵称:字段级 errorText,不发请求', (tester) async {
|
||||
final auth = await pumpEdit(tester);
|
||||
|
||||
await tester.enterText(nicknameField, '柴' * 33);
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('昵称最多 32 个字'), findsOneWidget);
|
||||
expect(auth.updateMePayloads, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('纯空格昵称:不是隐式清空,提示走显式清除入口', (tester) async {
|
||||
final auth = await pumpEdit(tester, nickname: '小柴');
|
||||
|
||||
await tester.enterText(nicknameField, ' ');
|
||||
await tester.pump();
|
||||
// 空白输入被判为「不改」,保存钮禁用(不会误发清空)。
|
||||
final button = tester.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, '保存'),
|
||||
);
|
||||
expect(button.onPressed, isNull);
|
||||
expect(auth.updateMePayloads, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('42203 头像未就绪 → 横幅提示,页面留在编辑态可重试', (tester) async {
|
||||
await pumpEdit(
|
||||
tester,
|
||||
saveError: const ApiBusinessException(
|
||||
code: ApiCodes.mediaNotReady,
|
||||
message: 'asset uploading',
|
||||
),
|
||||
);
|
||||
|
||||
await tester.enterText(nicknameField, '小柴');
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||
expect(find.text('头像还没上传完,请稍后重试'), findsOneWidget);
|
||||
expect(find.byType(ProfileEditPage), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('40405 头像 asset 失效 → 提示重新上传', (tester) async {
|
||||
await pumpEdit(
|
||||
tester,
|
||||
saveError: const ApiBusinessException(
|
||||
code: ApiCodes.mediaNotFound,
|
||||
message: 'gone',
|
||||
),
|
||||
);
|
||||
|
||||
await tester.enterText(nicknameField, '小柴');
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('头像已失效,请重新上传'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
|
||||
group('头像上传接线', () {
|
||||
testWidgets('上传全链路:purpose=user_avatar,ready 后确认 → 载荷带新 assetId', (
|
||||
tester,
|
||||
) async {
|
||||
final auth = await pumpEdit(tester);
|
||||
|
||||
await tester.tap(find.text('更换头像'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 上传成功后 sheet 给预览 + 「使用这张」。
|
||||
expect(find.text('使用这张'), findsOneWidget);
|
||||
await tester.tap(find.text('使用这张'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 用途即引用侧的类型检查:必须是 user_avatar。
|
||||
final request = community.lastMediaUploadRequest!;
|
||||
expect(request.purpose, MediaPurpose.userAvatar);
|
||||
expect(request.purpose.wire, 'user_avatar');
|
||||
|
||||
expect(find.text('已选择\n新头像'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final payload = auth.updateMePayloads.single;
|
||||
expect(payload, {'avatarAssetId': 'a-1'});
|
||||
expect(payload.containsKey('nickname'), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('上传后再改昵称 → 两键齐出(一次 PATCH 改两样)', (tester) async {
|
||||
final auth = await pumpEdit(tester);
|
||||
|
||||
await tester.tap(find.text('更换头像'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('使用这张'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.enterText(nicknameField, '小柴');
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(auth.updateMePayloads.single, {
|
||||
'nickname': '小柴',
|
||||
'avatarAssetId': 'a-1',
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_display.dart';
|
||||
|
||||
import '../../helpers/auth_test_helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('展示名回退 nickname ?? username', () {
|
||||
test('有昵称:展示昵称,nickname 字段保留 DB 原值', () {
|
||||
final profile = buildProfile(username: 'llx', nickname: '小柴');
|
||||
expect(profile.displayName, '小柴');
|
||||
expect(profile.nickname, '小柴');
|
||||
});
|
||||
|
||||
test('无昵称:展示 username,但 nickname 仍为 null(不被回退值污染)', () {
|
||||
final profile = buildProfile(username: 'llx');
|
||||
expect(profile.displayName, 'llx');
|
||||
expect(
|
||||
profile.nickname,
|
||||
isNull,
|
||||
reason: '回退只在展示层发生;nickname 必须保持 null,否则编辑页预填后会被固化成真昵称',
|
||||
);
|
||||
});
|
||||
|
||||
test('hasAvatar 只看 avatarUrl(响应不外露 avatarAssetId)', () {
|
||||
expect(buildProfile().hasAvatar, isFalse);
|
||||
expect(
|
||||
buildProfile(avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s').hasAvatar,
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('UpdateMeRequest 三态载荷', () {
|
||||
test('什么都没碰 → isEmpty 且 JSON 为空对象', () {
|
||||
const request = UpdateMeRequest();
|
||||
expect(request.isEmpty, isTrue);
|
||||
expect(request.toJson(), isEmpty);
|
||||
});
|
||||
|
||||
test('只改昵称:avatarAssetId 键**不出现**(不改,而不是清空)', () {
|
||||
const request = UpdateMeRequest(nickname: PatchField<String>.value('小柴'));
|
||||
final json = request.toJson();
|
||||
expect(json, {'nickname': '小柴'});
|
||||
expect(
|
||||
json.containsKey('avatarAssetId'),
|
||||
isFalse,
|
||||
reason: '发成 avatarAssetId: null 会把用户头像一起清掉',
|
||||
);
|
||||
});
|
||||
|
||||
test('只清昵称:nickname 显式 null,头像键仍不出现', () {
|
||||
const request = UpdateMeRequest(nickname: PatchField<String>.clear());
|
||||
final json = request.toJson();
|
||||
expect(json.containsKey('nickname'), isTrue);
|
||||
expect(json['nickname'], isNull);
|
||||
expect(json.containsKey('avatarAssetId'), isFalse);
|
||||
});
|
||||
|
||||
test('一次改两样:两键齐出', () {
|
||||
const request = UpdateMeRequest(
|
||||
nickname: PatchField<String>.value('小柴'),
|
||||
avatarAssetId: PatchField<String>.value('asset-9'),
|
||||
);
|
||||
expect(request.toJson(), {'nickname': '小柴', 'avatarAssetId': 'asset-9'});
|
||||
expect(request.isEmpty, isFalse);
|
||||
});
|
||||
|
||||
test('只清头像:nickname 键不出现', () {
|
||||
const request = UpdateMeRequest(
|
||||
avatarAssetId: PatchField<String>.clear(),
|
||||
);
|
||||
final json = request.toJson();
|
||||
expect(json.containsKey('nickname'), isFalse);
|
||||
expect(json['avatarAssetId'], isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('昵称校验与 ck_users_nickname 对齐', () {
|
||||
test('1 码点通过;32 CJK 通过;33 拒', () {
|
||||
expect(validateNickname('柴'), isNull);
|
||||
expect(validateNickname('柴' * 32), isNull);
|
||||
expect(validateNickname('柴' * 33), isNotNull);
|
||||
});
|
||||
|
||||
test('32 个 emoji(UTF-16 长度 64)通过——长度按码点计而非 String.length', () {
|
||||
const emoji = '🐕';
|
||||
final name = emoji * 32;
|
||||
expect(name.length, 64, reason: '前提:UTF-16 长度确为 64');
|
||||
expect(
|
||||
validateNickname(name),
|
||||
isNull,
|
||||
reason: '按 String.length 校验会误拒数据库能存的 32 emoji 昵称',
|
||||
);
|
||||
expect(validateNickname(emoji * 33), isNotNull);
|
||||
});
|
||||
|
||||
test('btrim 对齐:前后空格不计入长度', () {
|
||||
expect(validateNickname(' 豆豆 '), isNull);
|
||||
expect(validateNickname(' ${'柴' * 32} '), isNull);
|
||||
});
|
||||
|
||||
test('纯空白不是隐式清空,而是校验失败并指向显式清除入口', () {
|
||||
final message = validateNickname(' ');
|
||||
expect(message, isNotNull);
|
||||
expect(message, contains('清除昵称'));
|
||||
});
|
||||
});
|
||||
|
||||
group('错误文案分层', () {
|
||||
test('保存失败按 PATCH /me 错误谱分层', () {
|
||||
expect(
|
||||
profileSaveErrorMessage(
|
||||
const ApiBusinessException(code: ApiCodes.paramError, message: 'x'),
|
||||
),
|
||||
'填写内容不符合要求,请检查后重试',
|
||||
);
|
||||
expect(
|
||||
profileSaveErrorMessage(
|
||||
const ApiBusinessException(
|
||||
code: ApiCodes.mediaNotFound,
|
||||
message: 'x',
|
||||
),
|
||||
),
|
||||
'头像已失效,请重新上传',
|
||||
);
|
||||
expect(
|
||||
profileSaveErrorMessage(
|
||||
const ApiBusinessException(
|
||||
code: ApiCodes.mediaNotReady,
|
||||
message: 'x',
|
||||
),
|
||||
),
|
||||
'头像还没上传完,请稍后重试',
|
||||
);
|
||||
expect(
|
||||
profileSaveErrorMessage(const ApiNetworkException()),
|
||||
'网络异常,请检查网络后重试',
|
||||
);
|
||||
});
|
||||
|
||||
test('加载失败文案不透出服务端 message', () {
|
||||
expect(
|
||||
profileLoadErrorMessage(
|
||||
const ApiNetworkException('Connection refused'),
|
||||
),
|
||||
'网络异常,请检查网络后重试',
|
||||
);
|
||||
expect(profileLoadErrorMessage(null), '加载失败,请稍后重试');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||
import 'package:patbond_flutter/features/profile/profile_page.dart';
|
||||
import 'package:patbond_flutter/state/app_state.dart';
|
||||
|
||||
import '../../helpers/auth_test_helpers.dart';
|
||||
import '../../helpers/community_test_helpers.dart';
|
||||
|
||||
void main() {
|
||||
late FakeCommunityRepository community;
|
||||
|
||||
setUp(() {
|
||||
community = FakeCommunityRepository();
|
||||
community.onGetMyCommunityStats = () async =>
|
||||
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||
community.onGetFollowStats = (_) async =>
|
||||
FollowStats.fromJson(sampleFollowStatsJson());
|
||||
});
|
||||
|
||||
Future<ProfileController> pumpProfile(
|
||||
WidgetTester tester, {
|
||||
required FakeAuthRepository auth,
|
||||
}) async {
|
||||
// 资料页头部 + 五行菜单 + 两个底部按钮,加高视口保证全在栏内。
|
||||
tester.view.physicalSize = const Size(700, 2400);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.reset);
|
||||
final controller = ProfileController(
|
||||
authRepository: auth,
|
||||
communityRepository: community,
|
||||
);
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: buildAppTheme(),
|
||||
home: Scaffold(
|
||||
body: ProfilePage(appState: AppState(), controller: controller),
|
||||
),
|
||||
),
|
||||
);
|
||||
return controller;
|
||||
}
|
||||
|
||||
testWidgets('loading 态:居中转圈,无 demo 文案', (tester) async {
|
||||
final gate = Completer<UserProfile>();
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(meHandler: () => gate.future),
|
||||
);
|
||||
// 预取推到帧末,故先走一帧再断言。
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||
expect(find.text('萌宠新手(豆豆家长)'), findsNothing);
|
||||
expect(find.text('Patbond 社区创作达人'), findsNothing);
|
||||
|
||||
gate.complete(buildProfile());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
|
||||
testWidgets('ready + 有昵称:展示昵称与 @username,四个数字来自服务端', (tester) async {
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(
|
||||
meHandler: () async => buildProfile(username: 'llx', nickname: '小柴'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('小柴'), findsOneWidget);
|
||||
expect(find.text('@llx'), findsOneWidget);
|
||||
// 24 关注我 / 7 我关注 / 128 获赞 / 12 作品(helpers 样本)。
|
||||
expect(find.text('24'), findsOneWidget);
|
||||
expect(find.text('7'), findsOneWidget);
|
||||
expect(find.text('128'), findsOneWidget);
|
||||
expect(find.text('12'), findsOneWidget);
|
||||
expect(find.text('获赞'), findsOneWidget);
|
||||
expect(find.text('我的作品'), findsOneWidget);
|
||||
// demo 硬编码彻底退役。
|
||||
expect(find.text('1.8k'), findsNothing);
|
||||
expect(find.text('萌宠新手(豆豆家长)'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('ready + 无昵称:展示名回退 username(回退在客户端展示层)', (tester) async {
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(
|
||||
meHandler: () async => buildProfile(username: 'llx'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 展示名与 @username 两处都是 llx。
|
||||
expect(find.text('llx'), findsOneWidget);
|
||||
expect(find.text('@llx'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('空数据:四个数字全 0(不是空白、不是错误态)', (tester) async {
|
||||
community.onGetMyCommunityStats = () async => CommunityStats.fromJson(
|
||||
sampleCommunityStatsJson(receivedLikeCount: 0, publishedPostCount: 0),
|
||||
);
|
||||
community.onGetFollowStats = (_) async => FollowStats.fromJson(
|
||||
sampleFollowStatsJson(followerCount: 0, followingCount: 0),
|
||||
);
|
||||
await pumpProfile(tester, auth: FakeAuthRepository());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('0'), findsNWidgets(4));
|
||||
expect(find.text('统计加载失败'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('error 态:横幅 + 重试;重试成功进 ready', (tester) async {
|
||||
var attempts = 0;
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(
|
||||
meHandler: () async {
|
||||
attempts += 1;
|
||||
if (attempts == 1) throw const ApiNetworkException('断网');
|
||||
return buildProfile(nickname: '小柴');
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||
expect(find.text('网络异常,请检查网络后重试'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('重试'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('小柴'), findsOneWidget);
|
||||
expect(find.byType(InlineErrorBanner), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('统计块独立降级:资料照常显示,统计给「加载失败 + 重试」', (tester) async {
|
||||
var statsAttempts = 0;
|
||||
community.onGetMyCommunityStats = () async {
|
||||
statsAttempts += 1;
|
||||
if (statsAttempts == 1) throw const ApiNetworkException('断网');
|
||||
return CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||
};
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(
|
||||
meHandler: () async => buildProfile(nickname: '小柴'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('小柴'), findsOneWidget, reason: '统计失败不该拖垮整页');
|
||||
expect(find.text('统计加载失败'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('重试'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('128'), findsOneWidget);
|
||||
expect(find.text('统计加载失败'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('点「编辑资料」进编辑页;保存成功回资料页并提示', (tester) async {
|
||||
final auth = FakeAuthRepository(
|
||||
meHandler: () async => buildProfile(username: 'llx'),
|
||||
updateMeHandler: (_) async =>
|
||||
buildProfile(username: 'llx', nickname: '小柴'),
|
||||
);
|
||||
await pumpProfile(tester, auth: auth);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.text('编辑资料'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(ProfileEditPage), findsOneWidget);
|
||||
|
||||
await tester.enterText(find.byType(TextFormField), '小柴');
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('保存'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(ProfileEditPage), findsNothing);
|
||||
expect(find.text('资料已更新'), findsOneWidget);
|
||||
// 头部随之更新为新昵称。
|
||||
expect(find.text('小柴'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('未装配上传能力时编辑页不渲染头像入口', (tester) async {
|
||||
await pumpProfile(
|
||||
tester,
|
||||
auth: FakeAuthRepository(meHandler: () async => buildProfile()),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.text('编辑资料'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('更换头像'), findsNothing);
|
||||
expect(find.byType(TextFormField), findsOneWidget, reason: '昵称输入仍在');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user