Add Aprsme.Is tests for send_message live branch, init disable_connection branch, and start_link

This commit is contained in:
Graham McIntire 2026-05-08 17:09:06 -05:00
parent 02d435557a
commit e2c0b00238
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -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})