mirror of
https://github.com/hansjone/netx.git
synced 2026-10-09 05:30:46 +08:00
fix(webcrt): map backspace and arrows for network device CLIs
Huawei/ZTE/Cisco Telnet often rejects xterm DEL and CSI arrows; rewrite them to BS and emacs Ctrl-B/F/P/N so editing and history work. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
1f2e028337
commit
8e3bed562f
2 changed files with 91 additions and 3 deletions
|
|
@ -14,7 +14,7 @@ from netx_api import webcrt_service as svc
|
|||
class _FakeConn:
|
||||
def __init__(self) -> None:
|
||||
self.written: list[str] = []
|
||||
self.remote_conn = MagicMock()
|
||||
self.remote_conn = MagicMock(spec=["recv_ready", "recv", "exit_status_ready", "resize_pty"])
|
||||
self.remote_conn.recv_ready.return_value = False
|
||||
self.remote_conn.exit_status_ready.return_value = False
|
||||
self.remote_conn.resize_pty = MagicMock()
|
||||
|
|
@ -49,15 +49,27 @@ class WebcrtServiceTests(unittest.TestCase):
|
|||
protocol="ssh",
|
||||
cols=80,
|
||||
rows=24,
|
||||
cli_keymap=True,
|
||||
conn=conn, # type: ignore[arg-type]
|
||||
)
|
||||
sess.write_stdin("show ver\n")
|
||||
self.assertEqual(conn.written, ["show ver\n"])
|
||||
# xterm DEL / arrows -> network CLI controls
|
||||
sess.write_stdin("\x7f")
|
||||
self.assertEqual(conn.written[-1], "\x08")
|
||||
sess.write_stdin("\x1b[D\x1b[C\x1b[A\x1b[B")
|
||||
self.assertEqual(conn.written[-1], "\x02\x06\x10\x0e")
|
||||
sess.resize(120, 40)
|
||||
conn.remote_conn.resize_pty.assert_called_with(width=120, height=40)
|
||||
sess.close("test")
|
||||
self.assertTrue(sess.closed)
|
||||
|
||||
def test_map_network_cli_keys_helpers(self) -> None:
|
||||
self.assertEqual(svc.map_network_cli_keys("\x7fab"), "\x08ab")
|
||||
self.assertEqual(svc.map_network_cli_keys("\x1b[D"), "\x02")
|
||||
self.assertTrue(svc.uses_network_cli_keymap("huawei", "Huawei"))
|
||||
self.assertFalse(svc.uses_network_cli_keymap("linux", "bastion"))
|
||||
|
||||
@patch.object(svc, "_audit")
|
||||
@patch.object(svc, "open_netmiko_connection")
|
||||
@patch("netx_api.cli_resolve.resolve_cli_target")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue