defmodule AprsmeWeb.TelemetryTest do
  use ExUnit.Case, async: true

  alias AprsmeWeb.Telemetry

  describe "metrics/0" do
    test "returns a non-empty list of metric definitions" do
      metrics = Telemetry.metrics()
      refute Enum.empty?(metrics)
    end

    test "includes Phoenix endpoint, router, and live_view metrics" do
      names =
        Telemetry.metrics()
        |> Enum.map(& &1.name)
        |> Enum.map(&Enum.join(&1, "."))

      assert Enum.any?(names, &String.starts_with?(&1, "phoenix.endpoint"))
      assert Enum.any?(names, &String.starts_with?(&1, "phoenix.router_dispatch"))
    end

    test "includes Postgres metrics for connections, packets_table and replication" do
      names =
        Telemetry.metrics()
        |> Enum.map(& &1.name)
        |> Enum.map(&Enum.join(&1, "."))

      assert Enum.any?(names, &String.contains?(&1, "aprsme.postgres.connections"))
      assert Enum.any?(names, &String.contains?(&1, "aprsme.postgres.packets_table"))
      assert Enum.any?(names, &String.contains?(&1, "aprsme.postgres.replication"))
    end

    test "every metric has a defined name and event_name" do
      Enum.each(Telemetry.metrics(), fn m ->
        assert m.name != []
        assert m.event_name != []
      end)
    end
  end
end
