aprs.me/test/aprsme/regex_cache_test.exs
Graham McIntire 403300f696
test: broaden unit & property-test coverage (61.8% → 64.7%)
Adds ~800 new tests (incl. 27 StreamData properties) across 22 modules
that previously had little or no coverage. Highlights:

- Pure utility modules (LogSanitizer, PacketFieldWhitelist,
  PacketSanitizer, DeviceParser, CoordinateUtils, BoundsUtils,
  ParamUtils, Convert, WeatherUnits) get thorough unit + property
  coverage including UTF-8 truncation boundaries, antimeridian
  longitude wrap, Haversine symmetry, and unit-conversion inverses.
- JSON view modules (CallsignJSON, ErrorJSON, ChangesetJSON) verified
  for envelope shape and error templating.
- Plugs (HealthCheck, RateLimiter, ApiCSRF) exercised for happy-path
  and halt paths.
- GenServer modules (RegexCache, CleanupScheduler, DeploymentNotifier)
  tested without touching the supervised singleton where possible.
- Drops the orphaned map_live/map_helpers_test.exs whose module name
  collided with the new live/shared/coordinate_utils_test.exs.
2026-04-23 13:32:09 -05:00

59 lines
2.1 KiB
Elixir

defmodule Aprsme.RegexCacheTest do
# async: false — the cache is a singleton GenServer owning a named ETS table
# shared across the whole application. Parallel tests would see each other.
use ExUnit.Case, async: false
alias Aprsme.RegexCache
# Each test gets a unique pattern to avoid cross-test pollution of the
# shared cache (we can't safely clear it without racing other callers).
defp unique_pattern(tag) do
suffix = System.unique_integer([:positive, :monotonic])
"#{tag}_#{suffix}"
end
describe "get_or_compile/1" do
test "compiles and returns a Regex on first call" do
pattern = unique_pattern("hello")
assert {:ok, regex} = RegexCache.get_or_compile(pattern)
assert %Regex{} = regex
assert Regex.match?(regex, pattern)
end
test "returns the same compiled regex on subsequent calls" do
pattern = unique_pattern("same")
assert {:ok, r1} = RegexCache.get_or_compile(pattern)
assert {:ok, r2} = RegexCache.get_or_compile(pattern)
assert r1 == r2
end
test "returns {:error, _} for invalid patterns" do
assert {:error, _reason} = RegexCache.get_or_compile("[")
assert {:error, _reason} = RegexCache.get_or_compile("(unbalanced")
end
test "does not cache failed compilations" do
assert {:error, _} = RegexCache.get_or_compile("[bad")
# Still an error on retry — error path didn't poison the cache with nil.
assert {:error, _} = RegexCache.get_or_compile("[bad")
end
test "distinct patterns produce distinct regexes" do
p1 = unique_pattern("a")
p2 = unique_pattern("b")
assert {:ok, r1} = RegexCache.get_or_compile(p1)
assert {:ok, r2} = RegexCache.get_or_compile(p2)
refute r1 == r2
end
test "compiled regex actually matches as expected" do
pattern = unique_pattern("match") <> "\\d+"
assert {:ok, regex} = RegexCache.get_or_compile(pattern)
base = String.trim_trailing(pattern, "\\d+")
assert Regex.match?(regex, "#{base}123")
refute Regex.match?(regex, "nope")
end
end
end