支持现场服务器地址配置
This commit is contained in:
301
test/server_configuration_test.dart
Normal file
301
test/server_configuration_test.dart
Normal file
@@ -0,0 +1,301 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/env/env_config.dart';
|
||||
import 'package:maibu_satabot_v2/core/env/server_configuration.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/dio_client.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/interfaces/mqtt_client.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/models/mqtt_config.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/mqtt_manager.dart';
|
||||
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_state.dart';
|
||||
import 'package:maibu_satabot_v2/features/server_configuration/presentation/server_configuration_page.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'support/region_test_localizations.dart';
|
||||
|
||||
class _SignedOutAuth extends Fake implements AuthCubit {
|
||||
@override
|
||||
AuthState get state => AuthUnauthenticated();
|
||||
|
||||
@override
|
||||
Stream<AuthState> get stream => const Stream.empty();
|
||||
}
|
||||
|
||||
class _MqttClient extends Fake implements MqttClient {
|
||||
final initialConnection = Completer<void>();
|
||||
final attempts = <MqttConfig>[];
|
||||
int disconnects = 0;
|
||||
|
||||
@override
|
||||
Future<void> connect(MqttConfig config) async {
|
||||
attempts.add(config);
|
||||
if (attempts.length == 1) await initialConnection.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> disconnect() async => disconnects++;
|
||||
}
|
||||
|
||||
class _HttpAdapter extends Fake implements HttpClientAdapter {
|
||||
final requests = <Uri>[];
|
||||
|
||||
@override
|
||||
Future<ResponseBody> fetch(
|
||||
RequestOptions options,
|
||||
Stream<Uint8List>? requestStream,
|
||||
Future<void>? cancelFuture,
|
||||
) async {
|
||||
requests.add(options.uri);
|
||||
return ResponseBody.fromString('{}', 200, headers: {
|
||||
Headers.contentTypeHeader: [Headers.jsonContentType],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
setUpAll(RegionTestLocalizations.loadAssets);
|
||||
|
||||
late SharedPreferences prefs;
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
EnvConfig.loadServerConfiguration(prefs);
|
||||
sl.registerSingleton<SharedPreferences>(prefs);
|
||||
});
|
||||
tearDown(() => sl.reset());
|
||||
|
||||
test('keeps the existing addresses for test and production', () {
|
||||
final testConfig = ServerConfiguration(production: false);
|
||||
final productionConfig = ServerConfiguration(production: true);
|
||||
expect(ServerService.values, hasLength(10));
|
||||
expect(
|
||||
testConfig.address(ServerService.http).toString(),
|
||||
'http://1.95.137.212:59003',
|
||||
);
|
||||
expect(testConfig.address(ServerService.tcp).port, 59004);
|
||||
expect(testConfig.address(ServerService.mqtt).port, 59007);
|
||||
expect(productionConfig.address(ServerService.http).port, 8081);
|
||||
expect(productionConfig.address(ServerService.tcp).port, 9001);
|
||||
expect(productionConfig.address(ServerService.mqtt).port, 1883);
|
||||
expect(
|
||||
testConfig.address(ServerService.webRtc).toString(),
|
||||
'http://1.95.137.212:1985/rtc/v1/play/',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.droneMqtt).toString(),
|
||||
'ws://1.95.137.212:8083/mqtt',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.geoServer).toString(),
|
||||
'http://1.95.137.212:7900/geoserver',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.pathPlanning).toString(),
|
||||
'https://servicepathplan.satabot.com/api/path',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.ai).toString(),
|
||||
'http://1.95.137.212:30009',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.update).toString(),
|
||||
'http://8.159.134.0:8012/api/update/check',
|
||||
);
|
||||
expect(
|
||||
testConfig.address(ServerService.stationWeb).toString(),
|
||||
'https://pvs.satabot.com/',
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'saved overrides are applied to HTTP, TCP, MQTT and WebRTC after reload',
|
||||
() async {
|
||||
const host = '192.168.1.31';
|
||||
await EnvConfig.saveServerConfiguration(
|
||||
prefs,
|
||||
ServerConfiguration(
|
||||
production: EnvConfig.isProduction,
|
||||
sharedHost: host,
|
||||
overrides: const {
|
||||
ServerService.http: '8800',
|
||||
ServerService.tcp: '192.168.1.32:9002',
|
||||
ServerService.mqtt: '1884',
|
||||
ServerService.webRtc: 'http://192.168.1.33:1986',
|
||||
ServerService.droneMqtt: 'wss://drone.local/stream/mqtt',
|
||||
ServerService.pathPlanning: 'http://192.168.1.34:8088',
|
||||
},
|
||||
),
|
||||
);
|
||||
EnvConfig.loadServerConfiguration(prefs);
|
||||
expect(EnvConfig.baseUrl, 'http://$host:8800');
|
||||
expect(EnvConfig.tcpIp, '192.168.1.32');
|
||||
expect(EnvConfig.tcpPort, 9002);
|
||||
expect(MqttConfig.taskMessage().host, host);
|
||||
expect(MqttConfig.mowerRealtime().port, 1884);
|
||||
expect(MqttConfig.droneOsd().port, 443);
|
||||
expect(MqttConfig.droneOsd().wsPath, '/stream/mqtt');
|
||||
expect(EnvConfig.webRtcStreamAuthority, '192.168.1.33');
|
||||
expect(
|
||||
EnvConfig.webRtcPlayAddress(
|
||||
Uri.parse('webrtc://192.168.1.33/live/test'),
|
||||
).port,
|
||||
1986,
|
||||
);
|
||||
expect(EnvConfig.address(ServerService.pathPlanning).path, '/api/path');
|
||||
expect(
|
||||
EnvConfig.address(ServerService.geoServer).host,
|
||||
ServerConfiguration.defaultHost,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('rejects malformed settings and resolves IPv6 addresses', () {
|
||||
for (final value in [
|
||||
'0',
|
||||
'65536',
|
||||
'host:0',
|
||||
'https://host:9001',
|
||||
'host:9001/api',
|
||||
]) {
|
||||
expect(
|
||||
() => ServerService.tcp.resolve(
|
||||
value,
|
||||
production: false,
|
||||
commonHost: 'localhost',
|
||||
),
|
||||
throwsFormatException,
|
||||
);
|
||||
}
|
||||
final config = ServerConfiguration(production: false, sharedHost: '::1');
|
||||
expect(config.address(ServerService.http).toString(), 'http://[::1]:59003');
|
||||
expect(config.address(ServerService.tcp).host, '::1');
|
||||
});
|
||||
|
||||
test('an existing HTTP client sends new requests to the saved endpoint', () async {
|
||||
final user = AppUserCubit();
|
||||
sl.registerSingleton(user, dispose: (cubit) => cubit.close());
|
||||
final adapter = _HttpAdapter();
|
||||
final dio = DioClient.create()..httpClientAdapter = adapter;
|
||||
final previous = EnvConfig.baseUrl;
|
||||
await dio.get('/status');
|
||||
await EnvConfig.saveServerConfiguration(
|
||||
prefs,
|
||||
ServerConfiguration(
|
||||
production: EnvConfig.isProduction,
|
||||
overrides: const {ServerService.http: 'http://192.168.1.35:8800/api'},
|
||||
),
|
||||
);
|
||||
await dio.get('/status');
|
||||
await dio.get('https://example.com/absolute');
|
||||
expect(adapter.requests.map((uri) => uri.toString()), [
|
||||
'$previous/status',
|
||||
'http://192.168.1.35:8800/api/status',
|
||||
'https://example.com/absolute',
|
||||
]);
|
||||
});
|
||||
|
||||
test(
|
||||
'MQTT applies new endpoints while an old connection is pending',
|
||||
() async {
|
||||
final clients = {
|
||||
for (final name in [
|
||||
'droneOsdClient',
|
||||
'taskMessageClient',
|
||||
'mowerRealtimeClient',
|
||||
])
|
||||
name: _MqttClient(),
|
||||
};
|
||||
for (final entry in clients.entries) {
|
||||
sl.registerSingleton<MqttClient>(entry.value, instanceName: entry.key);
|
||||
}
|
||||
final manager = MqttManager();
|
||||
final startup = manager.initialize();
|
||||
try {
|
||||
await EnvConfig.saveServerConfiguration(
|
||||
prefs,
|
||||
ServerConfiguration(
|
||||
production: EnvConfig.isProduction,
|
||||
sharedHost: '192.168.1.35',
|
||||
overrides: const {ServerService.mqtt: '1884'},
|
||||
),
|
||||
);
|
||||
await manager.reconnect();
|
||||
for (final name in ['taskMessageClient', 'mowerRealtimeClient']) {
|
||||
expect(clients[name]!.disconnects, 1);
|
||||
expect(clients[name]!.attempts.last.host, '192.168.1.35');
|
||||
expect(clients[name]!.attempts.last.port, 1884);
|
||||
}
|
||||
expect(clients['droneOsdClient']!.attempts.last.port, 8083);
|
||||
} finally {
|
||||
for (final client in clients.values) {
|
||||
client.initialConnection.complete();
|
||||
}
|
||||
await startup;
|
||||
await manager.dispose();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('signed-out user can edit and save the shared host', (
|
||||
tester,
|
||||
) async {
|
||||
final router = GoRouter(
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
builder: (context, _) => Scaffold(
|
||||
body: TextButton(
|
||||
onPressed: () => context.push('/servers'),
|
||||
child: const Text('Configure'),
|
||||
),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/servers',
|
||||
builder: (_, _) => const ServerConfigurationPage(),
|
||||
),
|
||||
],
|
||||
);
|
||||
addTearDown(router.dispose);
|
||||
await tester.pumpWidget(
|
||||
BlocProvider<AuthCubit>.value(
|
||||
value: _SignedOutAuth(),
|
||||
child: MaterialApp.router(
|
||||
routerConfig: router,
|
||||
locale: AppLocalizations.english,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
localizationsDelegates: const [RegionTestLocalizations()],
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Configure'));
|
||||
await tester.pumpAndSettle();
|
||||
final hostField = find.widgetWithText(TextField, 'Shared IP or hostname');
|
||||
await tester.enterText(hostField, 'http://bad-host');
|
||||
await tester.tap(find.text('Save'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Enter a valid IP address or hostname'), findsOneWidget);
|
||||
|
||||
// Keep both MQTT clients on their original brokers in this widget test.
|
||||
await tester.enterText(hostField, '192.168.1.35');
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextField, 'Business MQTT'),
|
||||
EnvConfig.address(ServerService.mqtt).authority,
|
||||
);
|
||||
await tester.tap(find.text('Save'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Configure'), findsOneWidget);
|
||||
expect(EnvConfig.address(ServerService.http).host, '192.168.1.35');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user