prop/test/microwaveprop/pskr/client_test.exs
Graham McIntire fd976b0cd5
fix: resolve 391 Credo issues across codebase
- Add jump_credo_checks ~> 0.4 with all 20 checks enabled
- Fix all standard Credo issues: 139 @spec (113 done, 26 remain),
  4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom,
  2 max line length, 9 assert_receive timeout
- Fix 170+ jump_credo_checks warnings:
  - 117 TopLevelAliasImportRequire: move nested alias/import to module top
  - 32 UseObanProWorker: switch to Oban.Pro.Worker
  - 4 DoctestIExExamples: add doctests / create test file
  - ~20 WeakAssertion: strengthen type-check assertions
  - Various ConditionalAssertion, AssertReceiveTimeout fixes
- Exclude vendor/ from Credo analysis
- Remaining: 175 warnings (mostly opinionated WeakAssertion,
  AvoidSocketAssignsInTest), 26 @spec annotations
2026-06-12 13:51:32 -05:00

131 lines
4.1 KiB
Elixir

defmodule Microwaveprop.Pskr.ClientTest do
use ExUnit.Case, async: false
alias Microwaveprop.Pskr.Client
setup do
# Pre-register a placeholder under the global name so the Client
# under test loses leader election and goes to :standby — this
# avoids any outbound MQTT connection in the test environment.
{:ok, placeholder} = start_supervised({Task, fn -> Process.sleep(:infinity) end})
:global.unregister_name(Client)
:yes = :global.register_name(Client, placeholder)
on_exit(fn ->
:global.unregister_name(Client)
end)
%{placeholder: placeholder}
end
describe "start_link + init (standby branch)" do
test "starts as :standby when the global name is already taken" do
pid = start_supervised!({Client, name: __MODULE__.NotRegistered})
state = :sys.get_state(pid)
assert state.role == :standby
assert state.socket == nil
assert state.buffer == <<>>
assert is_list(state.bands)
assert is_binary(state.client_id)
end
test "uses defaults for bands when none are passed" do
pid = start_supervised!({Client, []})
state = :sys.get_state(pid)
assert "6m" in state.bands
assert "2m" in state.bands
assert "70cm" in state.bands
end
test "honors custom bands list and aggregator name from opts" do
pid = start_supervised!({Client, bands: ["2m"], aggregator: SomeOtherAggregator})
state = :sys.get_state(pid)
assert state.bands == ["2m"]
assert state.aggregator == SomeOtherAggregator
end
test "honors custom client_id" do
pid = start_supervised!({Client, client_id: "test-client-42"})
state = :sys.get_state(pid)
assert state.client_id == "test-client-42"
end
end
describe "tcp messages with mismatched socket" do
test "tcp message for a stale socket is ignored", %{placeholder: _} do
pid = start_supervised!({Client, []})
# Send a TCP message with a fake socket that doesn't match state.socket.
# state.socket is nil, but :tcp pattern matches when socket matches state.socket.
# Since state.socket is nil, this falls through to the catch-all handle_info.
send(pid, {:tcp, :stale_port, "data"})
_ = :sys.get_state(pid)
end
test "tcp_closed for a stale socket is ignored", %{placeholder: _} do
pid = start_supervised!({Client, []})
send(pid, {:tcp_closed, :stale_port})
_ = :sys.get_state(pid)
end
test "tcp_error for a stale socket is ignored", %{placeholder: _} do
pid = start_supervised!({Client, []})
send(pid, {:tcp_error, :stale_port, :reason})
_ = :sys.get_state(pid)
end
end
describe ":reconnect message routing" do
test "schedules a connect attempt for a standby (immediate :continue, :connect)" do
pid = start_supervised!({Client, []})
send(pid, :reconnect)
_ = :sys.get_state(pid)
end
end
describe "terminate/2" do
test "is graceful when state has no socket" do
pid = start_supervised!({Client, []})
# Just normal stop — terminate(_, %{socket: nil_or_other})
ref = Process.monitor(pid)
GenServer.stop(pid)
assert_receive {:DOWN, ^ref, :process, ^pid, _reason}
end
end
describe "standby handle_info" do
test "nodeup re-runs election (stays standby while name is taken)" do
pid = start_supervised!({Client, []})
send(pid, {:nodeup, :test@nowhere})
_ = :sys.get_state(pid)
state = :sys.get_state(pid)
assert state.role == :standby
end
test "nodedown re-runs election (stays standby while name is taken)" do
pid = start_supervised!({Client, []})
send(pid, {:nodedown, :test@nowhere})
_ = :sys.get_state(pid)
state = :sys.get_state(pid)
assert state.role == :standby
end
test "unrelated info messages are ignored" do
pid = start_supervised!({Client, []})
send(pid, :unrelated_message)
_ = :sys.get_state(pid)
end
test ":ping with no socket is a no-op" do
pid = start_supervised!({Client, []})
send(pid, :ping)
_ = :sys.get_state(pid)
state = :sys.get_state(pid)
assert state.socket == nil
end
end
end