<commit_before>require Logger

defmodule Serverboards.LoggerTest do
  use ExUnit.Case
  @moduletag :capture_log

  setup do
    # Explicitly get a connection before each test
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Serverboards.Repo)
    # Setting the shared mode must be done only after checkout
    Ecto.Adapters.SQL.Sandbox.mode(Serverboards.Repo, {:shared, self()})
  end

  test "Simple formatting" do
    metadata =  [file: "app/serverboards/logger/logger.ex", line: 32, level: :debug, timestamp: Logger.Utils.timestamp(true), pid: self()]
    IO.puts Serverboards.Logger.Console.format("Message", metadata, Serverboards.Logger.Console.colors, true)

    metadata =  [file: "app/serverboards/logger/logger.ex", line: 32, level: :error, timestamp: Logger.Utils.timestamp(true), pid: self()]
    IO.puts Serverboards.Logger.Console.format("Message", metadata, Serverboards.Logger.Console.colors, false)
  end

  test "Get history" do
    {:ok, history} = Serverboards.Logger.history %{}
    assert Enum.count(history) <= 50

    Logger.debug("History debug: #{inspect Enum.count(history)}")

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10
  end

  test "Get history, filter by service" do
    Logger.remove_backend(Serverboards.Logger)
    Logger.add_backend(Serverboards.Logger, [])

    {:ok, user} = Serverboards.Auth.User.user_info("<EMAIL>")
    {:ok, uuid} = Serverboards.Service.service_add %{ "name" => "Test service", "tags" => ~w(tag1 tag2 tag3), "type" => "aabb" }, user

    Logger.flush

    {:ok, history} = Serverboards.Logger.history %{service: uuid}
    #Logger.info("History debug: #{inspect history}")
    #Logger.info("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) > 0
    assert Enum.count(history.lines) <= 50

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10

    Logger.remove_backend(Serverboards.Logger)
  end

  test "Get history, filter by query" do
    Logger.remove_backend(Serverboards.Logger)
    Logger.add_backend(Serverboards.Logger, [])

    {:ok, user} = Serverboards.Auth.User.user_info("<EMAIL>")
    {:ok, uuid} = Serverboards.Service.service_add %{ "name" => "Test service", "tags" => ~w(tag1 tag2 tag3), "type" => "aabb" }, user

    Logger.flush

    {:ok, history} = Serverboards.Logger.history %{q: "Test service Serverboards.Service"}
    # Logger.info("History debug: #{inspect history}")
    # Logger.info("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) > 0
    assert Enum.count(history.lines) <= 50

    {:ok, history} = Serverboards.Logger.history %{q: "Test service Serverboards.Service #{UUID.uuid4}"}
    # Logger.info("History debug: #{inspect history}")
    # Logger.info("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) == 0

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10

    Logger.remove_backend(Serverboards.Logger)

  end

end
<commit_msg>Conflated two log history search tests into one<commit_after>require Logger

defmodule Serverboards.LoggerTest do
  use ExUnit.Case
  @moduletag :capture_log

  setup do
    # Explicitly get a connection before each test
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Serverboards.Repo)
    # Setting the shared mode must be done only after checkout
    Ecto.Adapters.SQL.Sandbox.mode(Serverboards.Repo, {:shared, self()})
  end

  test "Simple formatting" do
    metadata =  [file: "app/serverboards/logger/logger.ex", line: 32, level: :debug, timestamp: Logger.Utils.timestamp(true), pid: self()]
    IO.puts Serverboards.Logger.Console.format("Message", metadata, Serverboards.Logger.Console.colors, true)

    metadata =  [file: "app/serverboards/logger/logger.ex", line: 32, level: :error, timestamp: Logger.Utils.timestamp(true), pid: self()]
    IO.puts Serverboards.Logger.Console.format("Message", metadata, Serverboards.Logger.Console.colors, false)
  end

  test "Get history" do
    {:ok, history} = Serverboards.Logger.history %{}
    assert Enum.count(history) <= 50

    Logger.debug("History debug: #{inspect Enum.count(history)}")

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10
  end

  test "Get history, filter by service and q" do
    Logger.remove_backend(Serverboards.Logger)
    Logger.add_backend(Serverboards.Logger, [])

    {:ok, user} = Serverboards.Auth.User.user_info("<EMAIL>")
    {:ok, uuid} = Serverboards.Service.service_add %{ "name" => "Test service", "tags" => ~w(tag1 tag2 tag3), "type" => "aabb" }, user

    Logger.flush

    {:ok, history} = Serverboards.Logger.history %{service: uuid}
    #Logger.info("History debug: #{inspect history}")
    #Logger.info("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) > 0
    assert Enum.count(history.lines) <= 50

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10

    # check by query

    {:ok, history} = Serverboards.Logger.history %{q: "Test service Serverboards.Service"}
    Logger.debug("History debug: #{inspect history}")
    Logger.debug("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) > 0
    assert Enum.count(history.lines) <= 50

    {:ok, history} = Serverboards.Logger.history %{q: "Test service Serverboards.Service #{UUID.uuid4}"}
    # Logger.info("History debug: #{inspect history}")
    # Logger.info("History debug: #{inspect Enum.count(history.lines)}")

    assert Enum.count(history.lines) == 0

    {:ok, history} = Serverboards.Logger.history %{count: 10}
    assert Enum.count(history) <= 10



    Logger.remove_backend(Serverboards.Logger)
  end

end

{'fraction_non_alphanumeric': 0.14656645029039972, 'fraction_numerical': 0.010420225486846601, 'mean_word_length': 4.163139329805997, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6709845', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1817, 'n_words': 436}