aprs.me/test/aprsme/cluster/topology_test.exs
Graham McIntire 7db78675a2
refactor: collapse nested if/case into pattern-matched helpers
- SharedPacketHandler.handle_packet_update: dispatch on filter-result,
  and use maybe_enrich/2 dispatched on the enrich? boolean instead of
  an inner `if`. lookup_device_info/1 is now a trio of function heads.
- Cluster.Topology.child_spec: build_spec/3 dispatches on
  (cluster_enabled?, topologies) tuples — no more nested `if`.
- Cluster.PacketDistributor.distribute_packet: maybe_broadcast/2
  dispatches on the leader-and-enabled check; the non-leader / disabled
  paths return :ok explicitly instead of relying on an `if` expression's
  implicit nil.

Adds tests for SharedPacketHandler (filter/enrich/match paths,
callsign_filter + callsign_and_weather_filter factories) and extends
Cluster.Topology tests to cover the nil-topology and opts-merge cases.
Coverage 66.41 → 66.81%.
2026-04-23 13:55:11 -05:00

71 lines
2.3 KiB
Elixir

defmodule Aprsme.Cluster.TopologyTest do
use ExUnit.Case, async: false
alias Aprsme.Cluster.Topology
alias Cluster.Strategy.Gossip
setup do
on_exit(fn ->
Application.put_env(:aprsme, :cluster_enabled, false)
Application.put_env(:libcluster, :topologies, [])
end)
:ok
end
describe "start_link/1" do
test "returns :ignore" do
assert :ignore = Topology.start_link([])
end
end
describe "child_spec/1" do
test "returns no-op spec when cluster is disabled" do
Application.put_env(:aprsme, :cluster_enabled, false)
spec = Topology.child_spec([])
assert %{id: Topology, start: {Topology, :start_link, [[]]}, type: :worker} = spec
end
test "returns no-op spec when cluster is enabled but no topologies configured" do
Application.put_env(:aprsme, :cluster_enabled, true)
Application.put_env(:libcluster, :topologies, [])
spec = Topology.child_spec([])
assert %{id: Topology, start: {Topology, :start_link, [[]]}, type: :worker} = spec
end
test "returns Cluster.Supervisor tuple when cluster is enabled with valid topologies" do
Application.put_env(:aprsme, :cluster_enabled, true)
Application.put_env(:libcluster, :topologies, gossip: [strategy: Gossip])
spec = Topology.child_spec([])
assert {Cluster.Supervisor, [topologies, supervisor_opts]} = spec
assert topologies == [gossip: [strategy: Gossip]]
assert Keyword.get(supervisor_opts, :name) == Aprsme.ClusterSupervisor
end
test "returns no-op spec when topologies is nil" do
Application.put_env(:aprsme, :cluster_enabled, true)
Application.put_env(:libcluster, :topologies, nil)
spec = Topology.child_spec([])
assert %{id: Topology, start: {Topology, :start_link, [[]]}, type: :worker} = spec
end
test "merges caller opts onto the default supervisor name" do
Application.put_env(:aprsme, :cluster_enabled, true)
Application.put_env(:libcluster, :topologies, gossip: [strategy: Gossip])
spec = Topology.child_spec(shutdown: :brutal_kill)
assert {Cluster.Supervisor, [_, supervisor_opts]} = spec
assert supervisor_opts[:shutdown] == :brutal_kill
assert supervisor_opts[:name] == Aprsme.ClusterSupervisor
end
end
end