Improve remote session management and Windows support
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
@@ -33,12 +34,16 @@ class TmuxIntegrationTests(unittest.TestCase):
|
||||
encoding="utf-8",
|
||||
)
|
||||
self.fake_herdr.chmod(0o755)
|
||||
self.fake_ssh = root / "ssh"
|
||||
self.fake_ssh.write_text("#!/bin/sh\nexec sleep 30\n", encoding="utf-8")
|
||||
self.fake_ssh.chmod(0o755)
|
||||
self.socket_name = f"herdr-hub-test-{os.getpid()}-{uuid.uuid4().hex[:8]}"
|
||||
self.socket_path = Path(
|
||||
os.environ.get("TMUX_TMPDIR", f"/tmp/tmux-{os.getuid()}")
|
||||
) / self.socket_name
|
||||
self.hub = TmuxHub(
|
||||
tmux_bin=shutil.which("tmux") or "tmux",
|
||||
ssh_bin=str(self.fake_ssh),
|
||||
socket_name=self.socket_name,
|
||||
session_name="hub",
|
||||
config_file="/dev/null",
|
||||
@@ -86,6 +91,9 @@ class TmuxIntegrationTests(unittest.TestCase):
|
||||
status_right = self.hub.run(
|
||||
["show-options", "-gv", "status-right"]
|
||||
).stdout.strip()
|
||||
expanded_status_left = self.hub.run(
|
||||
["display-message", "-p", "#{E:status-left}"]
|
||||
).stdout.strip()
|
||||
dead_format = self.hub.run(
|
||||
["show-options", "-wgv", "remain-on-exit-format"]
|
||||
).stdout.strip()
|
||||
@@ -99,14 +107,21 @@ class TmuxIntegrationTests(unittest.TestCase):
|
||||
["list-keys", "-T", "root", "MouseDown1StatusLeft"]
|
||||
).stdout.strip()
|
||||
close_binding = self.hub.run(
|
||||
["list-keys", "-T", "prefix", "&"]
|
||||
["list-keys", "-T", "prefix", "q"]
|
||||
).stdout.strip()
|
||||
legacy_close_binding = self.hub.run(
|
||||
["list-keys", "-T", "prefix", "&"], check=False
|
||||
)
|
||||
uppercase_close_binding = self.hub.run(
|
||||
["list-keys", "-T", "prefix", "Q"], check=False
|
||||
)
|
||||
self.assertEqual(prefix, "C-a")
|
||||
self.assertEqual(status, "on")
|
||||
self.assertEqual(position, "top")
|
||||
self.assertEqual(mouse, "on")
|
||||
self.assertEqual(clipboard, "on")
|
||||
self.assertIn("连接已断开", status_right)
|
||||
self.assertTrue(expanded_status_left.startswith("#[bold,fg=colour81]"))
|
||||
self.assertIn("C-a R", status_right)
|
||||
self.assertIn("连接已断开", dead_format)
|
||||
self.assertIn("Ctrl+A R", dead_format)
|
||||
@@ -116,8 +131,11 @@ class TmuxIntegrationTests(unittest.TestCase):
|
||||
self.assertIn("client_width", current_window_format)
|
||||
self.assertIn(WINDOW_SHORT_LABEL_OPTION, current_window_format)
|
||||
self.assertIn("display-popup", status_left_binding)
|
||||
self.assertIn("-EE", status_left_binding)
|
||||
self.assertIn("confirm-before", close_binding)
|
||||
self.assertIn(" close --window ", close_binding)
|
||||
self.assertNotEqual(legacy_close_binding.returncode, 0)
|
||||
self.assertNotEqual(uppercase_close_binding.returncode, 0)
|
||||
|
||||
local_short_label = self.hub.run(
|
||||
[
|
||||
@@ -201,6 +219,87 @@ class TmuxIntegrationTests(unittest.TestCase):
|
||||
self.hub.reconnect(window.window_id)
|
||||
self.assertIsNotNone(self.hub.window_for_key("local:dead"))
|
||||
|
||||
def test_alias_is_applied_immediately_and_can_be_reset(self) -> None:
|
||||
self.hub.aliases["remote:orangepi:work"] = "客厅开发"
|
||||
self.hub.ensure_server()
|
||||
window = self.hub.create_window(
|
||||
Endpoint.remote("orangepi", "work"),
|
||||
str(self.fake_herdr),
|
||||
)
|
||||
self.hub.remove_bootstrap()
|
||||
|
||||
current = self.hub.window_for_key(window.key)
|
||||
assert current is not None
|
||||
self.assertEqual(current.name, "客厅开发")
|
||||
short_label = self.hub.run(
|
||||
[
|
||||
"show-options",
|
||||
"-wv",
|
||||
"-t",
|
||||
window.window_id,
|
||||
WINDOW_SHORT_LABEL_OPTION,
|
||||
]
|
||||
).stdout.strip()
|
||||
self.assertEqual(short_label, "客厅开发")
|
||||
|
||||
renamed = self.hub.apply_alias(window.key, "临时名称")
|
||||
assert renamed is not None
|
||||
self.assertEqual(renamed.name, "临时名称")
|
||||
restored = self.hub.apply_alias(window.key, "")
|
||||
assert restored is not None
|
||||
self.assertEqual(restored.name, "R orangepi/work")
|
||||
|
||||
def test_failed_native_window_can_migrate_to_windows_ssh_transport(self) -> None:
|
||||
self.hub.ensure_server()
|
||||
native = self.hub.create_window(
|
||||
Endpoint.remote("windows"),
|
||||
str(self.fake_herdr),
|
||||
)
|
||||
self.hub.remove_bootstrap()
|
||||
|
||||
migrated = self.hub.activate_endpoint(
|
||||
Endpoint.remote("windows", remote_transport="ssh"),
|
||||
str(self.fake_herdr),
|
||||
)
|
||||
self.assertEqual(migrated.window_id, native.window_id)
|
||||
assert migrated.endpoint is not None
|
||||
self.assertEqual(migrated.endpoint.remote_transport, "ssh")
|
||||
command = self.hub.run(
|
||||
[
|
||||
"show-options",
|
||||
"-wv",
|
||||
"-t",
|
||||
native.window_id,
|
||||
"@herdr_hub_command",
|
||||
]
|
||||
).stdout.strip()
|
||||
self.assertIn(str(self.fake_ssh), command)
|
||||
self.assertIn("-tt", command)
|
||||
|
||||
def test_concurrent_open_of_the_same_endpoint_creates_one_window(self) -> None:
|
||||
self.hub.ensure_server()
|
||||
second_hub = TmuxHub(
|
||||
tmux_bin=shutil.which("tmux") or "tmux",
|
||||
ssh_bin=str(self.fake_ssh),
|
||||
socket_name=self.socket_name,
|
||||
session_name="hub",
|
||||
config_file="/dev/null",
|
||||
script_path="/dev/null",
|
||||
)
|
||||
endpoint = Endpoint.local("concurrent")
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
windows = list(
|
||||
executor.map(
|
||||
lambda hub: hub.activate_endpoint(endpoint, str(self.fake_herdr)),
|
||||
[self.hub, second_hub],
|
||||
)
|
||||
)
|
||||
self.assertEqual(windows[0].window_id, windows[1].window_id)
|
||||
self.assertEqual(
|
||||
[window.key for window in self.hub.managed_windows()],
|
||||
["local:concurrent"],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user