from pathlib import Path


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


def test_metabase_and_grist_navigation_stays_inside_momo_pro():
    app_source = (ROOT / "app.py").read_text(encoding="utf-8")
    shell = (ROOT / "templates/components/_ewoooc_shell.html").read_text(encoding="utf-8")
    tabs = (ROOT / "templates/components/_analysis_report_tabs.html").read_text(encoding="utf-8")
    legacy_nav = (ROOT / "templates/components/_navbar.html").read_text(encoding="utf-8")
    env_example = (ROOT / ".env.example").read_text(encoding="utf-8")
    compose = (ROOT / "docker-compose.yml").read_text(encoding="utf-8")

    assert "'metabase_url': '/metabase'" in app_source
    assert "'grist_url': '/grist'" in app_source
    assert "['sales', 'daily_sales', 'monthly', 'growth', 'metabase', 'grist']" in shell
    assert "GRIST_URL=https://mo.wooo.work/grist" in env_example
    assert "APP_HOME_URL=${GRIST_APP_HOME_URL:-https://mo.wooo.work/grist}" in compose

    navigation_sources = "\n".join([app_source, shell, tabs, legacy_nav])
    combined = "\n".join([navigation_sources, env_example, compose])
    assert "grist.wooo.work" not in combined
    assert "awoooi" not in navigation_sources.lower()


def test_external_tool_bridge_pages_are_diagnostic_not_blank():
    route_source = (ROOT / "routes/system_public_routes.py").read_text(encoding="utf-8")
    template = (ROOT / "templates/external_tool_status.html").read_text(encoding="utf-8")
    css = (ROOT / "web/static/css/page-external-tools.css").read_text(encoding="utf-8")

    assert "def _external_tool_payload(kind)" in route_source
    assert "parsed.path.rstrip('/') == bridge_path.rstrip('/')" in route_source
    assert "def _is_blocked_external_url(configured_url)" in route_source
    assert "external-tool-checks" in template
    assert "external-tool-diagnostics" in template
    assert "external-tool-action-grid" in template
    assert "尚未接入 proxy" in route_source
    assert "已由入口攔截" in route_source
    assert "AwoooI" not in route_source

    assert "--momo-accent-rust" not in css
    assert "--momo-accent-honey" not in css


def test_analysis_tabs_use_internal_tool_labels_without_external_icon():
    tabs = (ROOT / "templates/components/_analysis_report_tabs.html").read_text(encoding="utf-8")

    assert "自訂圖表" in tabs
    assert "資料協作" in tabs
    assert "fa-up-right-from-square" not in tabs
