feat(umgebung): Server-Umgebung im Login-Screen waehlbar (Produktion/Test)
- Umgebungen (Name, API-URL, Keycloak-Issuer) kommen aus assets/hl_server_config.json statt hartcodiertem BackendConfig.prod; Testumgebung 192.168.1.9:3001 ist dort hinterlegt - AppBloc laedt die Config, aktiviert die gespeicherte Wahl (Fallback Default) und registriert das Networking; Wechsel persistiert per SharedPreferences und registriert neu - Bloc-Baum ist pro Umgebung gekeyt: beim Wechsel entstehen alle Blocs mit dem neuen API-Client neu - Login-Screen: Zahnrad oben rechts oeffnet Einstellungen mit Umgebungs- Cards (Name + IP:Port); Hinweis auf aktive Umgebung unter dem Button - Fahrzeugauswahl pro Umgebung getrennt (eigene DB = eigene Car-IDs) - Tests fuer Config-Parsing, Auswahl-Fallback, Card-Wechsel, Re-Registrierung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
113
test/environment_settings_page_test.dart
Normal file
113
test/environment_settings_page_test.dart
Normal file
@ -0,0 +1,113 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_bloc.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_events.dart';
|
||||
import 'package:hl_lieferservice/bloc/app_states.dart';
|
||||
import 'package:hl_lieferservice/data/network/backend_config.dart';
|
||||
import 'package:hl_lieferservice/data/network/backend_environment.dart';
|
||||
import 'package:hl_lieferservice/data/network/environment_repository.dart';
|
||||
import 'package:hl_lieferservice/feature/settings/presentation/environment_settings_page.dart';
|
||||
|
||||
/// Liest die echte Asset-Config, speichert die Auswahl im Speicher.
|
||||
class _FakeRepository extends EnvironmentRepository {
|
||||
_FakeRepository(this.selectedId);
|
||||
|
||||
String? selectedId;
|
||||
|
||||
@override
|
||||
Future<ServerCatalog> loadCatalog(String assetPath) async =>
|
||||
ServerCatalog.fromJson(
|
||||
jsonDecode(File(assetPath).readAsStringSync()) as Map<String, dynamic>,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<String?> loadSelectedId() async => selectedId;
|
||||
|
||||
@override
|
||||
Future<void> saveSelectedId(String id) async => selectedId = id;
|
||||
}
|
||||
|
||||
void main() {
|
||||
late _FakeRepository repo;
|
||||
late List<BackendConfig> activated;
|
||||
late AppBloc bloc;
|
||||
|
||||
setUp(() {
|
||||
repo = _FakeRepository(null);
|
||||
activated = [];
|
||||
bloc = AppBloc(repository: repo, activate: activated.add);
|
||||
});
|
||||
|
||||
tearDown(() => bloc.close());
|
||||
|
||||
test('ohne gespeicherte Wahl startet die Default-Umgebung', () async {
|
||||
bloc.add(AppLoadConfig(path: 'hl_server_config.json'));
|
||||
final state = await bloc.stream.firstWhere((s) => s is AppConfigLoaded)
|
||||
as AppConfigLoaded;
|
||||
|
||||
expect(state.active.id, 'production');
|
||||
expect(activated.single.apiBaseUrl, 'http://192.168.1.9:3000');
|
||||
});
|
||||
|
||||
test('gespeicherte, aber unbekannte Umgebung fällt auf Default', () async {
|
||||
repo.selectedId = 'gibt-es-nicht';
|
||||
bloc.add(AppLoadConfig(path: 'hl_server_config.json'));
|
||||
final state = await bloc.stream.firstWhere((s) => s is AppConfigLoaded)
|
||||
as AppConfigLoaded;
|
||||
|
||||
expect(state.active.id, 'production');
|
||||
});
|
||||
|
||||
testWidgets('Card antippen wechselt Umgebung, speichert und schließt',
|
||||
(tester) async {
|
||||
await tester.runAsync(() async {
|
||||
bloc.add(AppLoadConfig(path: 'hl_server_config.json'));
|
||||
await bloc.stream.firstWhere((s) => s is AppConfigLoaded);
|
||||
});
|
||||
|
||||
await tester.pumpWidget(
|
||||
BlocProvider.value(
|
||||
value: bloc,
|
||||
child: MaterialApp(
|
||||
home: Builder(
|
||||
builder: (context) => Scaffold(
|
||||
body: TextButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const EnvironmentSettingsPage(),
|
||||
),
|
||||
),
|
||||
child: const Text('open'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('open'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Beide Umgebungen aus der Config als Card mit Name + IP:Port.
|
||||
expect(find.text('Produktion'), findsOneWidget);
|
||||
expect(find.text('192.168.1.9:3000'), findsOneWidget);
|
||||
expect(find.text('Testumgebung'), findsOneWidget);
|
||||
expect(find.text('192.168.1.9:3001'), findsOneWidget);
|
||||
expect(find.byIcon(Icons.check_circle), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Testumgebung'));
|
||||
await tester.runAsync(
|
||||
() => bloc.stream.firstWhere(
|
||||
(s) => s is AppConfigLoaded && s.active.id == 'test',
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(EnvironmentSettingsPage), findsNothing);
|
||||
expect(repo.selectedId, 'test');
|
||||
expect(activated.last.apiBaseUrl, 'http://192.168.1.9:3001');
|
||||
});
|
||||
}
|
||||
73
test/server_catalog_test.dart
Normal file
73
test/server_catalog_test.dart
Normal file
@ -0,0 +1,73 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:holzleitner_api/holzleitner_api.dart';
|
||||
import 'package:hl_lieferservice/data/network/backend_config.dart';
|
||||
import 'package:hl_lieferservice/data/network/network_locator.dart';
|
||||
import 'package:hl_lieferservice/data/network/backend_environment.dart';
|
||||
|
||||
void main() {
|
||||
ServerCatalog load() => ServerCatalog.fromJson(
|
||||
jsonDecode(File('assets/hl_server_config.json').readAsStringSync())
|
||||
as Map<String, dynamic>,
|
||||
);
|
||||
|
||||
test('ausgelieferte Config enthält Produktion (Default) und Test', () {
|
||||
final catalog = load();
|
||||
|
||||
expect(catalog.defaultEnvironment.id, 'production');
|
||||
expect(catalog.byId('production')!.hostAndPort, '192.168.1.9:3000');
|
||||
expect(catalog.byId('test')!.hostAndPort, '192.168.1.9:3001');
|
||||
expect(catalog.keycloakClientId, 'holzleitner-app');
|
||||
});
|
||||
|
||||
test('configFor übernimmt Umgebung und globale Keycloak-Daten', () {
|
||||
final catalog = load();
|
||||
final config = catalog.configFor(catalog.byId('test')!);
|
||||
|
||||
expect(config.apiBaseUrl, 'http://192.168.1.9:3001');
|
||||
expect(config.keycloakRedirectUrl, 'holzleitner://oauth2redirect');
|
||||
});
|
||||
|
||||
test('unbekannte Default-Umgebung wird abgelehnt', () {
|
||||
expect(
|
||||
() => ServerCatalog.fromJson({
|
||||
'defaultEnvironment': 'nope',
|
||||
'keycloak': {'clientId': 'c', 'redirectUrl': 'r://x'},
|
||||
'environments': [
|
||||
{
|
||||
'id': 'a',
|
||||
'name': 'A',
|
||||
'apiBaseUrl': 'http://h:1/',
|
||||
'keycloakIssuerUrl': 'http://k/realms/x',
|
||||
},
|
||||
],
|
||||
}),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
|
||||
test('registerNetworking ersetzt beim Umgebungswechsel die Instanzen', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final catalog = load();
|
||||
final locator = GetIt.asNewInstance();
|
||||
|
||||
registerNetworking(
|
||||
locator: locator,
|
||||
config: catalog.configFor(catalog.byId('production')!),
|
||||
);
|
||||
final firstApi = locator<HolzleitnerApi>();
|
||||
|
||||
registerNetworking(
|
||||
locator: locator,
|
||||
config: catalog.configFor(catalog.byId('test')!),
|
||||
);
|
||||
|
||||
expect(locator<BackendConfig>().apiBaseUrl, 'http://192.168.1.9:3001');
|
||||
expect(identical(locator<HolzleitnerApi>(), firstApi), isFalse);
|
||||
expect(locator<HolzleitnerApi>().dio.options.baseUrl,
|
||||
'http://192.168.1.9:3001');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user