Merge remote-tracking branch 'origin/main' into bb/gui
# Conflicts: # apps/dashboard/src/i18n/af.ts # apps/dashboard/src/i18n/de.ts # apps/dashboard/src/i18n/es.ts # apps/dashboard/src/i18n/fr.ts # apps/dashboard/src/i18n/ga.ts # apps/dashboard/src/i18n/hu.ts # apps/dashboard/src/i18n/it.ts # apps/dashboard/src/i18n/ja.ts # apps/dashboard/src/i18n/ko.ts # apps/dashboard/src/i18n/pt.ts # apps/dashboard/src/i18n/ru.ts # apps/dashboard/src/i18n/tr.ts # apps/dashboard/src/i18n/uk.ts # apps/dashboard/src/i18n/zh-hant.ts # gateway/config.py # hermes_cli/main.py # plugins/strike-freedom-cockpit/README.md # tui_gateway/server.py
This commit is contained in:
@@ -13,7 +13,7 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from hermes_cli.main import _web_ui_build_needed, _build_web_ui
|
||||
from hermes_cli.main import _web_ui_build_needed, _build_web_ui, _run_npm_install_deterministic
|
||||
|
||||
|
||||
def _touch(path: Path, offset: float = 0.0) -> None:
|
||||
@@ -119,3 +119,92 @@ class TestBuildWebUISkipsWhenFresh:
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 2 # npm install + npm run build
|
||||
|
||||
def test_npm_install_uses_utf8_replace_output_decoding(self, tmp_path):
|
||||
web_dir, _ = _make_web_dir(tmp_path)
|
||||
(web_dir / "package-lock.json").write_text("{}", encoding="utf-8")
|
||||
|
||||
mock_cp = __import__("subprocess").CompletedProcess([], 0, stdout="", stderr="")
|
||||
with patch("hermes_cli.main.subprocess.run", return_value=mock_cp) as mock_run:
|
||||
result = _run_npm_install_deterministic("/usr/bin/npm", web_dir)
|
||||
|
||||
assert result.returncode == 0
|
||||
_, kwargs = mock_run.call_args
|
||||
assert kwargs["text"] is True
|
||||
assert kwargs["encoding"] == "utf-8"
|
||||
assert kwargs["errors"] == "replace"
|
||||
|
||||
def test_web_build_uses_utf8_replace_output_decoding(self, tmp_path):
|
||||
web_dir, _ = _make_web_dir(tmp_path)
|
||||
|
||||
mock_cp = __import__("subprocess").CompletedProcess([], 0, stdout="", stderr="")
|
||||
with patch("hermes_cli.main.shutil.which", return_value="/usr/bin/npm"), \
|
||||
patch("hermes_cli.main.subprocess.run", side_effect=[mock_cp, mock_cp]) as mock_run:
|
||||
result = _build_web_ui(web_dir)
|
||||
|
||||
assert result is True
|
||||
_, build_kwargs = mock_run.call_args_list[1]
|
||||
assert build_kwargs["text"] is True
|
||||
assert build_kwargs["encoding"] == "utf-8"
|
||||
assert build_kwargs["errors"] == "replace"
|
||||
|
||||
|
||||
class TestBuildWebUIRetryAndStaleFallback:
|
||||
"""Coverage for the retry + stale-dist fallback added in #23824 / issue #23817."""
|
||||
|
||||
def test_retries_build_once_on_failure(self, tmp_path):
|
||||
web_dir, _ = _make_web_dir(tmp_path)
|
||||
Subprocess = __import__("subprocess")
|
||||
# install: success; build attempt 1: fail; build attempt 2: success
|
||||
install_ok = Subprocess.CompletedProcess([], 0, stdout="", stderr="")
|
||||
build_fail = Subprocess.CompletedProcess([], 1, stdout="", stderr="EPERM")
|
||||
build_ok = Subprocess.CompletedProcess([], 0, stdout="", stderr="")
|
||||
with patch("hermes_cli.main.shutil.which", return_value="/usr/bin/npm"), \
|
||||
patch("hermes_cli.main._time.sleep") as mock_sleep, \
|
||||
patch("hermes_cli.main.subprocess.run",
|
||||
side_effect=[install_ok, build_fail, build_ok]) as mock_run:
|
||||
result = _build_web_ui(web_dir)
|
||||
|
||||
assert result is True
|
||||
assert mock_run.call_count == 3 # install + build + retry
|
||||
mock_sleep.assert_called_once_with(3)
|
||||
|
||||
def test_falls_back_to_stale_dist_when_retry_also_fails(self, tmp_path, capsys):
|
||||
web_dir, dist_dir = _make_web_dir(tmp_path)
|
||||
# Stale dist exists but is older than source
|
||||
_touch(dist_dir / "index.html", offset=-100)
|
||||
_touch(web_dir / "src" / "App.tsx") # newer source -> build_needed=True
|
||||
|
||||
Subprocess = __import__("subprocess")
|
||||
install_ok = Subprocess.CompletedProcess([], 0, stdout="", stderr="")
|
||||
build_fail = Subprocess.CompletedProcess([], 1, stdout="", stderr="vite ENOMEM")
|
||||
with patch("hermes_cli.main.shutil.which", return_value="/usr/bin/npm"), \
|
||||
patch("hermes_cli.main._time.sleep"), \
|
||||
patch("hermes_cli.main.subprocess.run",
|
||||
side_effect=[install_ok, build_fail, build_fail]):
|
||||
result = _build_web_ui(web_dir, fatal=True)
|
||||
|
||||
# MUST return True (serve stale) — issue #23817 — even with fatal=True,
|
||||
# because cmd_dashboard passes fatal=True and is the primary caller.
|
||||
assert result is True
|
||||
out = capsys.readouterr().out
|
||||
assert "serving stale dist as fallback" in out
|
||||
assert "vite ENOMEM" in out # stderr surfaced to user
|
||||
|
||||
def test_hard_fails_when_no_dist_to_fall_back_to(self, tmp_path, capsys):
|
||||
web_dir, _ = _make_web_dir(tmp_path)
|
||||
|
||||
Subprocess = __import__("subprocess")
|
||||
install_ok = Subprocess.CompletedProcess([], 0, stdout="", stderr="")
|
||||
build_fail = Subprocess.CompletedProcess([], 1, stdout="", stderr="vite ENOMEM")
|
||||
with patch("hermes_cli.main.shutil.which", return_value="/usr/bin/npm"), \
|
||||
patch("hermes_cli.main._time.sleep"), \
|
||||
patch("hermes_cli.main.subprocess.run",
|
||||
side_effect=[install_ok, build_fail, build_fail]):
|
||||
result = _build_web_ui(web_dir, fatal=True)
|
||||
|
||||
assert result is False
|
||||
out = capsys.readouterr().out
|
||||
assert "Web UI build failed" in out
|
||||
assert "vite ENOMEM" in out
|
||||
assert "Run manually" in out
|
||||
|
||||
Reference in New Issue
Block a user