defmodule LogflareWeb.BertParserTest do
  @moduledoc false
  use LogflareWeb.ConnCase
  alias LogflareWeb.BertParser

  describe "LogflareWeb.BertParser" do
    test "decodes a typical log batch post request", %{conn: conn} do
      data = %{
        "batch" => [
          %{
            "level" => "info",
            "message" => "info log",
            "metadata" => %{
              "context" => %{
                "application" => "logflare_logger_pinger",
                "file" => "lib/logflare_pinger/log_pinger.ex",
                "function" => "handle_info/2",
                "line" => 20,
                "module" => "Elixir.LogflareLoggerPinger.Server",
                "pid" => "<0.319.0>"
              },
              "context_key" => 1,
              "current_user" => %{"id" => "gurgeh"}
            },
            "timestamp" => "2019-05-15T20:05:09.000903+03:00"
          },
          %{
            "level" => "info",
            "message" => "info log",
            "metadata" => %{
              "context" => %{
                "application" => "logflare_logger_pinger",
                "file" => "lib/logflare_pinger/log_pinger.ex",
                "function" => "handle_info/2",
                "line" => 20,
                "module" => "Elixir.LogflareLoggerPinger.Server",
                "pid" => "<0.319.0>"
              },
              "context_key" => 1,
              "current_user" => %{"id" => "gurgeh"}
            },
            "timestamp" => "2019-05-15T20:05:08.000898+03:00"
          }
        ],
        "source_name" => "dashboard1"
      }

      body =
        <<131, 104, 3, 100, 0, 4, 98, 101, 114, 116, 100, 0, 4, 100, 105, 99, 116, 108, 0, 0, 0,
          2, 104, 2, 109, 0, 0, 0, 5, 98, 97, 116, 99, 104, 108, 0, 0, 0, 2, 116, 0, 0, 0, 4, 109,
          0, 0, 0, 5, 108, 101, 118, 101, 108, 109, 0, 0, 0, 4, 105, 110, 102, 111, 109, 0, 0, 0,
          7, 109, 101, 115, 115, 97, 103, 101, 109, 0, 0, 0, 8, 105, 110, 102, 111, 32, 108, 111,
          103, 109, 0, 0, 0, 8, 109, 101, 116, 97, 100, 97, 116, 97, 116, 0, 0, 0, 3, 109, 0, 0,
          0, 7, 99, 111, 110, 116, 101, 120, 116, 116, 0, 0, 0, 6, 109, 0, 0, 0, 11, 97, 112, 112,
          108, 105, 99, 97, 116, 105, 111, 110, 109, 0, 0, 0, 22, 108, 111, 103, 102, 108, 97,
          114, 101, 95, 108, 111, 103, 103, 101, 114, 95, 112, 105, 110, 103, 101, 114, 109, 0, 0,
          0, 4, 102, 105, 108, 101, 109, 0, 0, 0, 33, 108, 105, 98, 47, 108, 111, 103, 102, 108,
          97, 114, 101, 95, 112, 105, 110, 103, 101, 114, 47, 108, 111, 103, 95, 112, 105, 110,
          103, 101, 114, 46, 101, 120, 109, 0, 0, 0, 8, 102, 117, 110, 99, 116, 105, 111, 110,
          109, 0, 0, 0, 13, 104, 97, 110, 100, 108, 101, 95, 105, 110, 102, 111, 47, 50, 109, 0,
          0, 0, 4, 108, 105, 110, 101, 97, 20, 109, 0, 0, 0, 6, 109, 111, 100, 117, 108, 101, 109,
          0, 0, 0, 34, 69, 108, 105, 120, 105, 114, 46, 76, 111, 103, 102, 108, 97, 114, 101, 76,
          111, 103, 103, 101, 114, 80, 105, 110, 103, 101, 114, 46, 83, 101, 114, 118, 101, 114,
          109, 0, 0, 0, 3, 112, 105, 100, 109, 0, 0, 0, 9, 60, 48, 46, 51, 49, 57, 46, 48, 62,
          109, 0, 0, 0, 11, 99, 111, 110, 116, 101, 120, 116, 95, 107, 101, 121, 97, 1, 109, 0, 0,
          0, 12, 99, 117, 114, 114, 101, 110, 116, 95, 117, 115, 101, 114, 116, 0, 0, 0, 1, 109,
          0, 0, 0, 2, 105, 100, 109, 0, 0, 0, 6, 103, 117, 114, 103, 101, 104, 109, 0, 0, 0, 9,
          116, 105, 109, 101, 115, 116, 97, 109, 112, 109, 0, 0, 0, 32, 50, 48, 49, 57, 45, 48,
          53, 45, 49, 53, 84, 50, 48, 58, 48, 53, 58, 48, 57, 46, 48, 48, 48, 57, 48, 51, 43, 48,
          51, 58, 48, 48, 116, 0, 0, 0, 4, 109, 0, 0, 0, 5, 108, 101, 118, 101, 108, 109, 0, 0, 0,
          4, 105, 110, 102, 111, 109, 0, 0, 0, 7, 109, 101, 115, 115, 97, 103, 101, 109, 0, 0, 0,
          8, 105, 110, 102, 111, 32, 108, 111, 103, 109, 0, 0, 0, 8, 109, 101, 116, 97, 100, 97,
          116, 97, 116, 0, 0, 0, 3, 109, 0, 0, 0, 7, 99, 111, 110, 116, 101, 120, 116, 116, 0, 0,
          0, 6, 109, 0, 0, 0, 11, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 109, 0, 0,
          0, 22, 108, 111, 103, 102, 108, 97, 114, 101, 95, 108, 111, 103, 103, 101, 114, 95, 112,
          105, 110, 103, 101, 114, 109, 0, 0, 0, 4, 102, 105, 108, 101, 109, 0, 0, 0, 33, 108,
          105, 98, 47, 108, 111, 103, 102, 108, 97, 114, 101, 95, 112, 105, 110, 103, 101, 114,
          47, 108, 111, 103, 95, 112, 105, 110, 103, 101, 114, 46, 101, 120, 109, 0, 0, 0, 8, 102,
          117, 110, 99, 116, 105, 111, 110, 109, 0, 0, 0, 13, 104, 97, 110, 100, 108, 101, 95,
          105, 110, 102, 111, 47, 50, 109, 0, 0, 0, 4, 108, 105, 110, 101, 97, 20, 109, 0, 0, 0,
          6, 109, 111, 100, 117, 108, 101, 109, 0, 0, 0, 34, 69, 108, 105, 120, 105, 114, 46, 76,
          111, 103, 102, 108, 97, 114, 101, 76, 111, 103, 103, 101, 114, 80, 105, 110, 103, 101,
          114, 46, 83, 101, 114, 118, 101, 114, 109, 0, 0, 0, 3, 112, 105, 100, 109, 0, 0, 0, 9,
          60, 48, 46, 51, 49, 57, 46, 48, 62, 109, 0, 0, 0, 11, 99, 111, 110, 116, 101, 120, 116,
          95, 107, 101, 121, 97, 1, 109, 0, 0, 0, 12, 99, 117, 114, 114, 101, 110, 116, 95, 117,
          115, 101, 114, 116, 0, 0, 0, 1, 109, 0, 0, 0, 2, 105, 100, 109, 0, 0, 0, 6, 103, 117,
          114, 103, 101, 104, 109, 0, 0, 0, 9, 116, 105, 109, 101, 115, 116, 97, 109, 112, 109, 0,
          0, 0, 32, 50, 48, 49, 57, 45, 48, 53, 45, 49, 53, 84, 50, 48, 58, 48, 53, 58, 48, 56,
          46, 48, 48, 48, 56, 57, 56, 43, 48, 51, 58, 48, 48, 106, 104, 2, 109, 0, 0, 0, 11, 115,
          111, 117, 114, 99, 101, 95, 110, 97, 109, 101, 109, 0, 0, 0, 10, 100, 97, 115, 104, 98,
          111, 97, 114, 100, 49, 106>>

      assert BertParser.decode({:ok, body, conn}) == {:ok, data, conn}
    end
  end
end
