"""
30.09.2026: песок из-под колёс погрузчика в шапке.
"""

import pathlib

ROOT = pathlib.Path(__file__).resolve().parents[2]
ENGINE = ROOT / "engine" / "timesheet"
T = ENGINE / "templates"
read = lambda p: p.read_text(encoding="utf-8")
BASE = read(T / "base.html")
CSS = read(ENGINE / "static" / "app.css")


def test_sand_particle_style_and_flight():
    """Частица живёт в координатах шапки, летит против движения и гаснет."""
    assert ".fl-sand {" in CSS
    assert "animation: fl-sand var(--dur, .6s)" in CSS
    assert "@keyframes fl-sand" in CSS
    #  долёт — только вперёд: заставка не оставит висящих пылинок
    assert "animation: fl-sand var(--dur, .6s) cubic-bezier(.25,.5,.5,1) forwards" in CSS
    #  направление задаются переменными, а не двумя наборами ключей
    assert "translate(calc(var(--dx) * .45), var(--dy))" in CSS


def test_sand_spawns_under_wheels_against_direction():
    """Генератор считает направление езды и кладёт песок в шапку."""
    assert 'var dir = x > from ? 1 : -1;' in BASE
    #  --dx со знаком, обратным движению: едет вправо — песок влево
    assert 'p.style.setProperty("--dx", (-dir * (9 + Math.random() * 17))' in BASE
    assert 'var wheels = [11.5, 28.5];' in BASE
    assert 'bar.appendChild(' in BASE
    assert 'p.className = "fl-sand";' in BASE


def test_sand_stops_at_ride_end_and_filters_bubbling_events():
    """Конец езды гасит генератор; transitionend от svg-наклона не считается."""
    assert "e.target !== fl || e.propertyName !== \"left\"" in BASE
    assert "clearInterval(sand);" in BASE
    #  запасной сбор: частица не должна пережить свою анимацию
    assert "if (node.parentNode) node.remove();" in BASE


def test_sand_only_when_the_ride_is_animated():
    """Песок — часть анимированной езды: при отключённой анимации его нет."""
    #  генератор запускается внутри ветки анимации, после проверки animate
    assert 'var animate = mode === "on" || (mode === "auto" && !reduce);' in BASE
    spawner = BASE.index("var sand = setInterval")
    animate_check = BASE.index("if (animate) {")
    assert animate_check < spawner


def test_sand_wears_brand_colors():
    """Цвет пылинок — фирменные концы градиента самого погрузчика."""
    assert "background: #FF0066;" in CSS
    assert '"#990099"' in BASE


def test_forward_start_lifts_the_rear():
    """Старт вперёд — рывок разгона: нос клюёт, задняя часть поднимается."""
    assert ".topbar-forklift.fl-fwd svg { transform: rotate(2deg); }" in CSS
    assert "fl.classList.add(\"fl-fwd\");" in BASE
    #  наклон короткий: снимается сам и в обоих сценариях конца езды
    assert BASE.count("fl.classList.remove(\"fl-fwd\");") >= 3
