- Add contacts_inserted_at_desc_id_desc_idx so /contacts list runs as an Index Scan (~0.4ms) instead of a Seq Scan + top-N heapsort (~77ms on 58k rows) - Add Microwaveprop.Cache: tiny ETS-backed TTL cache for memoizing expensive-but-stale-tolerant values - Cache total_entries for unsearched /contacts page loads (30s TTL, invalidated on insert) - Cache the entire /contacts/map payload (pre-encoded JSON + band list), moving load_contacts into Radio.contact_map_payload; invalidated on new contact insert. Mount goes from ~150-300ms to ~5ms. - Fix /map overlay blinking on load: reuse the Leaflet GridLayer across renderScores calls and just redraw() against the updated ScoreGrid, instead of removeLayer + new layer which flashed between tile regens - Make mode optional on submission: schema change (null: true), submission_changeset no longer requires it, blank strings normalise to nil, CSV import accepts 6-column (no mode) or 7-column layouts - core_components .input adds a red asterisk to labels when required, giving users a visual cue for which fields must be filled on /submit
140 lines
4.8 KiB
Elixir
140 lines
4.8 KiB
Elixir
defmodule Microwaveprop.Radio.ContactSubmissionTest do
|
|
use Microwaveprop.DataCase, async: true
|
|
|
|
alias Microwaveprop.Radio.Contact
|
|
|
|
@valid_attrs %{
|
|
station1: "W5XD",
|
|
station2: "K5TR",
|
|
qso_timestamp: ~U[2026-03-28 18:00:00Z],
|
|
mode: "CW",
|
|
band: "1296",
|
|
grid1: "EM12",
|
|
grid2: "EM00",
|
|
submitter_email: "test@example.com"
|
|
}
|
|
|
|
describe "submission_changeset/2" do
|
|
test "valid attrs produce a valid changeset" do
|
|
changeset = Contact.submission_changeset(%Contact{}, @valid_attrs)
|
|
assert changeset.valid?
|
|
end
|
|
|
|
test "requires station1, station2, qso_timestamp, band, grid1, grid2, submitter_email" do
|
|
changeset = Contact.submission_changeset(%Contact{}, %{})
|
|
errors = errors_on(changeset)
|
|
|
|
assert errors[:station1]
|
|
assert errors[:station2]
|
|
assert errors[:qso_timestamp]
|
|
assert errors[:band]
|
|
assert errors[:grid1]
|
|
assert errors[:grid2]
|
|
assert errors[:submitter_email]
|
|
|
|
# Mode is optional on submission
|
|
refute errors[:mode]
|
|
end
|
|
|
|
test "accepts a blank mode and normalises it to nil" do
|
|
for blank <- ["", " ", nil] do
|
|
attrs = Map.put(@valid_attrs, :mode, blank)
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert changeset.valid?, "Expected blank mode #{inspect(blank)} to validate"
|
|
assert Ecto.Changeset.get_field(changeset, :mode) == nil
|
|
end
|
|
end
|
|
|
|
test "rejects invalid grid1" do
|
|
attrs = Map.put(@valid_attrs, :grid1, "ZZ99")
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert "is not a valid Maidenhead grid square" in errors_on(changeset).grid1
|
|
end
|
|
|
|
test "rejects invalid grid2" do
|
|
attrs = Map.put(@valid_attrs, :grid2, "ZZ99")
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert "is not a valid Maidenhead grid square" in errors_on(changeset).grid2
|
|
end
|
|
|
|
test "accepts valid 6-char grids" do
|
|
attrs = %{@valid_attrs | grid1: "EM12ab", grid2: "EM00cd"}
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert changeset.valid?
|
|
end
|
|
|
|
test "rejects invalid mode" do
|
|
attrs = Map.put(@valid_attrs, :mode, "RTTY")
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert errors_on(changeset).mode
|
|
end
|
|
|
|
test "accepts valid modes" do
|
|
for mode <- ~w(CW SSB FM FT8 FT4 Q65) do
|
|
attrs = Map.put(@valid_attrs, :mode, mode)
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert changeset.valid?, "Expected mode #{mode} to be valid"
|
|
end
|
|
end
|
|
|
|
test "rejects invalid band" do
|
|
attrs = Map.put(@valid_attrs, :band, "144")
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert errors_on(changeset).band
|
|
end
|
|
|
|
test "accepts valid microwave bands" do
|
|
for band <- ~w(1296 2304 3456 5760 10000 24000 47000 75000) do
|
|
attrs = Map.put(@valid_attrs, :band, band)
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert changeset.valid?, "Expected band #{band} to be valid"
|
|
end
|
|
end
|
|
|
|
test "rejects email without @" do
|
|
attrs = Map.put(@valid_attrs, :submitter_email, "notanemail")
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
assert errors_on(changeset).submitter_email
|
|
end
|
|
|
|
test "does not cast user_submitted" do
|
|
attrs = Map.put(@valid_attrs, :user_submitted, true)
|
|
changeset = Contact.submission_changeset(%Contact{}, attrs)
|
|
refute Ecto.Changeset.get_change(changeset, :user_submitted)
|
|
end
|
|
end
|
|
|
|
describe "create_contact/1 deduplication" do
|
|
alias Microwaveprop.Radio
|
|
|
|
test "rejects duplicate and returns the existing contact" do
|
|
assert {:ok, original} = Radio.create_contact(@valid_attrs)
|
|
|
|
assert {:error, :duplicate, ^original} = Radio.create_contact(@valid_attrs)
|
|
end
|
|
|
|
test "allows same stations on a different band" do
|
|
assert {:ok, _} = Radio.create_contact(@valid_attrs)
|
|
assert {:ok, _} = Radio.create_contact(%{@valid_attrs | band: "2304"})
|
|
end
|
|
|
|
test "allows same stations with different grids" do
|
|
assert {:ok, _} = Radio.create_contact(@valid_attrs)
|
|
assert {:ok, _} = Radio.create_contact(%{@valid_attrs | grid1: "EM13"})
|
|
end
|
|
|
|
test "allows contact outside the 1-hour window" do
|
|
assert {:ok, _} = Radio.create_contact(@valid_attrs)
|
|
|
|
later = DateTime.add(@valid_attrs.qso_timestamp, 3601, :second)
|
|
assert {:ok, _} = Radio.create_contact(%{@valid_attrs | qso_timestamp: later})
|
|
end
|
|
|
|
test "detects duplicates regardless of station order" do
|
|
assert {:ok, original} = Radio.create_contact(@valid_attrs)
|
|
|
|
swapped = %{@valid_attrs | station1: "K5TR", grid1: "EM00", station2: "W5XD", grid2: "EM12"}
|
|
assert {:error, :duplicate, ^original} = Radio.create_contact(swapped)
|
|
end
|
|
end
|
|
end
|