defmodule AprsmeWeb.Api.V1.ErrorJSONTest do
  use ExUnit.Case, async: true

  alias AprsmeWeb.Api.V1.ErrorJSON

  describe "render/2 with explicit messages" do
    test "renders a generic error with message and code" do
      assert ErrorJSON.render("error.json", %{message: "boom"}) == %{
               error: %{message: "boom", code: "generic_error"}
             }
    end
  end

  describe "render/2 for status-keyed templates" do
    test "renders 404 with not_found code" do
      result = ErrorJSON.render("404.json", %{})
      assert result.error.code == "not_found"
      assert String.length(result.error.message) > 0
    end

    test "renders 500 with internal_server_error code" do
      assert ErrorJSON.render("500.json", %{}).error.code == "internal_server_error"
    end

    test "renders 422 with unprocessable_entity code" do
      assert ErrorJSON.render("422.json", %{}).error.code == "unprocessable_entity"
    end

    test "renders 400 with bad_request code" do
      assert ErrorJSON.render("400.json", %{}).error.code == "bad_request"
    end

    test "renders 401 with unauthorized code" do
      assert ErrorJSON.render("401.json", %{}).error.code == "unauthorized"
    end

    test "renders 403 with forbidden code" do
      assert ErrorJSON.render("403.json", %{}).error.code == "forbidden"
    end

    test "renders 408 with request_timeout code" do
      assert ErrorJSON.render("408.json", %{}).error.code == "request_timeout"
    end
  end

  describe "render/2 fallback" do
    test "renders unknown templates with generic unknown_error code" do
      result = ErrorJSON.render("418.json", %{})
      assert result.error.code == "unknown_error"
      assert String.length(result.error.message) > 0
    end
  end
end
