from pathlib import Path


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


def test_daily_sales_canvas_is_primary_and_fallback_is_opt_in():
    css = (ROOT / "web/static/css/page-daily-sales.css").read_text(encoding="utf-8")
    script = (ROOT / "web/static/js/page-daily-sales.js").read_text(encoding="utf-8")

    assert ".chart-container.has-html-chart canvas" not in css
    assert ".chart-container.chart-fallback-active canvas" in css
    assert ".chart-container.chart-empty-active canvas" in css
    assert ".chart-container:not(.chart-fallback-active) .chart-fallback-list" in css
    assert "node.content && node.content.textContent" in script
    assert "chart-empty-active" in script
    render_body = script.split("function renderAllCharts()", 1)[1].split("function bootCharts()", 1)[0]
    assert "renderHtmlChartFallbacks();" not in render_body
    assert "catch(error =>" in script
    assert "renderHtmlChartFallbacks();" in script.split("catch(error =>", 1)[1]


def test_growth_analysis_canvas_is_primary_and_fallback_is_opt_in():
    css = (ROOT / "web/static/css/page-growth-bem.css").read_text(encoding="utf-8")
    script = (ROOT / "web/static/js/page-growth.js").read_text(encoding="utf-8")

    assert ".ga-chart-card__body.has-html-chart canvas" not in css
    assert ".ga-chart-card__body.chart-fallback-active canvas" in css
    assert ".ga-chart-card__body.chart-empty-active canvas" in css
    assert ".ga-chart-card__body:not(.chart-fallback-active) .ga-chart-snapshot" in css
    assert "node.content && node.content.textContent" in script
    assert "chart-empty-active" in script
    render_body = script.split("function renderCharts()", 1)[1].split("function bootCharts()", 1)[0]
    assert "renderHtmlChartFallbacks();" not in render_body
    assert "catch(error =>" in script
    assert "renderHtmlChartFallbacks();" in script.split("catch(error =>", 1)[1]
