fix: reload migrated profiles before startup
This commit is contained in:
@@ -2,8 +2,10 @@ import 'dart:async';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.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_provider.dart';
|
||||
import 'package:plezy/profiles/plex_home_service.dart';
|
||||
import 'package:plezy/profiles/profile.dart';
|
||||
@@ -13,6 +15,31 @@ import 'package:plezy/services/storage_service.dart';
|
||||
|
||||
import '../test_helpers/prefs.dart';
|
||||
|
||||
PlexHomeUser _homeUser(String uuid, {String name = 'Home User'}) {
|
||||
return PlexHomeUser(
|
||||
id: 1,
|
||||
uuid: uuid,
|
||||
title: name,
|
||||
thumb: '',
|
||||
hasPassword: false,
|
||||
restricted: false,
|
||||
updatedAt: null,
|
||||
admin: true,
|
||||
guest: false,
|
||||
protected: false,
|
||||
);
|
||||
}
|
||||
|
||||
PlexAccountConnection _account(String id) {
|
||||
return PlexAccountConnection(
|
||||
id: id,
|
||||
accountToken: 'token-$id',
|
||||
clientIdentifier: 'client-$id',
|
||||
accountLabel: 'Plex',
|
||||
createdAt: DateTime(2026, 1, 1),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
late AppDatabase db;
|
||||
late ProfileRegistry registry;
|
||||
@@ -20,6 +47,7 @@ void main() {
|
||||
late PlexHomeService plexHome;
|
||||
late ActiveProfileProvider provider;
|
||||
late StorageService storage;
|
||||
late List<PlexHomeUser> fetchedHomeUsers;
|
||||
|
||||
setUp(() async {
|
||||
resetSharedPreferencesForTest();
|
||||
@@ -27,12 +55,12 @@ void main() {
|
||||
registry = ProfileRegistry(db);
|
||||
connections = ConnectionRegistry(db);
|
||||
storage = await StorageService.getInstance();
|
||||
fetchedHomeUsers = const [];
|
||||
plexHome = PlexHomeService(
|
||||
connections: connections,
|
||||
profileConnections: ProfileConnectionRegistry(db),
|
||||
storage: storage,
|
||||
// No accounts in tests, so the fetcher is never called.
|
||||
plexHomeUserFetcher: (_) async => const [],
|
||||
plexHomeUserFetcher: (_) async => fetchedHomeUsers,
|
||||
);
|
||||
provider = ActiveProfileProvider(
|
||||
registry: registry,
|
||||
@@ -77,6 +105,25 @@ void main() {
|
||||
expect(provider.activeId, isNull);
|
||||
});
|
||||
|
||||
test('reloadFromStorage resolves Plex Home profile added after early initialize', () async {
|
||||
await provider.initialize();
|
||||
expect(provider.profiles, isEmpty);
|
||||
|
||||
final account = _account('plex.migrated');
|
||||
final user = _homeUser('home-user-1', name: 'Migrated User');
|
||||
fetchedHomeUsers = [user];
|
||||
await connections.upsert(account);
|
||||
await storage.savePlexHomeUsersCache(account.id, [user.toJson()]);
|
||||
final profileId = plexHomeProfileId(accountConnectionId: account.id, homeUserUuid: user.uuid);
|
||||
await storage.setActiveProfileId(profileId);
|
||||
|
||||
await provider.reloadFromStorage();
|
||||
|
||||
expect(provider.profiles.map((p) => p.id), contains(profileId));
|
||||
expect(provider.activeId, profileId);
|
||||
expect(provider.active?.displayName, 'Migrated User');
|
||||
});
|
||||
|
||||
test('initialize clears storage when stored id is stale', () async {
|
||||
// A previously-active profile that was deleted should not keep
|
||||
// storage-scoped settings under the removed profile id.
|
||||
|
||||
@@ -110,6 +110,31 @@ void main() {
|
||||
expect(service.current[acct.id]!.first.uuid, 'seeded-uuid');
|
||||
});
|
||||
|
||||
test('reloadFromStorage picks up caches written after startup', () async {
|
||||
final refreshBlocker = Completer<List<PlexHomeUser>>();
|
||||
service = PlexHomeService(
|
||||
connections: connections,
|
||||
profileConnections: profileConnections,
|
||||
storage: storage,
|
||||
plexHomeUserFetcher: (_) => refreshBlocker.future,
|
||||
);
|
||||
addTearDown(() {
|
||||
if (!refreshBlocker.isCompleted) refreshBlocker.complete(const []);
|
||||
});
|
||||
|
||||
await service.start();
|
||||
expect(service.current, isEmpty);
|
||||
|
||||
final acct = _account('plex.migrated');
|
||||
await connections.upsert(acct);
|
||||
await storage.savePlexHomeUsersCache(acct.id, [_user('migrated-home-user').toJson()]);
|
||||
|
||||
await service.reloadFromStorage();
|
||||
|
||||
expect(service.current[acct.id], hasLength(1));
|
||||
expect(service.current[acct.id]!.single.uuid, 'migrated-home-user');
|
||||
});
|
||||
|
||||
test('concurrent start calls await the same in-flight startup', () async {
|
||||
service = PlexHomeService(
|
||||
connections: connections,
|
||||
|
||||
Reference in New Issue
Block a user