fix that alias can only exist once and when dublicate give error
This commit is contained in:
+32
-1
@@ -21,7 +21,13 @@ from browser_cli.commands.dom import dom_group
|
|||||||
from browser_cli.commands.extract import extract_group
|
from browser_cli.commands.extract import extract_group
|
||||||
from browser_cli.commands.session import session_group
|
from browser_cli.commands.session import session_group
|
||||||
from browser_cli.commands.search import search_group
|
from browser_cli.commands.search import search_group
|
||||||
from browser_cli.client import send_command, BrowserNotConnected, REGISTRY_PATH, display_browser_name
|
from browser_cli.client import (
|
||||||
|
send_command,
|
||||||
|
BrowserNotConnected,
|
||||||
|
REGISTRY_PATH,
|
||||||
|
active_browser_targets,
|
||||||
|
display_browser_name,
|
||||||
|
)
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
@@ -51,6 +57,30 @@ NATIVE_HOST_DIRS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _rename_target_profile(target_browser: str | None) -> str | None:
|
||||||
|
if target_browser:
|
||||||
|
return target_browser
|
||||||
|
|
||||||
|
active = active_browser_targets()
|
||||||
|
if len(active) == 1:
|
||||||
|
return active[0].profile
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _ensure_unique_browser_alias(alias: str, target_browser: str | None) -> None:
|
||||||
|
target_profile = _rename_target_profile(target_browser)
|
||||||
|
|
||||||
|
profiles: dict[str, str] = {}
|
||||||
|
if REGISTRY_PATH.exists():
|
||||||
|
try:
|
||||||
|
profiles = json.loads(REGISTRY_PATH.read_text())
|
||||||
|
except Exception:
|
||||||
|
profiles = {}
|
||||||
|
|
||||||
|
if alias in profiles and alias != target_profile:
|
||||||
|
raise click.ClickException(f"Browser alias '{alias}' already exists")
|
||||||
|
|
||||||
|
|
||||||
def _native_host_wrapper_path() -> Path:
|
def _native_host_wrapper_path() -> Path:
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
base_dir = Path.home() / "Library" / "Application Support" / "browser-cli"
|
base_dir = Path.home() / "Library" / "Application Support" / "browser-cli"
|
||||||
@@ -184,6 +214,7 @@ main.add_command(clients_group)
|
|||||||
def cmd_clients_rename(target_browser, alias):
|
def cmd_clients_rename(target_browser, alias):
|
||||||
"""Set the profile alias used to identify this browser instance."""
|
"""Set the profile alias used to identify this browser instance."""
|
||||||
try:
|
try:
|
||||||
|
_ensure_unique_browser_alias(alias, target_browser)
|
||||||
send_command("clients.rename_profile", {"alias": alias}, profile=target_browser)
|
send_command("clients.rename_profile", {"alias": alias}, profile=target_browser)
|
||||||
except BrowserNotConnected as e:
|
except BrowserNotConnected as e:
|
||||||
console.print(f"[red]Error:[/red] {e}")
|
console.print(f"[red]Error:[/red] {e}")
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "browser-cli",
|
"name": "browser-cli",
|
||||||
"version": "0.5.6",
|
"version": "0.5.7",
|
||||||
"description": "Control your browser from the terminal via browser-cli",
|
"description": "Control your browser from the terminal via browser-cli",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"tabs",
|
"tabs",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "browser-cli"
|
name = "browser-cli"
|
||||||
version = "0.5.6"
|
version = "0.5.7"
|
||||||
description = "Control your real running browser from the terminal via a Chrome extension"
|
description = "Control your real running browser from the terminal via a Chrome extension"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
@@ -45,6 +45,27 @@ def test_clients_rename_uses_global_browser_target_when_set():
|
|||||||
send_command.assert_called_once_with("clients.rename_profile", {"alias": "work"}, profile=None)
|
send_command.assert_called_once_with("clients.rename_profile", {"alias": "work"}, profile=None)
|
||||||
assert "Restart the browser" not in result.output
|
assert "Restart the browser" not in result.output
|
||||||
|
|
||||||
|
def test_clients_rename_rejects_duplicate_alias(tmp_path):
|
||||||
|
registry_path = tmp_path / "registry.json"
|
||||||
|
registry_path.write_text('{"work": "/tmp/work.sock", "old-id": "/tmp/old-id.sock"}', encoding="utf-8")
|
||||||
|
|
||||||
|
with patch("browser_cli.cli.REGISTRY_PATH", registry_path), patch("browser_cli.cli.send_command") as send_command:
|
||||||
|
result = CliRunner().invoke(main, ["clients", "rename", "--browser", "old-id", "work"])
|
||||||
|
|
||||||
|
assert result.exit_code != 0
|
||||||
|
assert "Browser alias 'work' already exists" in result.output
|
||||||
|
send_command.assert_not_called()
|
||||||
|
|
||||||
|
def test_clients_rename_allows_same_alias_for_same_target(tmp_path):
|
||||||
|
registry_path = tmp_path / "registry.json"
|
||||||
|
registry_path.write_text('{"work": "/tmp/work.sock"}', encoding="utf-8")
|
||||||
|
|
||||||
|
with patch("browser_cli.cli.REGISTRY_PATH", registry_path), patch("browser_cli.cli.send_command") as send_command:
|
||||||
|
result = CliRunner().invoke(main, ["clients", "rename", "--browser", "work", "work"])
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
send_command.assert_called_once_with("clients.rename_profile", {"alias": "work"}, profile="work")
|
||||||
|
|
||||||
def test_install_help_lists_supported_browsers():
|
def test_install_help_lists_supported_browsers():
|
||||||
result = CliRunner().invoke(main, ["install", "--help"])
|
result = CliRunner().invoke(main, ["install", "--help"])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user