from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def _template() -> str:
    return (ROOT / "templates/admin/observability_overview.html").read_text(encoding="utf-8")


def _any_css_block_has(template: str, selector: str, expected: str) -> bool:
    marker = f"{selector} {{"
    assert marker in template
    return any(expected in block.split("}", 1)[0] for block in template.split(marker)[1:])


def test_observability_overview_first_viewport_is_compact_workbench():
    template = _template()
    hero = template.split('<section class="obs-hero"', 1)[1].split(
        '<section class="obs-grid">',
        1,
    )[0]

    assert 'data-density-guardrail="compact-observability-workbench"' in hero
    assert 'aria-label="AI 觀測模式"' in hero
    assert "AI 觀測" in hero
    assert "風險優先" in hero
    assert "下一步" in hero
    assert 'aria-label="AI 自動化狀態"' in hero
    assert 'data-benchmark-guardrail="golden-signals"' in hero


def test_observability_overview_hides_explanatory_copy_layers():
    template = _template()
    hidden_selectors = [
        ".obs-lede",
        ".obs-signal-note",
        ".obs-host-meta,\n  .obs-footer-source",
        ".obs-route-desc",
    ]

    for selector in hidden_selectors:
        assert _any_css_block_has(template, selector, "display: none")


def test_observability_overview_keeps_first_layer_to_status_numbers_and_actions():
    template = _template()
    hero = template.split('<section class="obs-hero"', 1)[1].split(
        '<section class="obs-grid">',
        1,
    )[0]

    for marker in ["即時風險", "24 小時呼叫", "成本水位", "知識命中率"]:
        assert marker in hero

    for long_copy in [
        "私有 AI 中樞的第一入口",
        "主機探測、AI 呼叫、自癒、預算與品質訊號已進總覽",
        "以正式資料回讀主機健康、錯誤率、成本與知識命中",
        "即時風險分層；詳細事件保留在各子頁",
        "持續觀察健康、成本與自癒訊號",
    ]:
        assert long_copy in template
        assert long_copy in hero

    assert "display: none" in template.split(".obs-signal-note {", 1)[1].split("}", 1)[0]
    assert "display: none" in template.split(".obs-lede {", 1)[1].split("}", 1)[0]
