diff --git a/lib/core/config/app_config.dart b/lib/core/config/app_config.dart index a4b02ad..a75bc96 100644 --- a/lib/core/config/app_config.dart +++ b/lib/core/config/app_config.dart @@ -85,7 +85,15 @@ class AppConfig { static Future probeAndApplyBaseUrl({ Future Function(String baseUrl)? probe, String? fallbackBaseUrl, + + /// 编译期地址测试注入;未传时读取 `API_BASE_URL`。 + String? compiledOverride, }) async { + final compiled = compiledOverride ?? compiledBaseUrl; + if (compiled.isNotEmpty) { + if (_runtimeBaseUrl.isEmpty) applyRuntimeBaseUrl(compiled); + return; + } final candidates = [ if (_runtimeBaseUrl.isNotEmpty) _runtimeBaseUrl, for (final candidate in _probeCandidates) diff --git a/test/config/server_endpoint_test.dart b/test/config/server_endpoint_test.dart index 7f640f6..2348717 100644 --- a/test/config/server_endpoint_test.dart +++ b/test/config/server_endpoint_test.dart @@ -66,6 +66,59 @@ void main() { expect(AppConfig.devHostBaseUrl, 'http://192.168.31.187:8081'); }); + test('should_skip_probe_when_compiled_override_is_set', () async { + const compiled = 'http://compiled.test:8081'; + var probeCalls = 0; + + await AppConfig.probeAndApplyBaseUrl( + compiledOverride: compiled, + probe: (_) async { + probeCalls++; + return true; + }, + ); + + expect(probeCalls, 0); + expect(AppConfig.apiBaseUrl, compiled); + }); + + test('should_keep_runtime_endpoint_when_compiled_override_is_set', () async { + const runtime = 'http://runtime.test:8081'; + const compiled = 'http://compiled.test:8081'; + var probeCalls = 0; + AppConfig.applyRuntimeBaseUrl(runtime); + + await AppConfig.probeAndApplyBaseUrl( + compiledOverride: compiled, + probe: (_) async { + probeCalls++; + return true; + }, + ); + + expect(probeCalls, 0); + expect(AppConfig.apiBaseUrl, runtime); + }); + + test('should_keep_probing_when_compiled_override_is_empty', () async { + final attempts = []; + + await AppConfig.probeAndApplyBaseUrl( + compiledOverride: '', + 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_probe_all_candidates_in_parallel_before_applying_priority', () async {