fix(player): 收藏与评论数实时刷新

This commit is contained in:
Zhou Weidong committed 2026-10-21 11:39:58 +08:00
1 parent 97bbced02c
commit 5e2b8d9e4d
11 files changed
+275 -29

No files matched your search

+27
View File
@@ -126,4 +126,31 @@ void main() {
'/app/comments/2111012753262120962',
);
});
test('should_parse_numeric_comment_count', () async {
final adapter = FakeHttpAdapter(
(_) async => FakeHttpResponse(envelope(17)),
);
final api = CommentApi(
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
);
final count = await api.count('5');
expect(count, 17);
expect(adapter.requests.single.method, 'GET');
expect(adapter.requests.single.uri.path, '/app/comments/count');
expect(adapter.requests.single.queryParameters, {'dramaId': '5'});
});
test('should_parse_string_comment_count', () async {
final adapter = FakeHttpAdapter(
(_) async => FakeHttpResponse(envelope('2111012753262120962')),
);
final api = CommentApi(
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
);
expect(await api.count('5'), 2111012753262120962);
});
}
+57 -2
View File
@@ -9,12 +9,16 @@ import 'package:fruitshorts/pages/drama_detail/drama_detail_logic.dart';
import '../support/fake_watch_history.dart';
/// 详情页逻辑依赖:票据仓库 + 收藏控制器(测试里都是内存实现)。
DramaDetailLogic _logic(Drama drama, FakeWatchHistory history) =>
DramaDetailLogic _logic(
Drama drama,
FakeWatchHistory history, {
FavoriteController? favorites,
}) =>
DramaDetailLogic(
drama: drama,
history: history,
playback: FakePlaybackRepository(),
favorites: FavoriteController(InMemoryFavoriteRepository()),
favorites: favorites ?? FavoriteController(InMemoryFavoriteRepository()),
);
Drama _card(String id) => Drama(
@@ -98,4 +102,55 @@ void main() {
expect(logic.drama.episodes, hasLength(3));
logic.dispose();
});
test('should_adjust_favorite_count_only_after_successful_changes', () async {
final repository = InMemoryFavoriteRepository(cards: [_favoriteDrama]);
final favorites = FavoriteController(repository);
await favorites.load();
final logic = _logic(
_favoriteDrama,
FakeWatchHistory(),
favorites: favorites,
);
expect(logic.adjustedFavoriteCount, 2);
expect(await logic.toggleFavorite(), isTrue);
expect(logic.adjustedFavoriteCount, 3);
repository.failToggle = true;
expect(await logic.toggleFavorite(), isFalse);
expect(logic.adjustedFavoriteCount, 3);
repository.failToggle = false;
expect(await logic.toggleFavorite(), isTrue);
expect(logic.adjustedFavoriteCount, 2);
logic.dispose();
});
test('should_decrement_favorite_count_from_saved_state', () async {
final repository = InMemoryFavoriteRepository(
saved: {'favorite-detail'},
cards: [_favoriteDrama],
);
final favorites = FavoriteController(repository);
await favorites.load();
final logic = _logic(
_favoriteDrama,
FakeWatchHistory(),
favorites: favorites,
);
expect(await logic.toggleFavorite(), isTrue);
expect(logic.adjustedFavoriteCount, 1);
logic.dispose();
});
}
const _favoriteDrama = Drama(
id: 'favorite-detail',
title: 'Favorite detail',
synopsis: 'favorite count',
posterUrl: '/files/favorite-detail.jpg',
favoriteCount: 2,
detailLoaded: true,
);
@@ -140,11 +140,23 @@ void main() {
expect(decoration().color, AppColors.card);
expect((decoration().border! as Border).top.color, AppColors.blue);
expect(
tester
.widget<Text>(find.byKey(const Key('detail-stat-favorite-value')))
.data,
'3200 Favorite',
);
await tester.tap(find.byKey(const Key('detail-favorite')));
await tester.pumpAndSettle();
expect(decoration().color, AppColors.blue);
expect(
tester
.widget<Text>(find.byKey(const Key('detail-stat-favorite-value')))
.data,
'3201 Favorite',
);
expect(
find.descendant(
of: find.byKey(const Key('detail-favorite')),
@@ -152,6 +164,16 @@ void main() {
),
findsOneWidget,
);
await tester.tap(find.byKey(const Key('detail-favorite')));
await tester.pumpAndSettle();
expect(
tester
.widget<Text>(find.byKey(const Key('detail-stat-favorite-value')))
.data,
'3200 Favorite',
);
});
testWidgets('should_expand_synopsis_when_it_exceeds_three_lines', (
+85 -1
View File
@@ -175,6 +175,68 @@ void main() {
expect(find.byKey(const Key('player-comments-sheet')), findsOneWidget);
});
testWidgets('should_refresh_comment_count_after_comment_sheet_closes', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.withData({
'auth.accessToken': 'access',
'auth.refreshToken': 'refresh',
'auth.user': '{"uid":"9","nickname":"Viewer","avatarUrl":""}',
});
final comments = FakeCommentRepository(countResult: 97);
await _pumpApp(tester, commentRepository: comments);
await _openSeriesPlayer(tester);
expect(
tester
.widget<Text>(find.byKey(const Key('player-rail-comments-count')))
.data,
'96',
);
await tester.tap(find.byKey(const Key('player-rail-comments')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('player-comments-sheet')), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(comments.countCalls, ['city-lights-demo']);
expect(
tester
.widget<Text>(find.byKey(const Key('player-rail-comments-count')))
.data,
'97',
);
});
testWidgets('should_update_home_feed_favorite_count_after_toggle', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.withData({
'auth.accessToken': 'access',
'auth.refreshToken': 'refresh',
'auth.user': '{"uid":"9","nickname":"Viewer","avatarUrl":""}',
});
final catalog = FakeCatalogRepository(dramas: [_countingDrama]);
await _pumpApp(tester, catalog: catalog);
expect(
tester.widget<Text>(find.byKey(const Key('rail-save-count'))).data,
'2',
);
await tester.tap(find.byKey(const Key('rail-save')));
await tester.pumpAndSettle();
expect(
tester.widget<Text>(find.byKey(const Key('rail-save-count'))).data,
'3',
);
});
testWidgets('should_show_center_toggle_only_when_paused', (tester) async {
await _pumpApp(tester);
await _openSeriesPlayer(tester);
@@ -1211,6 +1273,7 @@ Future<void> _pumpApp(
FakeCatalogRepository? catalog,
FakePlaybackRepository? playback,
FakeInteractionRepository? interactions,
FakeCommentRepository? commentRepository,
PlayerFactory? playerFactory,
}) async {
tester.view.physicalSize = size;
@@ -1226,7 +1289,7 @@ Future<void> _pumpApp(
watchHistoryRepository: InMemoryWatchHistoryRepository(),
favoriteRepository: InMemoryFavoriteRepository(cards: repository.dramas),
playbackRepository: playback ?? FakePlaybackRepository(),
commentRepository: FakeCommentRepository(),
commentRepository: commentRepository ?? FakeCommentRepository(),
interactionRepository: interactions ?? FakeInteractionRepository(),
noticeRepository: FakeNoticeRepository(),
haptics: FakeHaptics(),
@@ -1235,6 +1298,27 @@ Future<void> _pumpApp(
await tester.pumpAndSettle();
}
const _countingDrama = Drama(
id: 'counting-demo',
title: 'Counting Demo',
synopsis: 'Counting interaction totals',
posterUrl: '/files/counting-demo.png',
genre: 'Romance',
detailLoaded: true,
favoriteCount: 2,
commentCount: 3,
likeCount: 4,
episodes: [
Episode(
id: 'counting-demo-1',
number: 1,
title: 'Episode 1',
isFree: true,
videoUrl: 'https://example.test/counting-1.mp4',
),
],
);
class _RecordingPlayer implements PlayerPort {
bool playing = false;
@@ -42,6 +42,8 @@ class FakeCommentRepository implements CommentRepository {
this.publishError,
this.deleteError,
this.pageError,
this.countResult = 0,
this.countError,
}) : pages = pages ?? const [];
final List<CommentPage> pages;
@@ -49,9 +51,12 @@ class FakeCommentRepository implements CommentRepository {
final Object? publishError;
final Object? deleteError;
final Object? pageError;
final int countResult;
final Object? countError;
final List<CommentPageCall> pageCalls = [];
final List<PublishCall> publishCalls = [];
final List<String> deleteCalls = [];
final List<String> countCalls = [];
int _pageIndex = 0;
@override
@@ -75,6 +80,14 @@ class FakeCommentRepository implements CommentRepository {
return pages[_pageIndex++];
}
@override
Future<int> count(String dramaId) async {
countCalls.add(dramaId);
final error = countError;
if (error != null) throw error;
return countResult;
}
@override
Future<DramaComment> publish({
required String dramaId,