diff --git a/tests/test_vibe_update.py b/tests/test_vibe_update.py index 86b486f..4300510 100644 --- a/tests/test_vibe_update.py +++ b/tests/test_vibe_update.py @@ -228,6 +228,10 @@ def test_derive_health_url_drops_status_query_parameters(self): "http://localhost:8080/api/health", ) + def test_derive_health_url_reports_invalid_url_without_traceback(self): + with self.assertRaisesRegex(RuntimeError, "无效的 InkDash URL"): + vibe_update.derive_health_url("http://[::1") + def test_format_health_summary(self): summary = vibe_update.format_health_summary({ "status": "ok", @@ -295,6 +299,19 @@ def test_wait_for_health_times_out_with_last_error(self): monotonic_fn=iter([0.0, 0.0]).__next__, ) + def test_wait_for_health_rejects_invalid_url_before_retry(self): + with patch("vibe_update.request_vibe") as request_mock: + with self.assertRaisesRegex(RuntimeError, "无效的 InkDash URL"): + vibe_update.wait_for_health( + "http://[::1", + request_timeout=0.1, + wait_timeout=2.0, + wait_interval=0.1, + sleep_fn=lambda _: self.fail("invalid URL should not sleep"), + ) + + request_mock.assert_not_called() + def test_request_vibe_reports_invalid_json_without_traceback(self): with patch("vibe_update.request.urlopen", return_value=FakeResponse(b"not json")): with self.assertRaisesRegex(RuntimeError, "不是有效 JSON"): diff --git a/vibe_update.py b/vibe_update.py index c5ee33b..02bd898 100644 --- a/vibe_update.py +++ b/vibe_update.py @@ -311,7 +311,10 @@ def request_vibe( def derive_health_url(url: str) -> str: - parsed = urlparse(url.rstrip("/")) + try: + parsed = urlparse(url.rstrip("/")) + except ValueError as e: + raise RuntimeError(f"无效的 InkDash URL:{e}") from e path = parsed.path.rstrip("/") for suffix in ("/api/vibe", "/api/status", "/api/health"): if path.endswith(suffix): @@ -405,13 +408,14 @@ def wait_for_health( sleep_fn=time.sleep, monotonic_fn=time.monotonic, ) -> Dict[str, Any]: + health_url = derive_health_url(url) deadline = monotonic_fn() + max(0.0, wait_timeout) interval = max(0.1, wait_interval) last_error = "" while True: try: - return request_vibe(derive_health_url(url), None, request_timeout, token) + return request_vibe(health_url, None, request_timeout, token) except RuntimeError as e: last_error = str(e)