diff --git a/services/service_seerr/lib/src/seerr_api.dart b/services/service_seerr/lib/src/seerr_api.dart index 30037ff2..8b2e3e34 100644 --- a/services/service_seerr/lib/src/seerr_api.dart +++ b/services/service_seerr/lib/src/seerr_api.dart @@ -480,9 +480,14 @@ class SeerrApi { /// The signed-in user's watchlist. /// - /// Watchlist entries can come back in a Plex-flavoured shape carrying a - /// `tmdbId` but no TMDB `id`, so entries are normalised before parsing and - /// anything with no usable id is dropped instead of failing the whole page. + /// Entries are bare: a title, a media type and a `tmdbId`. For a user + /// without a Plex token they are rows of Seerr's own watchlist table, whose + /// `id` is the row's number rather than the title's, so the TMDB id is read + /// from `tmdbId` and anything without one is dropped. + /// + /// No shape carries artwork, so each entry is filled from its movie or TV + /// details, as Seerr's own web page does per card. An entry whose details + /// cannot be read stays in the row with only its title. Future> getWatchlist() async { try { final Response resp = @@ -490,16 +495,32 @@ class SeerrApi { final Map data = resp.data as Map; final List raw = (data['results'] as List?) ?? []; - final List> results = >[]; + final List bare = []; for (final dynamic e in raw) { if (e is! Map) continue; - e['id'] ??= e['tmdbId']; - if (e['id'] != null) results.add(e); + final Object? tmdbId = e['tmdbId'] ?? e['id']; + if (tmdbId is! int) continue; + bare.add( + SeerrDiscoverResult( + id: tmdbId, + mediaType: e['mediaType'] as String? ?? 'movie', + title: e['title'] as String?, + ), + ); } - data['results'] = results; - return SeerrDiscoverPage.fromJson(data).results; + return Future.wait(bare.map(_withDetails)); } on DioException catch (e) { throw NetworkException.fromDio(e); } } + + Future _withDetails(SeerrDiscoverResult item) async { + try { + final SeerrDiscoverResult full = + await getMediaDetails(item.mediaType, item.id); + return full.copyWith(id: item.id, mediaType: item.mediaType); + } on Object { + return item; + } + } } diff --git a/services/service_seerr/test/seerr_watchlist_test.dart b/services/service_seerr/test/seerr_watchlist_test.dart new file mode 100644 index 00000000..ae322f6e --- /dev/null +++ b/services/service_seerr/test/seerr_watchlist_test.dart @@ -0,0 +1,225 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:service_seerr/service_seerr.dart'; + +/// A Seerr whose `/discover/watchlist` answers with [watchlist] and whose +/// `/movie/{id}` and `/tv/{id}` answer from [details]. A title missing from +/// [details] answers 500. +class _FakeSeerr implements HttpClientAdapter { + _FakeSeerr(this.watchlist, this.details); + + final List> watchlist; + final Map> details; + final List asked = []; + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + asked.add(options.path); + if (options.path.endsWith('/discover/watchlist')) { + return _json({ + 'page': 1, + 'totalPages': 1, + 'totalResults': watchlist.length, + 'results': watchlist, + }); + } + final RegExpMatch? m = + RegExp(r'/(movie|tv)/(\d+)$').firstMatch(options.path); + final Map? body = + m == null ? null : details['${m[1]}/${m[2]}']; + return body == null ? _json({}, 500) : _json(body); + } + + ResponseBody _json(Map body, [int status = 200]) => + ResponseBody.fromString( + jsonEncode(body), + status, + headers: >{ + Headers.contentTypeHeader: [Headers.jsonContentType], + }, + ); + + @override + void close({bool force = false}) {} +} + +SeerrApi _api(_FakeSeerr fake) => SeerrApi( + Dio(BaseOptions(baseUrl: 'http://seerr.test/')) + ..httpClientAdapter = fake, + ); + +/// A row of Seerr's own watchlist table, as `/discover/watchlist` sends it +/// for a user with no Plex token: `id` is the row's own number. +Map _localRow(int row, String type, int tmdbId, String title) => + { + 'id': row, + 'ratingKey': '', + 'mediaType': type, + 'title': title, + 'tmdbId': tmdbId, + 'createdAt': '2026-11-20T10:00:00.000Z', + 'updatedAt': '2026-11-20T10:00:00.000Z', + }; + +void main() { + test('a local watchlist entry opens its TMDB title, not its row', () async { + final _FakeSeerr fake = _FakeSeerr( + >[ + _localRow(1, 'tv', 95396, 'Severance'), + _localRow(2, 'movie', 718930, 'Bullet Train'), + ], + >{ + 'tv/95396': { + 'id': 95396, + 'name': 'Severance', + 'posterPath': '/sev.jpg', + 'firstAirDate': '2022-02-17', + }, + 'movie/718930': { + 'id': 718930, + 'title': 'Bullet Train', + 'posterPath': '/bt.jpg', + 'releaseDate': '2022-08-03', + 'mediaInfo': { + 'id': 9, + 'mediaType': 'movie', + 'tmdbId': 718930, + 'status': 5, + }, + }, + }, + ); + + final List list = await _api(fake).getWatchlist(); + + expect(list.map((SeerrDiscoverResult r) => r.id), [95396, 718930]); + expect( + list.map((SeerrDiscoverResult r) => r.mediaType), + ['tv', 'movie'], + ); + expect(fake.asked, isNot(contains(endsWith('/tv/1')))); + expect(fake.asked, isNot(contains(endsWith('/movie/2')))); + }); + + test('watchlist entries carry their poster and status', () async { + final _FakeSeerr fake = _FakeSeerr( + >[_localRow(2, 'movie', 718930, 'Bullet Train')], + >{ + 'movie/718930': { + 'id': 718930, + 'title': 'Bullet Train', + 'posterPath': '/bt.jpg', + 'releaseDate': '2022-08-03', + 'mediaInfo': { + 'id': 9, + 'mediaType': 'movie', + 'tmdbId': 718930, + 'status': 5, + }, + }, + }, + ); + + final SeerrDiscoverResult item = (await _api(fake).getWatchlist()).single; + + expect(item.posterPath, '/bt.jpg'); + expect(item.year, '2022'); + expect(item.mediaInfo?.status, 5); + expect(item.isMovie, isTrue); + }); + + test('a Plex watchlist entry keeps its TMDB id and gets a poster', () async { + final _FakeSeerr fake = _FakeSeerr( + >[ + { + 'id': 95396, + 'ratingKey': '5d9c086fe9d5a1001f4d5c6b', + 'title': 'Severance', + 'mediaType': 'tv', + 'tmdbId': 95396, + }, + ], + >{ + 'tv/95396': { + 'id': 95396, + 'name': 'Severance', + 'posterPath': '/sev.jpg', + }, + }, + ); + + final SeerrDiscoverResult item = (await _api(fake).getWatchlist()).single; + + expect(item.id, 95396); + expect(item.mediaType, 'tv'); + expect(item.posterPath, '/sev.jpg'); + }); + + test('an entry with only an id opens and dresses by that id', () async { + final _FakeSeerr fake = _FakeSeerr( + >[ + { + 'id': 95396, + 'ratingKey': '5d9c086fe9d5a1001f4d5c6b', + 'title': 'Severance', + 'mediaType': 'tv', + }, + ], + >{ + 'tv/95396': { + 'id': 95396, + 'name': 'Severance', + 'posterPath': '/sev.jpg', + }, + }, + ); + + final SeerrDiscoverResult item = (await _api(fake).getWatchlist()).single; + + expect(item.id, 95396); + expect(fake.asked, contains(endsWith('/tv/95396'))); + expect(item.posterPath, '/sev.jpg'); + }); + + test('a title whose details fail still shows, by its name', () async { + final _FakeSeerr fake = _FakeSeerr( + >[ + _localRow(1, 'tv', 95396, 'Severance'), + _localRow(2, 'movie', 718930, 'Bullet Train'), + ], + >{ + 'movie/718930': { + 'id': 718930, + 'title': 'Bullet Train', + 'posterPath': '/bt.jpg', + }, + }, + ); + + final List list = await _api(fake).getWatchlist(); + + expect(list, hasLength(2)); + expect(list.first.id, 95396); + expect(list.first.displayTitle, 'Severance'); + expect(list.first.posterPath, isNull); + expect(list.last.posterPath, '/bt.jpg'); + }); + + test('an entry with no TMDB id is dropped', () async { + final _FakeSeerr fake = _FakeSeerr( + >[ + {'ratingKey': 'abc', 'title': 'Home Video'}, + ], + >{}, + ); + + expect(await _api(fake).getWatchlist(), isEmpty); + }); +}