diff --git a/lib/core/data/remote/watch_api.dart b/lib/core/data/remote/watch_api.dart index e46e240..f918e8d 100644 --- a/lib/core/data/remote/watch_api.dart +++ b/lib/core/data/remote/watch_api.dart @@ -40,7 +40,7 @@ class WatchApi { return data is Map ? WatchProgress.fromJson(data) : null; } - /// 最近观看分页(服务端已按更新时间倒序)。 + /// 最近观看分页:`pageIndex`/`pageSize`,解包 `PageResult`。 Future> recent({ int pageIndex = 1, int pageSize = 20, @@ -49,8 +49,13 @@ class WatchApi { '/app/watch/recent', query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'}, ); - // 线上可能仍返回 List;分页壳与纯数组都要能渲染。 - return PageResult.parse(data, WatchProgress.fromJson); + if (data is! Map) { + return const PageResult(); + } + return PageResult.fromJson( + Map.from(data), + WatchProgress.fromJson, + ); } /// 删除某部剧的观看记录(记录页清单条用)。 diff --git a/test/watch/watch_api_test.dart b/test/watch/watch_api_test.dart index 74a9c8a..3214b41 100644 --- a/test/watch/watch_api_test.dart +++ b/test/watch/watch_api_test.dart @@ -51,23 +51,13 @@ void main() { expect(page.list.single.totalEpisodes, 68); }); - test('should_parse_recent_when_server_returns_legacy_list', () async { + test('should_return_empty_page_when_recent_list_is_empty', () async { final adapter = FakeHttpAdapter( (_) async => FakeHttpResponse( jsonEncode({ 'code': 0, 'message': 'ok', - 'data': [ - { - 'dramaId': '5', - 'episodeId': '7', - 'positionMs': '203904', - 'durationMs': '227882', - 'finished': false, - 'playCount': 8, - 'updatedAt': '2026-10-21T15:17:05.419', - }, - ], + 'data': {'total': '0', 'list': []}, }), ), ); @@ -77,25 +67,31 @@ void main() { final page = await api.recent(); - expect(page.total, 1); - expect(page.list.single.dramaId, '5'); - expect(page.list.single.episodeId, '7'); - expect(page.list.single.position.inMilliseconds, 203904); - }); - - test('should_return_empty_recent_when_data_is_null', () async { - final adapter = FakeHttpAdapter( - (_) async => FakeHttpResponse( - jsonEncode({'code': 0, 'message': 'ok', 'data': null}), - ), - ); - final api = WatchApi( - ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter), - ); - - final page = await api.recent(); - expect(page.total, 0); expect(page.list, isEmpty); }); + + test('should_throw_api_exception_when_recent_response_is_error', () async { + final adapter = FakeHttpAdapter( + (_) async => FakeHttpResponse( + jsonEncode({'code': 401, 'message': 'unauthorized', 'data': null}), + ), + ); + final api = WatchApi( + ApiClient( + baseUrl: 'http://127.0.0.1:8081', + adapter: adapter, + tokenRefresher: null, + ), + ); + + await expectLater( + () => api.recent(), + throwsA( + isA() + .having((e) => e.code, 'code', 401) + .having((e) => e.message, 'message', 'unauthorized'), + ), + ); + }); }