from pathlib import Path

import pytest

from src.views import FRONTEND_BUILD, dashboard


@pytest.mark.anyio
async def test_dashboard_renders_outside_repository_working_directory(monkeypatch, tmp_path):
    monkeypatch.chdir(tmp_path)

    html = await dashboard()

    assert "Stackchain Dashboard" in html


@pytest.mark.anyio
async def test_dashboard_viewport_allows_mobile_zoom():
    html = await dashboard()

    assert 'name="viewport"' in html
    assert "width=device-width" in html
    assert "user-scalable=no" not in html


@pytest.mark.anyio
async def test_dashboard_bootstrap_uses_csp_compatible_static_assets():
    html = await dashboard()

    assert '<link rel="stylesheet" href="static/dashboard.css" />' in html
    assert f'<script src="{FRONTEND_BUILD.runtime_name}"></script>' in html
    assert "<style>" not in html
    assert "<script>" not in html


@pytest.mark.anyio
async def test_global_search_preview_is_a_phone_safe_accessible_dialog():
    html = await dashboard()
    css = (Path(__file__).resolve().parents[1] / "frontend" / "dashboard.css").read_text()

    assert 'id="search-preview" role="dialog" aria-modal="true"' in html
    assert 'aria-labelledby="search-preview-title"' in html
    assert 'id="claim-search-result"' in html
    assert 'id="share-search-result"' in html
    assert 'id="open-search-result-gitea"' in html
    assert "static/search-preview.js" in FRONTEND_BUILD.page_sources
    assert ".search-preview-panel" in css
    assert "height:100dvh" in css
    assert "env(safe-area-inset-bottom)" in css
    assert ".search-preview-actions button, .search-preview-actions a { min-height:44px;" in css
