150 lines
4.4 KiB
Dart
150 lines
4.4 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
|
|
|
import 'package:fruitshorts/core/config/app_config.dart';
|
|
import 'package:fruitshorts/core/config/server_endpoint.dart';
|
|
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
AppConfig.clearRuntimeBaseUrl();
|
|
});
|
|
|
|
tearDown(AppConfig.clearRuntimeBaseUrl);
|
|
|
|
test('should_normalize_http_urls_and_reject_invalid_values', () {
|
|
expect(
|
|
ServerEndpoint.normalize(' https://192.168.1.9:8081/// '),
|
|
'https://192.168.1.9:8081',
|
|
);
|
|
expect(
|
|
ServerEndpoint.normalize('http://example.test:8081/path/'),
|
|
'http://example.test:8081/path',
|
|
);
|
|
expect(ServerEndpoint.normalize('ftp://example.test'), isNull);
|
|
expect(ServerEndpoint.normalize('example.test:8081'), isNull);
|
|
expect(ServerEndpoint.normalize('http://'), isNull);
|
|
});
|
|
|
|
test('should_prioritize_runtime_over_dart_define_over_default', () {
|
|
expect(
|
|
ServerEndpoint.resolve(
|
|
stored: ' http://runtime.test:8081/ ',
|
|
compiled: 'http://define.test:8081',
|
|
fallback: 'http://default.test:8081',
|
|
),
|
|
'http://runtime.test:8081',
|
|
);
|
|
expect(
|
|
ServerEndpoint.resolve(
|
|
stored: 'ftp://invalid',
|
|
compiled: 'http://define.test:8081/',
|
|
fallback: 'http://default.test:8081',
|
|
),
|
|
'http://define.test:8081',
|
|
);
|
|
expect(
|
|
ServerEndpoint.resolve(
|
|
stored: null,
|
|
compiled: '',
|
|
fallback: 'http://default.test:8081/',
|
|
),
|
|
'http://default.test:8081',
|
|
);
|
|
});
|
|
|
|
test('should_use_the_configured_lan_address_for_device_default', () {
|
|
expect(AppConfig.devHostBaseUrl, 'http://192.168.31.187:8081');
|
|
});
|
|
|
|
test('should_keep_loopback_priority_when_probe_succeeds', () async {
|
|
final attempts = <String>[];
|
|
|
|
await AppConfig.probeAndApplyBaseUrl(
|
|
fallbackBaseUrl: AppConfig.devHostBaseUrl,
|
|
probe: (baseUrl) async {
|
|
attempts.add(baseUrl);
|
|
return baseUrl == AppConfig.lanBaseUrl;
|
|
},
|
|
);
|
|
|
|
expect(attempts, [AppConfig.lanBaseUrl]);
|
|
expect(AppConfig.apiBaseUrl, AppConfig.lanBaseUrl);
|
|
});
|
|
|
|
test('should_probe_loopback_dev_host_then_public_in_order', () async {
|
|
final attempts = <String>[];
|
|
|
|
await AppConfig.probeAndApplyBaseUrl(
|
|
fallbackBaseUrl: AppConfig.devHostBaseUrl,
|
|
probe: (baseUrl) async {
|
|
attempts.add(baseUrl);
|
|
return baseUrl == AppConfig.publicBaseUrl;
|
|
},
|
|
);
|
|
|
|
expect(attempts, [
|
|
AppConfig.lanBaseUrl,
|
|
AppConfig.devHostBaseUrl,
|
|
AppConfig.publicBaseUrl,
|
|
]);
|
|
expect(AppConfig.apiBaseUrl, AppConfig.publicBaseUrl);
|
|
});
|
|
|
|
test('should_use_platform_default_when_all_probes_fail', () async {
|
|
final attempts = <String>[];
|
|
|
|
await AppConfig.probeAndApplyBaseUrl(
|
|
fallbackBaseUrl: AppConfig.devHostBaseUrl,
|
|
probe: (baseUrl) async {
|
|
attempts.add(baseUrl);
|
|
return false;
|
|
},
|
|
);
|
|
|
|
expect(attempts, [
|
|
AppConfig.lanBaseUrl,
|
|
AppConfig.devHostBaseUrl,
|
|
AppConfig.publicBaseUrl,
|
|
]);
|
|
expect(AppConfig.apiBaseUrl, AppConfig.devHostBaseUrl);
|
|
});
|
|
|
|
test('should_apply_stored_url_only_when_debug_is_enabled', () async {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.withData({
|
|
ServerEndpoint.preferenceKey: ' http://192.168.31.187:8081/ ',
|
|
});
|
|
final original = AppConfig.apiBaseUrl;
|
|
|
|
await ServerEndpoint.load(debug: false);
|
|
expect(AppConfig.apiBaseUrl, original);
|
|
|
|
await ServerEndpoint.load(debug: true);
|
|
expect(AppConfig.apiBaseUrl, 'http://192.168.31.187:8081');
|
|
});
|
|
|
|
test('should_save_normalized_url_and_clear_override', () async {
|
|
expect(await ServerEndpoint.save('ftp://invalid'), isFalse);
|
|
expect(
|
|
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
|
isNull,
|
|
);
|
|
|
|
expect(await ServerEndpoint.save(' http://192.168.31.187:8081/ '), isTrue);
|
|
expect(
|
|
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
|
'http://192.168.31.187:8081',
|
|
);
|
|
|
|
await ServerEndpoint.clear();
|
|
expect(
|
|
await SharedPreferencesAsync().getString(ServerEndpoint.preferenceKey),
|
|
isNull,
|
|
);
|
|
});
|
|
}
|