defmodule TarakanWeb.ThemeBootTest do
  use TarakanWeb.ConnCase, async: true

  test "the theme script is blocking and precedes the deferred bundle", %{conn: conn} do
    html = conn |> get(~p"/") |> html_response(200)

    theme_at = :binary.match(html, "/assets/js/theme") |> elem(0)
    app_at = :binary.match(html, "/assets/js/app") |> elem(0)

    assert theme_at < app_at, "theme.js must load before app.js"

    # The whole point: it must not be deferred, or it runs after first paint
    # and the toggle flashes the wrong option again.
    [theme_tag] = Regex.run(~r|<script[^>]*/assets/js/theme[^>]*>|, html)
    refute theme_tag =~ "defer"
    refute theme_tag =~ "type=\"module\""

    # And the bundle that needs to stay deferred still is.
    [app_tag] = Regex.run(~r|<script[^>]*/assets/js/app[^>]*>|, html)
    assert app_tag =~ "defer"
  end
end
