"""Browser-Verhaltens-Endpoint: URL-Ableitung + Request-Defaults (Phase 2).

Reine Logik ohne DB/HTTP — die Live-Matrix + Persistenz werden im macmini-E2E
verifiziert. Sichert v.a.: scanbare Homepage-URL korrekt aus dem Snapshot
abgeleitet und das Request-Modell ist auf Python 3.9 baubar (Optional statt
`| None` — siehe Pydantic-v2-Falle mit `from __future__ import annotations`)."""

from compliance.api.snapshot_check_routes import (
    _snapshot_scan_url,
    BrowserBehaviorRunRequest,
)


def test_url_from_doc_entry_origin():
    snap = {"site_domain": "bmw.de",
            "doc_entries": [{"url": "https://www.bmw.de/de/impressum.html"}]}
    assert _snapshot_scan_url(snap) == "https://www.bmw.de"


def test_url_falls_back_to_site_domain():
    assert _snapshot_scan_url({"site_domain": "example.com",
                               "doc_entries": []}) == "https://example.com"


def test_url_skips_blank_entries_then_uses_domain():
    snap = {"site_domain": "shop.de", "doc_entries": [{"url": ""}, {"url": "  "}]}
    assert _snapshot_scan_url(snap) == "https://shop.de"


def test_url_empty_when_unknown_and_no_entries():
    assert _snapshot_scan_url({"site_domain": "unknown", "doc_entries": []}) == ""
    assert _snapshot_scan_url({"site_domain": "", "doc_entries": []}) == ""


def test_request_model_defaults_build_on_py39():
    m = BrowserBehaviorRunRequest()
    assert m.browser_profiles is None
    assert m.timeout_per_phase == 10
    m2 = BrowserBehaviorRunRequest(browser_profiles=["brave-default-de"],
                                   timeout_per_phase=20)
    assert m2.browser_profiles == ["brave-default-de"]
    assert m2.timeout_per_phase == 20
