fix tests for profile order, add correct migration

This commit is contained in:
stopflock
2026-03-02 13:56:07 -06:00
parent bc671c4efe
commit 57df8e83a7
4 changed files with 89 additions and 23 deletions
+37 -9
View File
@@ -12,6 +12,12 @@ class ProfileState extends ChangeNotifier {
final Set<NodeProfile> _enabled = {};
List<String> _customOrder = []; // List of profile IDs in user's preferred order
// Test-only getters for accessing private state
@visibleForTesting
List<NodeProfile> get internalProfiles => _profiles;
@visibleForTesting
Set<NodeProfile> get internalEnabled => _enabled;
// Callback for when a profile is deleted (used to clear stale sessions)
void Function(NodeProfile)? _onProfileDeleted;
@@ -75,7 +81,7 @@ class ProfileState extends ChangeNotifier {
_enabled.add(p);
_saveEnabledProfiles();
}
ProfileService().save(_profiles);
_saveProfilesToStorage();
notifyListeners();
}
@@ -89,7 +95,7 @@ class ProfileState extends ChangeNotifier {
_enabled.add(builtIn);
}
_saveEnabledProfiles();
ProfileService().save(_profiles);
_saveProfilesToStorage();
// Notify about profile deletion so other parts can clean up
_onProfileDeleted?.call(p);
@@ -100,6 +106,8 @@ class ProfileState extends ChangeNotifier {
// Reorder profiles (for drag-and-drop in settings)
void reorderProfiles(int oldIndex, int newIndex) {
final orderedProfiles = _getOrderedProfiles();
// Standard Flutter reordering logic
if (oldIndex < newIndex) {
newIndex -= 1;
}
@@ -138,16 +146,36 @@ class ProfileState extends ChangeNotifier {
// Save enabled profile IDs to disk
Future<void> _saveEnabledProfiles() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(
_enabledPrefsKey,
_enabled.map((p) => p.id).toList(),
);
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(
_enabledPrefsKey,
_enabled.map((p) => p.id).toList(),
);
} catch (e) {
// Fail gracefully in tests or if SharedPreferences isn't available
debugPrint('[ProfileState] Failed to save enabled profiles: $e');
}
}
// Save profiles to storage
Future<void> _saveProfilesToStorage() async {
try {
await ProfileService().save(_profiles);
} catch (e) {
// Fail gracefully in tests or if storage isn't available
debugPrint('[ProfileState] Failed to save profiles: $e');
}
}
// Save custom order to disk
Future<void> _saveCustomOrder() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(_profileOrderPrefsKey, _customOrder);
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(_profileOrderPrefsKey, _customOrder);
} catch (e) {
// Fail gracefully in tests or if SharedPreferences isn't available
debugPrint('[ProfileState] Failed to save custom order: $e');
}
}
}