import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:ignis_app/features/homes/providers/homes_providers.dart'; import 'package:ignis_app/features/homes/services/geofence_automation_service.dart'; import 'package:ignis_app/features/shared/providers/core_providers.dart'; import 'package:ignis_app/models/home_config.dart'; import 'package:ignis_app/services/settings_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'test_support.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('loading current home syncs geofence with active ready home', () async { SharedPreferences.setMockInitialValues({ 'ignis_homes': '[{"id":"home-1","name":"Home 1","url":"https://one.example","latitude":55.75,"longitude":37.61,"geofenceEnabled":true,"geofenceRadiusMeters":700}]', 'ignis_current_home_id': 'home-1', }); final settingsService = SettingsService( credentialsStorage: InMemoryCredentialsStorage(), ); await settingsService.setHomeApiKey('home-1', 'key-1'); final geofenceService = _RecordingGeofenceAutomationService(); final container = ProviderContainer( overrides: [ settingsServiceProvider.overrideWithValue(settingsService), geofenceAutomationServiceProvider.overrideWithValue(geofenceService), ], ); addTearDown(container.dispose); await container.read(currentHomeProvider.notifier).load(); expect(container.read(currentHomeProvider)?.id, 'home-1'); expect(geofenceService.syncedHomes, hasLength(1)); expect(geofenceService.syncedHomes.single?.geofenceReady, isTrue); expect(geofenceService.syncedHomes.single?.geofenceRadiusMeters, 700); }); test('clearing current home disarms geofence automation', () async { SharedPreferences.setMockInitialValues({ 'ignis_homes': '[{"id":"home-1","name":"Home 1","url":"https://one.example","latitude":55.75,"longitude":37.61,"geofenceEnabled":true}]', 'ignis_current_home_id': 'home-1', }); final settingsService = SettingsService( credentialsStorage: InMemoryCredentialsStorage(), ); await settingsService.setHomeApiKey('home-1', 'key-1'); final geofenceService = _RecordingGeofenceAutomationService(); final container = ProviderContainer( overrides: [ settingsServiceProvider.overrideWithValue(settingsService), geofenceAutomationServiceProvider.overrideWithValue(geofenceService), ], ); addTearDown(container.dispose); await container.read(currentHomeProvider.notifier).load(); await container.read(currentHomeProvider.notifier).clear(); expect(container.read(currentHomeProvider), isNull); expect(geofenceService.syncedHomes, hasLength(2)); expect(geofenceService.syncedHomes.last, isNull); }); } class _RecordingGeofenceAutomationService extends GeofenceAutomationService { _RecordingGeofenceAutomationService() : super( settingsService: SettingsService( credentialsStorage: InMemoryCredentialsStorage(), ), ); final List syncedHomes = []; @override Future syncActiveHome(HomeConfig? home) async { syncedHomes.add(home); } }