fix: handle gateway Ctrl+C shutdown cleanly

This commit is contained in:
Rylen Anil 2026-04-30 09:04:48 +00:00 committed by Teknium
parent 01d7c87ecc
commit 3858f9419e
2 changed files with 53 additions and 2 deletions

View file

@ -1,11 +1,58 @@
"""Tests for hermes_cli.gateway."""
from types import SimpleNamespace
import sys
from types import ModuleType, SimpleNamespace
from unittest.mock import patch, call
import pytest
import hermes_cli.gateway as gateway
def _install_fake_gateway_run(monkeypatch, start_gateway):
module = ModuleType("gateway.run")
module.start_gateway = start_gateway
monkeypatch.setitem(sys.modules, "gateway.run", module)
def test_run_gateway_exits_cleanly_on_keyboard_interrupt(monkeypatch, capsys):
calls = []
def fake_start_gateway(*, replace, verbosity):
calls.append((replace, verbosity))
return object()
def fake_asyncio_run(coro):
raise KeyboardInterrupt
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
monkeypatch.setattr(gateway.asyncio, "run", fake_asyncio_run)
gateway.run_gateway()
out = capsys.readouterr().out
assert calls == [(False, 0)]
assert "Press Ctrl+C to stop" in out
assert "Gateway stopped." in out
def test_run_gateway_exits_nonzero_when_start_gateway_reports_failure(monkeypatch):
calls = []
def fake_start_gateway(*, replace, verbosity):
calls.append((replace, verbosity))
return object()
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
monkeypatch.setattr(gateway.asyncio, "run", lambda coro: False)
with pytest.raises(SystemExit) as exc_info:
gateway.run_gateway(verbose=1, quiet=True, replace=True)
assert exc_info.value.code == 1
assert calls == [(True, None)]
class TestSystemdLingerStatus:
def test_reports_enabled(self, monkeypatch):
monkeypatch.setattr(gateway, "is_linux", lambda: True)