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 'href="/webcrumbs"' 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")
    service_source = (ROOT / "services/external_tool_payload_service.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 "build_external_tool_payload" in route_source
    assert "def _has_sensitive_webcrumbs_access()" in route_source
    assert "hmac.compare_digest(provided, internal_key)" in route_source
    assert "include_host_data=_has_sensitive_webcrumbs_access()" in route_source
    assert "def build_external_tool_payload(kind, include_host_data: bool = True)" in service_source
    assert "include_host_data: bool = True" in service_source
    assert "def build_webcrumbs_auth_required_seed_data()" in service_source
    assert "auth_required" in service_source
    assert 'parsed.path.rstrip("/") == bridge_path.rstrip("/")' in service_source
    assert "def _is_blocked_external_url(configured_url)" in service_source
    assert "external-tool-checks" in template
    assert "external-tool-diagnostics" in template
    assert "external-tool-action-grid" in template
    assert "def webcrumbs_status()" in route_source
    assert "@system_public_bp.route('/api/webcrumbs/marketplace-host-data')" in route_source
    assert "def webcrumbs_marketplace_host_data_api()" in route_source
    assert "if not _has_sensitive_webcrumbs_access()" in route_source
    assert "'error': 'auth_required'" in route_source
    assert "build_webcrumbs_seed_data(limit=limit)" in route_source
    assert "'allowed_match_contract': 'exact/total_price/price_alert_exact'" in route_source
    assert "def webcrumbs_asset_proxy(asset_path)" in route_source
    assert "WEBCRUMBS_ASSET_ALLOWED_PREFIXES" in route_source
    assert "Webcrumbs 共用 UI Runtime" in service_source
    assert "def build_webcrumbs_seed_data(limit: int = 5)" in service_source
    assert "build_webcrumbs_marketplace_host_data" in service_source
    assert "plugin_previews" in service_source
    assert "finance.market-ticker-strip/0.1.0" in service_source
    assert "finance.ai-candidate-card/0.1.0" in service_source
    assert "plugin_seed_data" in service_source
    assert "diagnostic_unavailable" in service_source
    assert "source_unavailable" in service_source
    assert "StockPlatformSharedUI.allowedPluginUris" in template
    assert "StockPlatformSharedUI.marketSnapshot" in template
    assert "StockPlatformSharedUI.aiCandidate" in template
    assert "stock-platform-plugin" in template
    assert "external-tool-webcrumbs-grid" in css
    assert "尚未接入 proxy" in service_source
    assert "已由入口攔截" in service_source
    assert "AwoooI" not in service_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
