<commit_before>defmodule FarmbotFirmware.CommandTest do
  use ExUnit.Case
  use Mimic
  setup :verify_on_exit!
  alias FarmbotFirmware.Command
  import ExUnit.CaptureLog
  @subject FarmbotFirmware.Command

  test "command() runs RPCs" do
    arg = [transport: FarmbotFirmware.StubTransport]
    {:ok, pid} = FarmbotFirmware.start_link(arg, [])
    send(pid, :timeout)
    {:error, message} = @subject.command(pid, {:a, {:b, :c}})
    assert "Can't send command when in :boot state" == message
  end

  test "command() refuses to run RPCs in :boot state" do
    arg = [transport: FarmbotFirmware.StubTransport]
    {:ok, pid} = FarmbotFirmware.start_link(arg, [])
    send(pid, :timeout)
    {:error, message} = @subject.command(pid, {:a, {:b, :c}})
    assert "Can't send command when in :boot state" == message
  end

  test "enable_debug_logs" do
    Application.put_env(:farmbot_firmware, @subject, foo: :bar, debug_log: false)

    old_env = Application.get_env(:farmbot_firmware, @subject)

    assert false == Keyword.fetch!(old_env, :debug_log)
    assert :bar == Keyword.fetch!(old_env, :foo)
    assert false == @subject.debug?()

    refute capture_log(fn ->
             @subject.debug_log("Never Shown")
           end) =~ "Never Shown"

    # === Change ENV settings
    assert :ok ==
             Command.enable_debug_logs()

    assert capture_log(fn ->
             @subject.debug_log("Good!")
           end) =~ "Good!"

    assert true == @subject.debug?()
    new_env = Application.get_env(:farmbot_firmware, @subject)
    assert true == Keyword.fetch!(new_env, :debug_log)
    assert :bar == Keyword.fetch!(new_env, :foo)

    # === And back again
    assert :ok == Command.disable_debug_logs()
    even_newer = Application.get_env(:farmbot_firmware, @subject)

    assert false == Keyword.fetch!(even_newer, :debug_log)
    assert :bar == Keyword.fetch!(even_newer, :foo)
    assert false == @subject.debug?()

    refute capture_log(fn ->
             @subject.debug_log("Also Never Shown")
           end) =~ "Also Never Shown"
  end
end
<commit_msg>TEST (40.1%): command() runs RPCs<commit_after>defmodule FarmbotFirmware.CommandTest do
  use ExUnit.Case
  use Mimic
  setup :verify_on_exit!
  alias FarmbotFirmware.Command
  import ExUnit.CaptureLog
  @subject FarmbotFirmware.Command

  @tag :this_is_the_bug
  test "command() runs RPCs" do
    arg = [transport: FarmbotFirmware.StubTransport]
    {:ok, pid} = FarmbotFirmware.start_link(arg, [])
    send(pid, :timeout)

    assert {:error, :emergency_lock} ==
             @subject.command(pid, {:command_emergency_lock, []})

    assert :ok == @subject.command(pid, {:command_emergency_unlock, []})
    cmd = {:parameter_write, [movement_stop_at_home_x: 0.0]}
    assert :ok == @subject.command(pid, cmd)
  end

  test "command() refuses to run RPCs in :boot state" do
    arg = [transport: FarmbotFirmware.StubTransport]
    {:ok, pid} = FarmbotFirmware.start_link(arg, [])
    send(pid, :timeout)
    {:error, message} = @subject.command(pid, {:a, {:b, :c}})
    assert "Can't send command when in :boot state" == message
  end

  test "enable_debug_logs" do
    Application.put_env(:farmbot_firmware, @subject, foo: :bar, debug_log: false)

    old_env = Application.get_env(:farmbot_firmware, @subject)

    assert false == Keyword.fetch!(old_env, :debug_log)
    assert :bar == Keyword.fetch!(old_env, :foo)
    assert false == @subject.debug?()

    refute capture_log(fn ->
             @subject.debug_log("Never Shown")
           end) =~ "Never Shown"

    # === Change ENV settings
    assert :ok ==
             Command.enable_debug_logs()

    assert capture_log(fn ->
             @subject.debug_log("Good!")
           end) =~ "Good!"

    assert true == @subject.debug?()
    new_env = Application.get_env(:farmbot_firmware, @subject)
    assert true == Keyword.fetch!(new_env, :debug_log)
    assert :bar == Keyword.fetch!(new_env, :foo)

    # === And back again
    assert :ok == Command.disable_debug_logs()
    even_newer = Application.get_env(:farmbot_firmware, @subject)

    assert false == Keyword.fetch!(even_newer, :debug_log)
    assert :bar == Keyword.fetch!(even_newer, :foo)
    assert false == @subject.debug?()

    refute capture_log(fn ->
             @subject.debug_log("Also Never Shown")
           end) =~ "Also Never Shown"
  end
end

{'fraction_non_alphanumeric': 0.1362591911764706, 'fraction_numerical': 0.0011488970588235295, 'mean_word_length': 3.423780487804878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3135501', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1455, 'n_words': 336}