aprs.me/test/aprsme_web/live/locale_hook_test.exs
Graham McIntire c5a82b77c1
refactor: pattern-match over conditionals in Navigation and InsertOptimizer
Swaps `if`/`cond` branches for multi-clause function heads or head-pattern
matching, making the control flow visible in the function signatures:

- Navigation.determine_map_location: resolve_location/4 now dispatches on
  the shape of the geolocation input and whether URL params are explicit.
- Navigation.handle_callsign_tracking: per-case clauses for empty callsign
  and explicit-URL-params short-circuits, plus a final clause that does the
  DB lookup and rescues once.
- Navigation.handle_callsign_search: dispatch_callsign_search/3 separates
  the valid/invalid callsign branches.
- InsertOptimizer.optimize_based_on_performance: pattern-match on
  [_, _, _ | _] to require ≥3 samples without calling length/1.
- InsertOptimizer.calculate_insert_options and throughput helpers split
  into guarded clauses instead of inline `if`.
- maybe_emit_optimization uses same-var pattern matching to skip the
  telemetry hit when nothing changed.

Adds unit tests covering every branch of both modules plus SignalHandler,
LocaleHook, and the InsertOptimizer's :noproc fallback path. Coverage
64.69% → 65.72%.
2026-04-23 13:38:24 -05:00

87 lines
2.6 KiB
Elixir

defmodule AprsmeWeb.LocaleHookTest do
# on_mount/4 mutates the process-wide Gettext locale and reads app env;
# serialize with anything else that does the same.
use ExUnit.Case, async: false
alias AprsmeWeb.LocaleHook
alias Phoenix.LiveView.Socket
setup do
original_env = Application.get_env(:aprsme, :env)
original_locale = Gettext.get_locale(AprsmeWeb.Gettext)
on_exit(fn ->
Application.put_env(:aprsme, :env, original_env)
Gettext.put_locale(AprsmeWeb.Gettext, original_locale)
end)
:ok
end
defp bare_socket do
%Socket{
assigns: %{__changed__: %{}},
private: %{}
}
end
describe "on_mount/4 in production" do
setup do
Application.put_env(:aprsme, :env, :prod)
:ok
end
test "sets a supported locale from session" do
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{"locale" => "es"}, bare_socket())
assert socket.assigns.locale == "es"
assert Gettext.get_locale(AprsmeWeb.Gettext) == "es"
end
test "falls back to 'en' for unsupported session locale" do
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{"locale" => "zz"}, bare_socket())
assert socket.assigns.locale == "en"
end
test "falls back to 'en' when session has no locale" do
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{}, bare_socket())
assert socket.assigns.locale == "en"
end
test "accepts all declared supported locales" do
for loc <- ["en", "es", "de", "fr"] do
{:cont, socket} =
LocaleHook.on_mount(:set_locale, %{}, %{"locale" => loc}, bare_socket())
assert socket.assigns.locale == loc
end
end
end
describe "on_mount/4 in non-prod" do
test "leaves locale assign as nil" do
Application.put_env(:aprsme, :env, :dev)
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{"locale" => "es"}, bare_socket())
assert socket.assigns.locale == nil
end
end
describe "on_mount/4 map_page assign" do
test "map_page is false for non-MapLive socket" do
Application.put_env(:aprsme, :env, :dev)
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{}, bare_socket())
assert socket.assigns.map_page == false
end
test "map_page is true when the socket's view is MapLive.Index" do
Application.put_env(:aprsme, :env, :dev)
socket = %Socket{
assigns: %{__changed__: %{}},
private: %{phoenix_live_view: %{view: AprsmeWeb.MapLive.Index}}
}
{:cont, socket} = LocaleHook.on_mount(:set_locale, %{}, %{}, socket)
assert socket.assigns.map_page == true
end
end
end