diff --git a/test/aprsme/is_test.exs b/test/aprsme/is_test.exs index 8133538..9a74d47 100644 --- a/test/aprsme/is_test.exs +++ b/test/aprsme/is_test.exs @@ -201,6 +201,74 @@ defmodule Aprsme.IsTest do end end + describe "send_message/1 when GenServer is running" do + test "calls into the live process" do + # Spawn a tiny GenServer registered as Aprsme.Is so send_message/1 hits + # the `_pid -> GenServer.call(__MODULE__, ...)` branch. + original = Process.whereis(Aprsme.Is) + if original, do: :erlang.unregister(Aprsme.Is) + + {:ok, fake_pid} = + GenServer.start(__MODULE__.FakeIs, :ok) + + Process.register(fake_pid, Aprsme.Is) + + try do + # The fake responds with :ok to {:send_message, _} calls. + assert Aprsme.Is.send_message("hello") == :ok + after + if Process.whereis(Aprsme.Is) == fake_pid, do: :erlang.unregister(Aprsme.Is) + if original && !Process.whereis(Aprsme.Is), do: Process.register(original, Aprsme.Is) + if Process.alive?(fake_pid), do: GenServer.stop(fake_pid) + end + end + end + + defmodule FakeIs do + @moduledoc false + use GenServer + + def init(state), do: {:ok, state} + def handle_call({:send_message, _msg}, _from, state), do: {:reply, :ok, state} + end + + describe "init/1 disable_connection branch in non-test env" do + test "init with non-test env + disable_connection=true returns {:stop, :test_environment_disabled}" do + original_env = Application.get_env(:aprsme, :env) + original_disable = Application.get_env(:aprsme, :disable_aprs_connection) + + try do + Application.put_env(:aprsme, :env, :prod) + Application.put_env(:aprsme, :disable_aprs_connection, true) + + # Hits do_init(:prod, true) — line 60 Logger.warning + {:stop, _}. + assert {:stop, :test_environment_disabled} = Aprsme.Is.init([]) + after + Application.put_env(:aprsme, :env, original_env) + Application.put_env(:aprsme, :disable_aprs_connection, original_disable) + end + end + end + + describe "start_link/1 in test env" do + test "starts and immediately returns an error due to {:stop, _} from init" do + # The GenServer link will exit when init returns {:stop, _}, so we run + # it in a temporarily trap-exit task to avoid taking down the test pid. + Process.flag(:trap_exit, true) + + try do + result = Aprsme.Is.start_link([]) + + assert match?({:error, :test_environment_disabled}, result) or + match?({:error, _}, result) + catch + :exit, _ -> :ok + after + Process.flag(:trap_exit, false) + end + end + end + describe "handle_info(:aprsme_no_message_timeout, ...)" do test "ignores timeout when socket is nil" do state = build_state(%{socket: nil})