2.4 KiB · text History b868907
defmodule GitGud.Badges.PaletteTest do
  use ExUnit.Case, async: true

  alias GitGud.Badges.Oklch
  alias GitGud.Badges.Palette

  # The contrast gate: a theme edited in app.css that leaves a badge
  # unreadable fails here, before it ships.
  for style <- ~w(split dot outline) do
    test "every #{style} pair reaches its minimum in every theme" do
      failures =
        for theme <- Palette.themes(),
            {name, fg, bg, min} <- Palette.audit_pairs(unquote(style)),
            ratio = Palette.contrast(theme, fg, bg),
            ratio < min,
            do: "#{theme} #{name}: #{Float.round(ratio, 2)} < #{min}"

      assert failures == []
    end
  end

  test "every theme is read from app.css" do
    for theme <- GitGud.Themes.all() do
      assert Palette.token_hex(theme, "base-100") =~ ~r/^#[0-9a-f]{6}$/
    end
  end

  test "the GG mark is each theme's accent where it reads" do
    assert Palette.token("dark", "gg-fg") == "accent"
    assert Palette.token("light", "gg-fg") == "accent"
    assert Palette.token("forest", "gg-fg") == "accent"
    # And on the dot and outline plates too: pink on dark, not a fallback.
    for theme <- Palette.themes(), do: assert(Palette.token(theme, "mark") == "accent")
    # The neiam themes' accent is the yellow.
    assert Palette.hex("forest", "gg-fg") == Palette.hex("her", "gg-fg")
  end

  test "dark and light borrow blueprint's primary for their info blue" do
    blueprint = Palette.token_hex("blueprint", "primary")

    for theme <- ~w(dark light) do
      assert Palette.hex(theme, "info-bg") == blueprint
      assert Palette.hex(theme, "info-fg") == Palette.token_hex("blueprint", "primary-content")
    end

    # Only for badges: the neiam themes keep their own.
    refute Palette.hex("forest", "info-bg") == blueprint
  end

  test "light's status blocks swap to dark ink" do
    assert Palette.token("light", "success-fg") == "base-content"
  end

  test "oklch converts to the sRGB a browser would show" do
    assert Oklch.hex(Oklch.to_linear(100, 0, 0)) == "#ffffff"
    assert Oklch.hex(Oklch.to_linear(0, 0, 0)) == "#000000"
    assert_in_delta Oklch.contrast(1.0, 0.0), 21.0, 0.001
  end

  test "the inline stylesheet covers every theme and role" do
    css = Palette.inline_stylesheet()

    for theme <- GitGud.Themes.all(), do: assert(css =~ "[data-theme=#{theme}] .ggb{")
    for role <- Palette.roles(), do: assert(css =~ ".ggb .f-#{role}{")
  end
end