fix: auto-discover local gateway url

This commit is contained in:
2026-06-30 09:57:17 +08:00
parent 99194a8f7d
commit fc88744c50
4 changed files with 115 additions and 13 deletions
+42
View File
@@ -1,9 +1,13 @@
from __future__ import annotations
import importlib.util
import json
import os
import sys
from pathlib import Path
import tempfile
import unittest
from unittest import mock
def load_module():
@@ -56,6 +60,10 @@ class CodexRetryGatewayTUITests(unittest.TestCase):
def test_gateway_url_helpers_and_request_age(self) -> None:
mod = load_module()
self.assertEqual(
mod.gateway_admin_url("http://127.0.0.1:4610"),
"http://127.0.0.1:4610/__codex_retry_gateway",
)
self.assertEqual(
mod.normalize_gateway_url("http://127.0.0.1:4610/__codex_retry_gateway/api/status"),
"http://127.0.0.1:4610/__codex_retry_gateway",
@@ -66,6 +74,40 @@ class CodexRetryGatewayTUITests(unittest.TestCase):
)
self.assertIn("updated", mod.render_request_detail({"seq": 1, "request_id": "r", "method": "POST", "path": "/responses", "status_code": 200, "upstream_status_code": 200, "upstream_attempt_count": 1, "first_response_delay_ms": 10, "duration_ms": 20, "request_body_bytes": 3, "response_bytes_received": 4, "stream_chunk_count": 1, "started_at": "2026-06-30T00:00:00Z", "finished_at": "2026-06-30T00:00:01Z", "usage_last_updated_at": "2026-06-30T00:00:01Z"}))
def test_default_api_url_discovers_gateway_state(self) -> None:
mod = load_module()
with tempfile.TemporaryDirectory() as tmpdir:
state_path = Path(tmpdir) / "state.json"
config_path = Path(tmpdir) / "config.json"
api_url_path = Path(tmpdir) / "api-url"
state_path.write_text(json.dumps({"gateway_base_url": "http://100.115.235.115:4610"}), encoding="utf-8")
config_path.write_text("{}", encoding="utf-8")
mod.DEFAULT_GATEWAY_STATE_FILE = str(state_path)
mod.DEFAULT_GATEWAY_JSON_CONFIG_FILE = str(config_path)
with mock.patch.dict(os.environ, {"CODEX_RETRY_GATEWAY_TUI_API_URL_FILE": str(api_url_path)}, clear=False):
self.assertEqual(
mod.default_api_url(),
"http://100.115.235.115:4610/__codex_retry_gateway",
)
def test_default_api_url_discovers_gateway_config_when_state_missing(self) -> None:
mod = load_module()
with tempfile.TemporaryDirectory() as tmpdir:
state_path = Path(tmpdir) / "missing-state.json"
config_path = Path(tmpdir) / "config.json"
api_url_path = Path(tmpdir) / "api-url"
config_path.write_text(
json.dumps({"listen_host": "0.0.0.0", "listen_port": 4610}),
encoding="utf-8",
)
mod.DEFAULT_GATEWAY_STATE_FILE = str(state_path)
mod.DEFAULT_GATEWAY_JSON_CONFIG_FILE = str(config_path)
with mock.patch.dict(os.environ, {"CODEX_RETRY_GATEWAY_TUI_API_URL_FILE": str(api_url_path)}, clear=False):
self.assertEqual(
mod.default_api_url(),
"http://127.0.0.1:4610/__codex_retry_gateway",
)
if __name__ == "__main__":
unittest.main()