import 'dart:async'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:ttstd_family_care/features/device/data/device_api.dart'; import 'package:ttstd_family_care/features/device/data/device_providers.dart'; import 'package:ttstd_family_care/features/device/data/device_repository_impl.dart'; import 'package:ttstd_family_care/features/device/domain/device_models.dart'; import 'package:ttstd_family_care/features/home/presentation/system_settings_controller.dart'; class _Repository extends DeviceRepositoryImpl { _Repository() : super(const DeviceApi()); DevicePolicy? cached = const DevicePolicy(bluetooth: 1); Completer response = Completer(); Completer saved = Completer(); int writes = 0; @override Future getCachedPolicy(String sn) async => cached; @override Future getPolicy(String sn) => response.future; @override Future togglePolicy(String sn, String field, int value) { writes++; return saved.future; } } void main() { late _Repository repository; late ProviderContainer container; final provider = systemSettingsControllerProvider('device-one'); setUp(() { repository = _Repository(); container = ProviderContainer(overrides: [ deviceRepositoryProvider.overrideWithValue(repository), ]); container.listen(provider, (_, _) {}); }); tearDown(() => container.dispose()); test('cached switches remain visible and locked until network completes', () async { await pumpEventQueue(); expect(container.read(provider).valueOrNull?.bluetooth, 1); expect(container.read(provider).isLoading, isTrue); await container.read(provider.notifier).toggle('bluetooth', false); expect(repository.writes, 0); repository.response.complete(const DevicePolicy(bluetooth: 0)); await pumpEventQueue(); expect(container.read(provider).isLoading, isFalse); expect(container.read(provider).valueOrNull?.bluetooth, 0); }); test('save and refresh stay locked and reject duplicate toggles', () async { repository.response.complete(const DevicePolicy(bluetooth: 1)); await pumpEventQueue(); repository.response = Completer(); final pending = container.read(provider.notifier).toggle('bluetooth', false); await container.read(provider.notifier).toggle('bluetooth', false); expect(repository.writes, 1); repository.saved.complete(null); await pumpEventQueue(); expect(container.read(provider).isLoading, isTrue); repository.response.complete(const DevicePolicy(bluetooth: 0)); await pending; expect(container.read(provider).isLoading, isFalse); expect(container.read(provider).valueOrNull?.bluetooth, 0); }); test('failed load unlocks and clears cached switches', () async { await pumpEventQueue(); repository.response.complete(null); await pumpEventQueue(); expect(container.read(provider).isLoading, isFalse); expect(container.read(provider).valueOrNull, isNull); }); test('failed save unlocks with previous switches and exposes error', () async { repository.response.complete(const DevicePolicy(bluetooth: 1)); await pumpEventQueue(); final pending = container.read(provider.notifier).toggle('bluetooth', false); repository.saved.complete('保存失败'); await pending; expect(container.read(provider).isLoading, isFalse); expect(container.read(provider).hasError, isTrue); expect(container.read(provider).valueOrNull?.bluetooth, 1); }); }