fix(plex): tolerate transient resource failures

This commit is contained in:
edde746
2026-05-16 22:54:53 +02:00
parent 3798a0c332
commit cfd4aa307d
7 changed files with 342 additions and 7 deletions
+142 -1
View File
@@ -1,17 +1,25 @@
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:plezy/connection/connection.dart';
import 'package:plezy/connection/connection_registry.dart';
import 'package:plezy/database/app_database.dart';
import 'package:plezy/models/plex/plex_home_user.dart';
import 'package:plezy/profiles/active_profile_binder.dart';
import 'package:plezy/profiles/active_profile_provider.dart';
import 'package:plezy/profiles/plex_home_service.dart';
import 'package:plezy/profiles/profile.dart';
import 'package:plezy/profiles/profile_connection.dart';
import 'package:plezy/profiles/profile_connection_registry.dart';
import 'package:plezy/profiles/profile_registry.dart';
import 'package:plezy/providers/multi_server_provider.dart';
import 'package:plezy/services/data_aggregation_service.dart';
import 'package:plezy/services/multi_server_manager.dart';
import 'package:plezy/services/plex_auth_service.dart';
import 'package:plezy/services/storage_service.dart';
import 'package:plezy/utils/media_server_http_client.dart';
import 'package:plezy/utils/media_server_timeouts.dart';
import '../test_helpers/prefs.dart';
@@ -27,6 +35,7 @@ void main() {
late ActiveProfileBinder binder;
late StorageService storage;
late bool shouldDeferInitialBind;
late List<PlexHomeUser> fetchedHomeUsers;
setUp(() async {
resetSharedPreferencesForTest();
@@ -35,11 +44,12 @@ void main() {
profileConnections = ProfileConnectionRegistry(db);
profiles = ProfileRegistry(db);
storage = await StorageService.getInstance();
fetchedHomeUsers = const [];
plexHome = PlexHomeService(
connections: connections,
profileConnections: profileConnections,
storage: storage,
plexHomeUserFetcher: (_) async => const [],
plexHomeUserFetcher: (_) async => fetchedHomeUsers,
);
activeProfile = ActiveProfileProvider(
registry: profiles,
@@ -161,4 +171,135 @@ void main() {
expect(binder.consumePlexHomePreVerified('plex-home-a'), isTrue);
});
});
group('Plex Home server refresh fallback', () {
Future<({String profileId, _CapturingMultiServerManager manager})> preparePlexHomeBind({
required bool protected,
required http.Client httpClient,
}) async {
binder.dispose();
multiServerProvider.dispose();
final capturingManager = _CapturingMultiServerManager();
manager = capturingManager;
multiServerProvider = MultiServerProvider(manager, DataAggregationService(manager));
binder = ActiveProfileBinder(
activeProfile: activeProfile,
connections: connections,
profileConnections: profileConnections,
serverManager: manager,
multiServerProvider: multiServerProvider,
pinPrompt: (_, {String? errorMessage}) async => null,
shouldDeferInitialBind: (_) async => false,
plexAuth: PlexAuthService.forTesting(http: MediaServerHttpClient(client: httpClient)),
);
final account = PlexAccountConnection(
id: 'plex.account',
accountToken: 'account-token',
clientIdentifier: 'client-id',
accountLabel: 'Owner',
servers: [_server(accessToken: 'account-server-token')],
createdAt: DateTime(2026, 1, 1),
);
await connections.upsert(account);
final homeUser = PlexHomeUser(
id: 1,
uuid: 'home-user-uuid',
title: 'Home User',
thumb: '',
hasPassword: protected,
restricted: false,
updatedAt: null,
admin: true,
guest: false,
protected: protected,
);
fetchedHomeUsers = [homeUser];
final profileId = plexHomeProfileId(accountConnectionId: account.id, homeUserUuid: homeUser.uuid);
await storage.savePlexHomeUsersCache(account.id, [homeUser.toJson()]);
await profileConnections.upsert(
ProfileConnection(
profileId: profileId,
connectionId: account.id,
userToken: 'home-user-token',
userIdentifier: homeUser.uuid,
tokenAcquiredAt: DateTime(2026, 1, 1),
),
);
await storage.setActiveProfileId(profileId);
await activeProfile.initialize();
return (profileId: profileId, manager: capturingManager);
}
test('uses cached server metadata with the active user token after transient resources failure', () async {
final prepared = await preparePlexHomeBind(
protected: false,
httpClient: MockClient((request) async {
throw http.ClientException('DNS failed', request.url);
}),
);
await binder.rebindActive();
expect(activeProfile.lastBindingSucceeded, isTrue);
expect(binder.debugLastBoundProfileId, prepared.profileId);
expect(prepared.manager.refreshCalls, 1);
expect(prepared.manager.lastConnection?.servers.single.accessToken, 'home-user-token');
expect(prepared.manager.lastConnection?.servers.single.clientIdentifier, 'srv-1');
});
test('does not use cached server metadata after cached token auth failure', () async {
final prepared = await preparePlexHomeBind(
protected: true,
httpClient: MockClient((request) async {
return http.Response('{"errors":[]}', 401, headers: {'content-type': 'application/json'});
}),
);
await binder.rebindActive();
expect(activeProfile.lastBindingSucceeded, isFalse);
expect(prepared.manager.refreshCalls, 0);
final pc = await profileConnections.get(prepared.profileId, 'plex.account');
expect(pc?.userToken, isNull);
});
});
}
PlexServer _server({required String accessToken}) {
return PlexServer(
name: 'Home Server',
clientIdentifier: 'srv-1',
accessToken: accessToken,
connections: [
PlexConnection(
protocol: 'https',
address: '192.168.1.3',
port: 32400,
uri: 'https://192-168-1-3.machine.plex.direct:32400',
local: true,
relay: false,
ipv6: false,
),
],
owned: true,
presence: true,
);
}
class _CapturingMultiServerManager extends MultiServerManager {
int refreshCalls = 0;
PlexAccountConnection? lastConnection;
@override
Future<Set<String>> refreshTokensForProfile(
PlexAccountConnection connection, {
Duration timeout = MediaServerTimeouts.perServerConnect,
}) async {
refreshCalls++;
lastConnection = connection;
return connection.servers.map((server) => server.clientIdentifier).toSet();
}
}