- DeviceCache.pattern_matches?: replace a 3-branch `cond` with a two-clause `case` on `String.contains?(pattern, "?")`. The old `*` branch was dead code (it did the same exact-match compare as the fallback), so it's removed. - Telemetry.DatabaseMetrics.collect_postgres_metrics: dispatch on environment atom via multi-clause private functions instead of `if Application.get_env(...) == :test`. New tests: - DeviceCache wildcard/exact lookup paths exercised via a seeded cache. - MobileUserSocket connect/3 for x_headers/peer_data/empty info and rate-limit deny. - DatabaseMetrics pool-metrics emission captured via :telemetry handler and the :test-env short-circuit.
75 lines
2.4 KiB
Elixir
75 lines
2.4 KiB
Elixir
defmodule Aprsme.DeviceCacheTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
alias Aprsme.DeviceCache
|
|
|
|
setup do
|
|
pid = Process.whereis(DeviceCache)
|
|
|
|
if pid do
|
|
:erlang.unregister(DeviceCache)
|
|
end
|
|
|
|
on_exit(fn ->
|
|
if pid && !Process.whereis(DeviceCache) do
|
|
true = Process.register(pid, DeviceCache)
|
|
end
|
|
end)
|
|
|
|
:ok
|
|
end
|
|
|
|
test "refresh_cache/0 returns :ok when the cache server is not running" do
|
|
assert DeviceCache.refresh_cache() == :ok
|
|
end
|
|
|
|
test "lookup_device/1 returns nil on cache miss without crashing when server is not running" do
|
|
assert DeviceCache.lookup_device("TEST-DEVICE") == nil
|
|
end
|
|
|
|
test "lookup_device/1 returns nil for nil input" do
|
|
assert DeviceCache.lookup_device(nil) == nil
|
|
end
|
|
|
|
describe "wildcard pattern matching via cache" do
|
|
setup do
|
|
# Seed the cache directly with a small device list so we can exercise
|
|
# the pattern-matching branches without running the GenServer.
|
|
devices = [
|
|
%Aprsme.Devices{identifier: "APSK21", vendor: "Kenwood", model: "TH-D74"},
|
|
%Aprsme.Devices{identifier: "APS???", vendor: "Kenwood", model: "Unknown"},
|
|
%Aprsme.Devices{identifier: "EXACT", vendor: "Test", model: "Exact"}
|
|
]
|
|
|
|
Aprsme.Cache.put(:device_cache, :all_devices, devices)
|
|
|
|
on_exit(fn -> Aprsme.Cache.put(:device_cache, :all_devices, []) end)
|
|
:ok
|
|
end
|
|
|
|
test "exact identifier matches literally" do
|
|
assert %{vendor: "Test"} = DeviceCache.lookup_device("EXACT")
|
|
end
|
|
|
|
test "returns nil for non-matching identifier" do
|
|
assert DeviceCache.lookup_device("DOES-NOT-EXIST") == nil
|
|
end
|
|
|
|
test "prefers exact match when both wildcard and exact apply" do
|
|
# Both EXACT (literal) and APS??? would not both apply here; just verify
|
|
# that exact-string hits don't get confused with wildcard entries.
|
|
assert %{identifier: "EXACT"} = DeviceCache.lookup_device("EXACT")
|
|
end
|
|
|
|
test "? wildcards match a single character" do
|
|
# APSK21 matches APS??? (three wildcards after APS)
|
|
assert %{identifier: _} = DeviceCache.lookup_device("APSK21")
|
|
end
|
|
|
|
test "wildcard pattern does not match if length mismatches" do
|
|
# APS??? requires exactly 3 trailing chars; "APSXX" has only 2 and
|
|
# shouldn't match the wildcard entry (nor is there any other entry).
|
|
assert DeviceCache.lookup_device("APSXX") == nil
|
|
end
|
|
end
|
|
end
|