"""Tests for navigate.* commands.""" import pytest from browser_cli.client import send_command def test_nav_open_and_close(browser): """Open a tab, verify it appears in the list, then close it.""" result = browser("navigate.open", {"url": "https://example.com", "background": True}) assert "id" in result new_id = result["id"] tabs = browser("tabs.list") ids = [t["id"] for t in tabs] assert new_id in ids # Clean up browser("tabs.close", {"tabId": new_id}) tabs_after = browser("tabs.list") assert new_id not in [t["id"] for t in tabs_after] def test_nav_focus_by_pattern(browser): # Open a known URL in background first result = browser("navigate.open", {"url": "https://example.com", "background": True}) tab_id = result["id"] focus_result = browser("navigate.focus", {"pattern": "example.com"}) assert focus_result is not None assert "example.com" in (focus_result.get("url") or "") browser("tabs.close", {"tabId": tab_id}) def test_nav_focus_by_tab_id(browser): result = browser("navigate.open", {"url": "https://example.com", "background": True}) tab_id = result["id"] focus_result = browser("navigate.focus", {"pattern": str(tab_id)}) assert focus_result is not None assert focus_result.get("id") == tab_id browser("tabs.close", {"tabId": tab_id}) def test_nav_reload(browser): result = browser("navigate.open", {"url": "https://example.com", "background": True}) tab_id = result["id"] try: reload_result = browser("navigate.reload", {"tabId": tab_id}) assert reload_result is None or isinstance(reload_result, dict) finally: browser("tabs.close", {"tabId": tab_id}) def test_nav_hard_reload(browser): result = browser("navigate.open", {"url": "https://example.com", "background": True}) tab_id = result["id"] try: result = browser("navigate.hard_reload", {"tabId": tab_id}) assert result is None or isinstance(result, dict) finally: try: browser("tabs.close", {"tabId": tab_id}) except Exception: pass # tab ID may change after hard reload def test_nav_open_in_background(browser): """Tab opened with background=True should not be the active tab.""" active_before = next( t for t in browser("tabs.list") if t.get("active") ) result = browser("navigate.open", {"url": "https://example.com", "background": True}) new_id = result["id"] try: tabs = browser("tabs.list") new_tab = next(t for t in tabs if t["id"] == new_id) assert not new_tab.get("active"), "background tab should not be active" finally: browser("tabs.close", {"tabId": new_id}) def test_nav_to_updates_existing_tab(browser): result = browser("navigate.open", {"url": "https://example.com", "background": True}) tab_id = result["id"] try: before_ids = {t["id"] for t in browser("tabs.list")} updated = browser("navigate.to", {"tabId": tab_id, "url": "https://example.org"}) assert updated["id"] == tab_id tabs = browser("tabs.list") after_ids = {t["id"] for t in tabs} assert after_ids == before_ids tab = next(t for t in tabs if t["id"] == tab_id) assert "example.org" in (tab.get("url") or "") finally: try: browser("tabs.close", {"tabId": tab_id}) except Exception: pass