Files
patbond-flutter/test/helpers/community_test_helpers.dart
lixi 9892b65a19
CI / flutter-gates (push) Successful in 3m9s
新增:发布页真实化——媒体九宫格编辑态 + 建草稿/迁移发布两步 + 发布漏斗与媒体三段埋点,create 页 demo 发布流退役(T3-17)
- 新增 PostComposePage(P3 发布页,push 路由 post_form):正文/类目
  (general·help)/九宫格选图(组装 MediaUploader + UploadProgressOverlay,
  删格按列表序重发 position)/发布 gating(正文非空 + 在场媒体全 ready)
- 发布走「createPost(draft) → PATCH status=published」两步:迁移失败时草稿
  已在服务端,UI 明确提示「草稿已保存」;40905 重置幂等键、42203 提示等图、
  网络失败同键重放不重复建帖;40902 自动取新 version 重提一次
- 草稿:「存草稿」(manual) 与「取消 → 保留」(on_exit) 两路径 + 进页恢复最新
  一条草稿(提示条/清空);「不保留」软删服务端草稿(post_deleted 触点)
- 埋点:新增 post_analytics.dart(发布漏斗五事件 + 媒体三段,键集对齐字典
  v3);MediaUploader 挂接 started/succeeded/failed(含 cancelled) 与 attemptSeq;
  pageName 枚举补 post_form
- PostMediaEditGrid(同文件编辑态):3 列九宫格 + 虚线「+」格 + 删除角标 +
  进度覆盖层 + 失败整格重试;页级上传汇总条
- create 页只余 AI 生成模拟(M4 原样保留),demo 发布流与 AppState.posts /
  publishPost / updatePost 及其持久化一并退役;首页 story「发布」与 Feed 空态
  CTA 改为 push 真实发布页
- 测试 458 → 502(+44):发布页 widget 22、发布埋点 11、媒体三段 7、编辑态
  九宫格 4、主壳发布闭环 1;另加桌面真链路 integration_test(env 门控)
- flutter analyze 0 问题、dart format 无 diff;compose 六容器真链路实测通过
  (选图上传 → 发布 → Feed 置顶 → 第二客户端可见)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-10 14:42:13 +08:00

348 lines
9.9 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<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 = [];
@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}');
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!(userId);
}
@override
Future<CursorPage<FeedCard>> listMyBookmarks({int? limit, String? cursor}) =>
throw UnimplementedError();
}