fix(webui): repair bootstrap secret launch paths

This commit is contained in:
chengyongru
2026-07-08 21:01:48 +08:00
committed by Xubin Ren
parent 88143a8bf0
commit 444f488563
2 changed files with 114 additions and 13 deletions
+78
View File
@@ -1672,6 +1672,10 @@ def test_webui_yes_creates_config_and_enables_local_websocket(
assert data["agents"]["defaults"]["workspace"] == str(workspace)
assert seen["templates"] == workspace
assert seen["gateway_kwargs"] == {"port": 18888, "open_browser_url": None}
compact_output = _strip_ansi(result.stdout).replace("\n", " ")
assert "bootstrap secret was generated" in compact_output
assert "channels.websocket.tokenIssueSecret" in compact_output
assert "rerun without --no-open" in compact_output
def test_webui_yes_refuses_missing_provider_setup(monkeypatch, tmp_path: Path) -> None:
@@ -1753,6 +1757,80 @@ def test_webui_background_starts_runtime_and_opens_browser(monkeypatch, tmp_path
assert "bootstrapSecret=" in opened_url
def test_webui_background_restarts_when_config_changes_and_gateway_is_running(
monkeypatch,
tmp_path: Path,
) -> None:
from nanobot.gateway import GatewayStartOptions, GatewayStatus, RuntimeResult
config_file = tmp_path / "config.json"
workspace = tmp_path / "workspace"
config_file.write_text("{}")
seen: dict[str, object] = {}
_patch_webui_provider_ready(monkeypatch)
monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
def _status(options: GatewayStartOptions) -> GatewayStatus:
return GatewayStatus(
running=True,
pid=123,
state_path=tmp_path / "gateway.json",
log_path=tmp_path / "gateway.log",
port=options.port,
reason="running",
)
class _FakeRuntime:
def __init__(self, **kwargs) -> None:
seen["runtime_kwargs"] = kwargs
def start_background(self, options: GatewayStartOptions) -> RuntimeResult:
seen["start_options"] = options
return RuntimeResult(False, "gateway_already_running", _status(options))
def restart(self, options: GatewayStartOptions, *, timeout_s: int) -> RuntimeResult:
seen["restart_options"] = options
seen["restart_timeout"] = timeout_s
return RuntimeResult(True, "gateway_started_background", _status(options))
monkeypatch.setattr("nanobot.gateway.GatewayRuntime", _FakeRuntime)
monkeypatch.setattr(
"nanobot.cli.commands._open_webui_browser",
lambda url: seen.__setitem__("opened_url", url),
)
result = runner.invoke(
app,
[
"webui",
"--config",
str(config_file),
"--workspace",
str(workspace),
"--background",
"--gateway-port",
"18889",
"--yes",
],
)
assert result.exit_code == 0
compact_output = _strip_ansi(result.stdout).replace("\n", " ")
assert "WebUI config changed; restarting the background gateway" in compact_output
assert "Gateway restarted in the background" in compact_output
assert "Gateway is already running" not in compact_output
options = seen["restart_options"]
assert isinstance(options, GatewayStartOptions)
assert options is seen["start_options"]
assert seen["restart_timeout"] == 20
assert options.port == 18889
assert options.config_path == str(config_file.resolve(strict=False))
assert options.workspace == str(workspace.resolve(strict=False))
opened_url = seen["opened_url"]
assert isinstance(opened_url, str)
assert opened_url.startswith("http://127.0.0.1:8765/#/?bootstrapSecret=")
def _patch_serve_runtime(monkeypatch, config: Config, seen: dict[str, object]) -> None:
pytest.importorskip("aiohttp")