feat(library): 支持设备追剧与观看历史

This commit is contained in:
Zhou Weidong committed 2026-10-27 09:54:51 +08:00
1 parent 4c3acb047c
commit 0d785bf1f8
12 files changed
+232 -76

No files matched your search

+12 -14
View File
@@ -187,22 +187,20 @@ void main() {
expect(find.byKey(const Key('favorites-retry')), findsOneWidget);
});
testWidgets(
'should_show_auth_gate_without_loading_favorites_when_signed_out',
(tester) async {
final auth = await _auth(signedIn: false);
final repository = InMemoryFavoriteRepository(
saved: {'signal'},
cards: [_card('signal')],
);
final favorites = FavoriteController(repository);
testWidgets('should_show_device_favorites_when_signed_out', (tester) async {
final auth = await _auth(signedIn: false);
final repository = InMemoryFavoriteRepository(
saved: {'signal'},
cards: [_card('signal')],
);
final favorites = FavoriteController(repository);
await _pumpPage(tester, auth: auth, favorites: favorites);
await _pumpPage(tester, auth: auth, favorites: favorites);
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
expect(repository.pageCalls, 0);
},
);
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
expect(find.text('Drama signal'), findsOneWidget);
expect(repository.pageCalls, 1);
});
testWidgets('should_open_player_when_favorite_card_is_tapped', (
tester,
+16 -3
View File
@@ -4,8 +4,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/remote/api_client.dart';
import 'package:fruitshorts/core/data/remote/auth_api.dart';
import 'package:fruitshorts/core/data/repositories/catalog_repository.dart';
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
import 'package:fruitshorts/core/data/services/auth_controller.dart';
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
import 'package:fruitshorts/core/platform/haptics.dart';
import 'package:fruitshorts/core/theme/app_colors.dart';
import 'package:fruitshorts/core/theme/app_theme.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
@@ -15,6 +18,8 @@ import 'package:shared_preferences_platform_interface/in_memory_shared_preferenc
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import '../support/fake_http_adapter.dart';
import '../support/fake_catalog.dart';
import '../support/fake_haptics.dart';
void main() {
setUp(() {
@@ -92,8 +97,15 @@ void main() {
);
await tester.pumpWidget(
ChangeNotifierProvider<AuthController>.value(
value: auth,
MultiProvider(
providers: [
ChangeNotifierProvider<AuthController>.value(value: auth),
Provider<WatchHistoryRepository>.value(
value: InMemoryWatchHistoryRepository(),
),
Provider<CatalogRepository>.value(value: FakeCatalogRepository()),
Provider<Haptics>.value(value: FakeHaptics()),
],
child: MaterialApp(
theme: AppTheme.dark(),
darkTheme: AppTheme.dark(),
@@ -111,7 +123,8 @@ void main() {
find.byKey(const Key('watch-records-page')),
);
expect(scaffold.backgroundColor, AppColors.ink);
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
expect(find.byKey(const Key('watch-records-empty')), findsOneWidget);
expect(find.byType(Scaffold), findsOneWidget);
});
}
+57
View File
@@ -5,6 +5,7 @@ import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/remote/api_client.dart';
import 'package:fruitshorts/core/data/remote/watch_api.dart';
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
import '../support/fake_http_adapter.dart';
@@ -94,4 +95,60 @@ void main() {
),
);
});
test('should_allow_guest_watch_read_save_and_clear', () async {
final adapter = FakeHttpAdapter((options) async {
if (options.method == 'GET' && options.uri.path.endsWith('/progress')) {
return FakeHttpResponse(
jsonEncode({
'code': 0,
'message': 'ok',
'data': {
'dramaId': '12',
'episodeId': '88',
'positionMs': '8000',
'durationMs': '60000',
},
}),
);
}
if (options.method == 'GET') {
return FakeHttpResponse(
jsonEncode({
'code': 0,
'message': 'ok',
'data': {'total': '0', 'list': <Object>[]},
}),
);
}
return FakeHttpResponse(
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
);
});
final repository = HttpWatchHistoryRepository(
WatchApi(ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter)),
);
expect((await repository.progressFor('12'))?.episodeId, '88');
expect((await repository.recent()).list, isEmpty);
await repository.save(
episodeId: '88',
position: const Duration(seconds: 8),
);
await repository.remove('12');
await repository.clear();
expect(
adapter.requests.map(
(request) => '${request.method} ${request.uri.path}',
),
[
'GET /app/watch/progress',
'GET /app/watch/recent',
'POST /app/watch/progress',
'DELETE /app/watch/progress/12',
'DELETE /app/watch/progress',
],
);
});
}
+99 -8
View File
@@ -174,9 +174,7 @@ void main() {
},
);
testWidgets('should_show_auth_gate_without_loading_records_when_signed_out', (
tester,
) async {
testWidgets('should_show_device_history_when_signed_out', (tester) async {
final auth = await _auth(signedIn: false);
final history = CountingWatchHistory([_progress('the-signal-demo')]);
@@ -187,8 +185,59 @@ void main() {
catalog: FakeCatalogRepository(),
);
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
expect(history.recentCalls, 0);
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
expect(find.text('The Signal'), findsOneWidget);
expect(history.recentCalls, 1);
});
testWidgets('should_reload_device_history_after_login', (tester) async {
final auth = _ToggleAuthController();
final history = CountingWatchHistory([_progress('the-signal-demo')]);
await _pumpPage(
tester,
auth: auth,
history: history,
catalog: FakeCatalogRepository(),
);
expect(history.recentCalls, 1);
auth.setSignedIn(true);
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(history.recentCalls, 2);
expect(find.text('The Signal'), findsOneWidget);
});
testWidgets('should_remove_and_clear_device_history_when_signed_out', (
tester,
) async {
final auth = await _auth(signedIn: false);
final history = CountingWatchHistory([
_progress('the-signal-demo'),
_progress('city-lights-demo'),
]);
await _pumpPage(
tester,
auth: auth,
history: history,
catalog: FakeCatalogRepository(),
);
await tester.tap(
find.byKey(const Key('watch-record-remove-the-signal-demo')),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('watch-record-the-signal-demo')), findsNothing);
expect(history.removeCalls, 1);
await tester.tap(find.byKey(const Key('watch-records-clear')));
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(FilledButton, 'Clear'));
await tester.pumpAndSettle();
expect(history.clearCalls, 1);
expect(find.byKey(const Key('watch-records-empty')), findsOneWidget);
});
testWidgets(
@@ -313,9 +362,17 @@ Future<AuthController> _auth({required bool signedIn}) async {
}
final client = ApiClient(
baseUrl: 'http://127.0.0.1:8081',
adapter: FakeHttpAdapter(
(_) async => FakeHttpResponse('{"code":0,"message":"ok","data":null}'),
),
adapter: FakeHttpAdapter((options) async {
if (options.uri.path == '/app/auth/login') {
return FakeHttpResponse(
'{"code":0,"message":"ok","data":{'
'"accessToken":"test-access","refreshToken":"test-refresh",'
'"expiresIn":7200,"user":{"uid":"1","nickname":"Viewer",'
'"avatarUrl":""}}}',
);
}
return FakeHttpResponse('{"code":0,"message":"ok","data":null}');
}),
);
final auth = AuthController(AuthApi(client), AuthSessionStore());
await auth.load();
@@ -353,6 +410,7 @@ class CountingWatchHistory extends InMemoryWatchHistoryRepository {
final List<WatchProgress> visibleRecords;
int recentCalls = 0;
int removeCalls = 0;
int clearCalls = 0;
@override
@@ -371,6 +429,12 @@ class CountingWatchHistory extends InMemoryWatchHistoryRepository {
);
}
@override
Future<void> remove(String dramaId) async {
removeCalls++;
await super.remove(dramaId);
}
@override
Future<void> clear() async {
clearCalls++;
@@ -440,3 +504,30 @@ class _AlwaysFailingWatchCatalog implements CatalogRepository {
@override
Future<HomeFeed> fetchHomeFeed() async => const HomeFeed();
}
class _ToggleAuthController extends AuthController {
_ToggleAuthController()
: super(
AuthApi(
ApiClient(
baseUrl: 'http://127.0.0.1:8081',
adapter: FakeHttpAdapter(
(_) async =>
FakeHttpResponse('{"code":0,"message":"ok","data":null}'),
),
),
),
AuthSessionStore(),
);
bool _signedIn = false;
@override
bool get isSignedIn => _signedIn;
void setSignedIn(bool value) {
if (_signedIn == value) return;
_signedIn = value;
notifyListeners();
}
}