From 9ebb54ecfd29789a85e9c815c083586c04fa5317 Mon Sep 17 00:00:00 2001 From: Zhou Weidong <502333560@qq.com> Date: Tue, 20 Oct 2026 20:20:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E6=8C=87=E5=AE=9A=20API=5FBASE?= =?UTF-8?q?=5FURL=20=E6=97=B6=E8=B7=B3=E8=BF=87=E5=9C=B0=E5=9D=80=E6=8E=A2?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/config/app_config.dart | 8 ++++ test/config/server_endpoint_test.dart | 53 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) 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 {