a4a97c03c2
资料页头部三项(展示名 / 头像 / 数字)自此全部来自服务端,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>
387 lines
11 KiB
Dart
387 lines
11 KiB
Dart
import 'package:patbond_flutter/features/community/community_models.dart';
|
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
|
|
|
/// community 域测试样本 JSON(字段与契约 v1.3.0 逐字一致)。
|
|
|
|
Map<String, dynamic> sampleAuthorJson({
|
|
String userId = 'u-1',
|
|
String? nickname = '毛毛的铲屎官',
|
|
String? avatarUrl = 'https://minio.local/avatar.jpg?X-Amz-Signature=sig',
|
|
}) => {'userId': userId, 'nickname': nickname, 'avatarUrl': avatarUrl};
|
|
|
|
Map<String, dynamic> samplePostMediaItemJson({
|
|
String assetId = 'a-1',
|
|
int position = 0,
|
|
bool isCover = true,
|
|
}) => {
|
|
'assetId': assetId,
|
|
'position': position,
|
|
'isCover': isCover,
|
|
'url': 'https://minio.local/p.jpg?X-Amz-Signature=sig',
|
|
'widthPx': 1080,
|
|
'heightPx': 810,
|
|
'caption': '晒太阳',
|
|
};
|
|
|
|
Map<String, dynamic> samplePostJson({
|
|
String id = 'p-1',
|
|
String status = 'published',
|
|
bool likedByMe = false,
|
|
int likeCount = 6,
|
|
bool bookmarkedByMe = false,
|
|
int bookmarkCount = 2,
|
|
int version = 1,
|
|
}) => {
|
|
'id': id,
|
|
'author': sampleAuthorJson(),
|
|
'petId': 'pet-1',
|
|
'category': 'general',
|
|
'title': '今天的豆豆',
|
|
'content': '晒了一下午太阳。',
|
|
'status': status,
|
|
'visibility': 'public',
|
|
'media': [samplePostMediaItemJson()],
|
|
'likeCount': likeCount,
|
|
'commentCount': 3,
|
|
'bookmarkCount': bookmarkCount,
|
|
'likedByMe': likedByMe,
|
|
'bookmarkedByMe': bookmarkedByMe,
|
|
'createdAt': '2026-09-08T10:00:00.000Z',
|
|
'updatedAt': '2026-09-08T10:05:00.000Z',
|
|
'publishedAt': status == 'published' ? '2026-09-08T10:05:00.000Z' : null,
|
|
'version': version,
|
|
};
|
|
|
|
Map<String, dynamic> sampleFeedCardJson({
|
|
String id = 'p-1',
|
|
bool likedByMe = false,
|
|
int likeCount = 6,
|
|
bool bookmarkedByMe = false,
|
|
int bookmarkCount = 2,
|
|
}) => {
|
|
'id': id,
|
|
'author': sampleAuthorJson(),
|
|
'category': 'general',
|
|
'title': '今天的豆豆',
|
|
'contentPreview': '晒了一下午太阳。',
|
|
'coverImage': samplePostMediaItemJson(),
|
|
'mediaCount': 1,
|
|
'likeCount': likeCount,
|
|
'commentCount': 3,
|
|
'bookmarkCount': bookmarkCount,
|
|
'likedByMe': likedByMe,
|
|
'bookmarkedByMe': bookmarkedByMe,
|
|
'publishedAt': '2026-09-08T10:05:00.000Z',
|
|
};
|
|
|
|
Map<String, dynamic> sampleCommentJson({
|
|
String id = 'c-1',
|
|
Map<String, dynamic>? author,
|
|
Map<String, dynamic>? replyToUser,
|
|
String content = '好可爱!',
|
|
}) => {
|
|
'id': id,
|
|
'postId': 'p-1',
|
|
'author': author ?? sampleAuthorJson(),
|
|
'replyToUser': replyToUser,
|
|
'content': content,
|
|
'createdAt': '2026-09-08T11:00:00.000Z',
|
|
};
|
|
|
|
Map<String, dynamic> sampleUploadCredentialsJson() => {
|
|
'assetId': 'a-1',
|
|
'uploadUrl':
|
|
'https://minio.local/patbond-media/post_image/a-1?X-Amz-Signature=sig',
|
|
'method': 'PUT',
|
|
'requiredHeaders': {'Content-Type': 'image/jpeg'},
|
|
'expiresAt': '2026-09-08T10:10:00.000Z',
|
|
};
|
|
|
|
Map<String, dynamic> sampleMediaAssetJson({String status = 'ready'}) => {
|
|
'id': 'a-1',
|
|
'kind': 'image',
|
|
'purpose': 'post_image',
|
|
'mimeType': 'image/jpeg',
|
|
'byteSize': 204800,
|
|
'widthPx': 1080,
|
|
'heightPx': 810,
|
|
'status': status,
|
|
'url': status == 'ready'
|
|
? 'https://minio.local/p.jpg?X-Amz-Signature=sig'
|
|
: null,
|
|
'readyAt': status == 'ready' ? '2026-09-08T10:06:00.000Z' : null,
|
|
'createdAt': '2026-09-08T10:00:00.000Z',
|
|
};
|
|
|
|
Map<String, Object?> cursorPageJson(
|
|
List<Map<String, dynamic>> items, {
|
|
String? nextCursor,
|
|
bool hasMore = false,
|
|
}) => {'items': items, 'nextCursor': nextCursor, 'hasMore': hasMore};
|
|
|
|
FeedCard sampleFeedCard({
|
|
String id = 'p-1',
|
|
bool likedByMe = false,
|
|
int likeCount = 6,
|
|
}) => FeedCard.fromJson(
|
|
sampleFeedCardJson(id: id, likedByMe: likedByMe, likeCount: likeCount),
|
|
);
|
|
|
|
CursorPage<FeedCard> feedPage(
|
|
List<FeedCard> items, {
|
|
String? nextCursor,
|
|
bool hasMore = false,
|
|
}) => CursorPage(items: items, nextCursor: nextCursor, hasMore: hasMore);
|
|
|
|
Post samplePost({
|
|
String id = 'p-1',
|
|
bool likedByMe = false,
|
|
int likeCount = 6,
|
|
bool bookmarkedByMe = false,
|
|
int bookmarkCount = 2,
|
|
}) => Post.fromJson(
|
|
samplePostJson(
|
|
id: id,
|
|
likedByMe: likedByMe,
|
|
likeCount: likeCount,
|
|
bookmarkedByMe: bookmarkedByMe,
|
|
bookmarkCount: bookmarkCount,
|
|
),
|
|
);
|
|
|
|
PostComment sampleComment({
|
|
String id = 'c-1',
|
|
Map<String, dynamic>? author,
|
|
Map<String, dynamic>? replyToUser,
|
|
String content = '好可爱!',
|
|
}) => PostComment.fromJson(
|
|
sampleCommentJson(
|
|
id: id,
|
|
author: author,
|
|
replyToUser: replyToUser,
|
|
content: content,
|
|
),
|
|
);
|
|
|
|
CursorPage<PostComment> commentPage(
|
|
List<PostComment> items, {
|
|
String? nextCursor,
|
|
bool hasMore = false,
|
|
}) => CursorPage(items: items, nextCursor: nextCursor, hasMore: hasMore);
|
|
|
|
/// 草稿样本(T3-17 发布页草稿恢复用;media 可清空为纯文字草稿)。
|
|
Post sampleDraft({
|
|
String id = 'draft-1',
|
|
int version = 3,
|
|
String content = '草稿正文',
|
|
bool withMedia = true,
|
|
}) => Post.fromJson({
|
|
...samplePostJson(id: id, status: 'draft', version: version),
|
|
'content': content,
|
|
if (!withMedia) 'media': const <Map<String, dynamic>>[],
|
|
});
|
|
|
|
CursorPage<Post> postPage(List<Post> items) =>
|
|
CursorPage(items: items, nextCursor: null, hasMore: false);
|
|
|
|
/// 假仓库:controller 测试注入行为并记录调用(Completer 控时序)。
|
|
/// 未注入 handler 的方法一律 UnimplementedError(误触发即测试失败)。
|
|
class FakeCommunityRepository implements CommunityRepository {
|
|
/// 调用日志,如 `feed:cursor=null`、`like:p-1`、`unlike:p-1`。
|
|
final List<String> calls = [];
|
|
|
|
Future<CursorPage<FeedCard>> Function(int? limit, String? cursor)? onFeed;
|
|
Future<LikeState> Function(String postId, bool target)? onLikeToggle;
|
|
Future<BookmarkState> Function(String postId, bool target)? onBookmarkToggle;
|
|
Future<Post> Function(String postId)? onGetPost;
|
|
Future<CursorPage<PostComment>> Function(String postId, String? cursor)?
|
|
onListComments;
|
|
Future<PostComment> Function(String postId, CreateCommentRequest request)?
|
|
onCreateComment;
|
|
Future<void> Function(String commentId)? onDeleteComment;
|
|
Future<FollowState> Function(String userId, bool target)? onFollowToggle;
|
|
Future<FollowStats> Function(String userId)? onGetFollowStats;
|
|
Future<CommunityStats> Function()? onGetMyCommunityStats;
|
|
Future<MediaUploadCredentials> Function(CreateMediaUploadRequest request)?
|
|
onCreateMediaUpload;
|
|
Future<MediaAsset> Function(String assetId)? onCompleteMediaUpload;
|
|
Future<Post> Function(CreatePostRequest request, String? idempotencyKey)?
|
|
onCreatePost;
|
|
Future<Post> Function(String postId, UpdatePostRequest request)? onUpdatePost;
|
|
Future<void> Function(String postId)? onDeletePost;
|
|
Future<CursorPage<Post>> Function(PostStatus? status)? onListMyPosts;
|
|
|
|
/// createPost 收到的幂等键顺序(同键重放断言用)。
|
|
final List<String?> idempotencyKeys = [];
|
|
|
|
/// 最近一次 createMediaUpload 的请求(purpose 断言用)。
|
|
CreateMediaUploadRequest? lastMediaUploadRequest;
|
|
|
|
@override
|
|
Future<CursorPage<FeedCard>> getFeed({int? limit, String? cursor}) {
|
|
calls.add('feed:cursor=$cursor');
|
|
return onFeed!(limit, cursor);
|
|
}
|
|
|
|
@override
|
|
Future<LikeState> likePost(String postId) {
|
|
calls.add('like:$postId');
|
|
return onLikeToggle!(postId, true);
|
|
}
|
|
|
|
@override
|
|
Future<LikeState> unlikePost(String postId) {
|
|
calls.add('unlike:$postId');
|
|
return onLikeToggle!(postId, false);
|
|
}
|
|
|
|
@override
|
|
Future<BookmarkState> bookmarkPost(String postId) {
|
|
calls.add('bookmark:$postId');
|
|
return onBookmarkToggle!(postId, true);
|
|
}
|
|
|
|
@override
|
|
Future<BookmarkState> unbookmarkPost(String postId) {
|
|
calls.add('unbookmark:$postId');
|
|
return onBookmarkToggle!(postId, false);
|
|
}
|
|
|
|
@override
|
|
Future<Post> getPost(String postId) {
|
|
calls.add('getPost:$postId');
|
|
return onGetPost!(postId);
|
|
}
|
|
|
|
@override
|
|
Future<MediaUploadCredentials> createMediaUpload(
|
|
CreateMediaUploadRequest request,
|
|
) {
|
|
calls.add('createUpload:${request.mimeType}:${request.byteSize}');
|
|
lastMediaUploadRequest = request;
|
|
return onCreateMediaUpload!(request);
|
|
}
|
|
|
|
@override
|
|
Future<MediaAsset> completeMediaUpload(String assetId) {
|
|
calls.add('complete:$assetId');
|
|
return onCompleteMediaUpload!(assetId);
|
|
}
|
|
|
|
@override
|
|
Future<Post> createPost(CreatePostRequest request, {String? idempotencyKey}) {
|
|
calls.add('createPost:${request.status?.name}:${request.content}');
|
|
idempotencyKeys.add(idempotencyKey);
|
|
return onCreatePost!(request, idempotencyKey);
|
|
}
|
|
|
|
@override
|
|
Future<Post> updatePost(String postId, UpdatePostRequest request) {
|
|
calls.add(
|
|
'updatePost:$postId:v${request.version}:publish=${request.publish}',
|
|
);
|
|
return onUpdatePost!(postId, request);
|
|
}
|
|
|
|
@override
|
|
Future<void> deletePost(String postId) {
|
|
calls.add('deletePost:$postId');
|
|
return onDeletePost!(postId);
|
|
}
|
|
|
|
@override
|
|
Future<CursorPage<Post>> listMyPosts({
|
|
int? limit,
|
|
String? cursor,
|
|
PostStatus? status,
|
|
}) {
|
|
calls.add('listMyPosts:${status?.name}');
|
|
return onListMyPosts == null
|
|
? Future.value(
|
|
const CursorPage(items: [], nextCursor: null, hasMore: false),
|
|
)
|
|
: onListMyPosts!(status);
|
|
}
|
|
|
|
@override
|
|
Future<CursorPage<PostComment>> listComments(
|
|
String postId, {
|
|
int? limit,
|
|
String? cursor,
|
|
}) {
|
|
calls.add('comments:$postId:cursor=$cursor');
|
|
return onListComments!(postId, cursor);
|
|
}
|
|
|
|
@override
|
|
Future<PostComment> createComment(
|
|
String postId,
|
|
CreateCommentRequest request,
|
|
) {
|
|
calls.add('createComment:$postId:${request.content}');
|
|
return onCreateComment!(postId, request);
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteComment(String commentId) {
|
|
calls.add('deleteComment:$commentId');
|
|
return onDeleteComment!(commentId);
|
|
}
|
|
|
|
@override
|
|
Future<FollowState> followUser(String userId) {
|
|
calls.add('follow:$userId');
|
|
return onFollowToggle!(userId, true);
|
|
}
|
|
|
|
@override
|
|
Future<FollowState> unfollowUser(String userId) {
|
|
calls.add('unfollow:$userId');
|
|
return onFollowToggle!(userId, false);
|
|
}
|
|
|
|
@override
|
|
Future<FollowStats> getFollowStats(String userId) {
|
|
calls.add('followStats:$userId');
|
|
return onGetFollowStats?.call(userId) ??
|
|
Future.value(FollowStats.fromJson(sampleFollowStatsJson()));
|
|
}
|
|
|
|
@override
|
|
Future<CommunityStats> getMyCommunityStats() {
|
|
calls.add('communityStats');
|
|
return onGetMyCommunityStats?.call() ??
|
|
// 缺省零值(契约:空数据返回 0 而非 null,且永不 404)。
|
|
Future.value(
|
|
CommunityStats.fromJson(
|
|
sampleCommunityStatsJson(
|
|
receivedLikeCount: 0,
|
|
publishedPostCount: 0,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<CursorPage<FeedCard>> listMyBookmarks({int? limit, String? cursor}) =>
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
Map<String, dynamic> sampleCommunityStatsJson({
|
|
int receivedLikeCount = 128,
|
|
int publishedPostCount = 12,
|
|
}) => {
|
|
'receivedLikeCount': receivedLikeCount,
|
|
'publishedPostCount': publishedPostCount,
|
|
};
|
|
|
|
Map<String, dynamic> sampleFollowStatsJson({
|
|
int followerCount = 24,
|
|
int followingCount = 7,
|
|
bool followedByMe = false,
|
|
}) => {
|
|
'followerCount': followerCount,
|
|
'followingCount': followingCount,
|
|
'followedByMe': followedByMe,
|
|
};
|