"""Tests for embedded browser UI state."""

from server.utils.browser_ui_state import (
    empty_browser_state,
    parse_browser_state,
    record_visit,
)


def test_empty_browser_state():
    assert parse_browser_state(None) == empty_browser_state()


def test_parse_filters_unsafe_urls():
    raw = {
        "url_history": ["https://ok.example", "javascript:alert(1)", "ftp://x"],
        "tabs": [
            {"id": "t1", "url": "https://a.com", "stack": ["https://a.com"], "stack_index": 0},
            {"id": "t2", "url": "data:text/html,x"},
        ],
    }
    state = parse_browser_state(raw)
    assert state["url_history"] == ["https://ok.example"]
    assert len(state["tabs"]) == 1
    assert state["tabs"][0]["id"] == "t1"


def test_record_visit_dedupes_history():
    state = empty_browser_state()
    state = record_visit(state, "https://a.example", "A")
    state = record_visit(state, "https://b.example", "B")
    assert state["url_history"][0] == "https://b.example"
    assert len(state["visits"]) == 2
