Trakt was the one service outside the tracker abstraction. TraktScrobbleService re-implemented the whole playback lifecycle beside TrackerCoordinator, and TraktSyncService pushed watched state from its own WatchStateNotifier subscription, so the player called two objects at every lifecycle point and one watch could be written twice. TraktTracker now implements RealtimeScrobbleTracker like Simkl; the duplicated player call sites collapse to one each, and Trakt shares the coordinator's ID resolver instead of re-fetching show ids every episode. Capabilities are split so a tracker declares what it is rather than being special-cased: ScrobblePolicy carries each service's own resend/seek rules, EpisodeHistoryTracker names the remote row a per-item history write targets, and SeriesProgressTracker covers one-counter-per-series services. Writes from all four trackers go through a shared TrackerWriteQueue, generalised from the Trakt-only queue, with the legacy Trakt payload migrated on load. Trakt becomes the fourth TrackersProvider slot and TraktAccountProvider is deleted, so one object owns the active session per profile. Two failure paths found while consolidating are fixed here too. The queue's retries only ran on profile bind, connect and app foreground, so a network blip mid-session left queued watches waiting for the next foreground. OfflineModeProvider now notifies on connectivity changes, not just offline-state or WiFi-flag changes, and main.dart flushes the queue when the network returns. The queue also counted every failure toward the five attempts that permanently drop an item, so a rate limit or a service having a bad hour could discard a pending watch - the loss the queue exists to prevent. Only an answer about the write itself now spends an attempt: 4xx counts, while rate limits, 5xx, recoverable token-refresh failures and requests that never arrived do not. A back-off answer also defers that service for the rest of the flush, so a queue holding many rows does not fire all of them at a service that just asked for quiet.
181 lines
5.9 KiB
Dart
181 lines
5.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:plezy/media/media_kind.dart';
|
|
import 'package:plezy/models/trackers/anime_ids.dart';
|
|
import 'package:plezy/services/trackers/anilist/anilist_tracker.dart';
|
|
import 'package:plezy/services/trackers/mal/mal_tracker.dart';
|
|
import 'package:plezy/services/trackers/simkl/simkl_tracker.dart';
|
|
import 'package:plezy/services/trackers/tracker_id_resolver.dart';
|
|
import 'package:plezy/services/trackers/tracker_session.dart';
|
|
import 'package:plezy/services/trackers/trakt/trakt_tracker.dart';
|
|
import 'package:plezy/utils/external_ids.dart';
|
|
|
|
int _now() => DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
|
|
TrackerSession _traktSession() => TrackerSession(
|
|
accessToken: 'token',
|
|
refreshToken: 'refresh',
|
|
expiresAt: _now() + 86400,
|
|
scope: 'public',
|
|
createdAt: _now(),
|
|
);
|
|
|
|
TrackerSession _simklSession() => TrackerSession(accessToken: 'token', createdAt: _now());
|
|
|
|
TrackerSession _malSession() =>
|
|
TrackerSession(accessToken: 'token', refreshToken: 'refresh', expiresAt: _now() + 86400, createdAt: _now());
|
|
|
|
TrackerSession _anilistSession() => TrackerSession(accessToken: 'token', expiresAt: _now() + 86400, createdAt: _now());
|
|
|
|
TrackerRatingContext _ctx({
|
|
required MediaKind kind,
|
|
ExternalIds external = const ExternalIds(tvdb: 123, tmdb: 456, imdb: 'tt789'),
|
|
AnimeIds? anime,
|
|
int? season,
|
|
int? episodeNumber,
|
|
}) {
|
|
return TrackerRatingContext(
|
|
ids: TrackerIds(external: external, anime: anime),
|
|
kind: kind,
|
|
season: season,
|
|
episodeNumber: episodeNumber,
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
tearDown(() {
|
|
TraktTracker.instance.rebindSession(null, onSessionInvalidated: () {});
|
|
SimklTracker.instance.rebindSession(null, onSessionInvalidated: () {});
|
|
MalTracker.instance.rebindSession(null, onSessionInvalidated: () {});
|
|
AnilistTracker.instance.rebindSession(null, onSessionInvalidated: () {});
|
|
});
|
|
|
|
test('Trakt fetches the current episode rating by show ids and episode number', () async {
|
|
final client = MockClient((request) async {
|
|
expect(request.method, 'GET');
|
|
expect(request.url.path, '/sync/ratings/episodes');
|
|
return http.Response(
|
|
json.encode([
|
|
{
|
|
'rating': 8,
|
|
'show': {
|
|
'ids': {'tvdb': 123},
|
|
},
|
|
'episode': {'season': 1, 'number': 2},
|
|
},
|
|
]),
|
|
200,
|
|
);
|
|
});
|
|
TraktTracker.instance.rebindSession(_traktSession(), onSessionInvalidated: () {}, httpClient: client);
|
|
|
|
final score = await TraktTracker.instance.getRating(_ctx(kind: MediaKind.episode, season: 1, episodeNumber: 2));
|
|
|
|
expect(score, 8);
|
|
});
|
|
|
|
test('Simkl fetches the current show rating by external ids', () async {
|
|
final client = MockClient((request) async {
|
|
expect(request.method, 'GET');
|
|
expect(request.url.path, '/sync/ratings/shows');
|
|
return http.Response(
|
|
json.encode({
|
|
'shows': [
|
|
{
|
|
'user_rating': 7,
|
|
'show': {
|
|
'ids': {'tmdb': 456},
|
|
},
|
|
},
|
|
],
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
SimklTracker.instance.rebindSession(_simklSession(), onSessionInvalidated: () {}, httpClient: client);
|
|
|
|
final score = await SimklTracker.instance.getRating(_ctx(kind: MediaKind.show));
|
|
|
|
expect(score, 7);
|
|
});
|
|
|
|
test('Simkl checks the anime ratings bucket for non-movie anime', () async {
|
|
final paths = <String>[];
|
|
final client = MockClient((request) async {
|
|
expect(request.method, 'GET');
|
|
paths.add(request.url.path);
|
|
if (request.url.path == '/sync/ratings/shows') {
|
|
return http.Response(json.encode({'shows': []}), 200);
|
|
}
|
|
if (request.url.path == '/sync/ratings/anime') {
|
|
return http.Response(
|
|
json.encode({
|
|
'anime': [
|
|
{
|
|
'user_rating': 9,
|
|
'show': {
|
|
'ids': {'simkl': 987},
|
|
},
|
|
},
|
|
],
|
|
}),
|
|
200,
|
|
);
|
|
}
|
|
fail('Unexpected Simkl request: ${request.url.path}');
|
|
});
|
|
SimklTracker.instance.rebindSession(_simklSession(), onSessionInvalidated: () {}, httpClient: client);
|
|
|
|
final score = await SimklTracker.instance.getRating(_ctx(kind: MediaKind.show, anime: AnimeIds(simkl: 987)));
|
|
|
|
expect(score, 9);
|
|
expect(paths, ['/sync/ratings/shows', '/sync/ratings/anime']);
|
|
});
|
|
|
|
test('MAL fetches the current list score', () async {
|
|
final client = MockClient((request) async {
|
|
expect(request.method, 'GET');
|
|
expect(request.url.path, '/v2/anime/21');
|
|
expect(request.url.queryParameters['fields'], 'my_list_status');
|
|
return http.Response(
|
|
json.encode({
|
|
'my_list_status': {'score': 9},
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
MalTracker.instance.rebindSession(_malSession(), onSessionInvalidated: () {}, httpClient: client);
|
|
|
|
final score = await MalTracker.instance.getRating(_ctx(kind: MediaKind.show, anime: AnimeIds(mal: 21)));
|
|
|
|
expect(score, 9);
|
|
});
|
|
|
|
test('AniList fetches point-100 score and maps it to a 1-10 score', () async {
|
|
final client = MockClient((request) async {
|
|
final body = json.decode(request.body) as Map<String, dynamic>;
|
|
expect(body['variables'], {'mediaId': 21});
|
|
expect(body['query'], contains('mediaListEntry'));
|
|
expect(body['query'], contains('scoreRaw: score(format: POINT_100)'));
|
|
return http.Response(
|
|
json.encode({
|
|
'data': {
|
|
'Media': {
|
|
'mediaListEntry': {'scoreRaw': 80},
|
|
},
|
|
},
|
|
}),
|
|
200,
|
|
);
|
|
});
|
|
AnilistTracker.instance.rebindSession(_anilistSession(), onSessionInvalidated: () {}, httpClient: client);
|
|
|
|
final score = await AnilistTracker.instance.getRating(_ctx(kind: MediaKind.show, anime: AnimeIds(anilist: 21)));
|
|
|
|
expect(score, 8);
|
|
});
|
|
}
|