// Startup must survive a corrupt preference store. The store is // writable from outside the app (`defaults write` stores an int // where a bool belongs); shared_preferences then THROWS on the // typed getter, and before the hardening that exception escaped // from main() before the first frame — a black window with no // visible error (observed live on 2026-07-15: `hub.secure = 0`). import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:chain_studio/data/hub.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('loadPersistedEndpoint tolerates an int where a bool belongs', () async { SharedPreferences.setMockInitialValues({ 'hub.host': '127.0.0.1', 'hub.port': 50051, 'hub.secure': 0, // the corrupt value that killed startup }); // Must not throw; the int coerces to false. await HubService.instance.loadPersistedEndpoint(); expect(HubService.instance.currentEndpoint.host, '127.0.0.1'); expect(HubService.instance.currentEndpoint.port, 50051); expect(HubService.instance.currentEndpoint.secure, false); }); test('loadPersistedEndpoint tolerates wholesale wrong types', () async { SharedPreferences.setMockInitialValues({ 'hub.host': 12345, // wrong type → treated as absent 'hub.port': 'not-a-port', // wrong type → treated as absent 'hub.secure': 'yes', // wrong type → treated as absent }); await HubService.instance.loadPersistedEndpoint(); // No explicit endpoint restored → discovery/default path; the // call simply must not throw. }); test('theme + locale loaders tolerate wrong types', () async { SharedPreferences.setMockInitialValues({ 'theme.mode': 42, 'locale.code': true, }); final mode = await HubService.instance.loadThemeMode(); final locale = await HubService.instance.loadLocale(); expect(mode, ThemeModeValue.system); expect(locale.languageCode, 'en'); }); }