ammocpr/test/ammoprices_web/live/home_live_test.exs
Graham McIntire 872c94bea1
Fix bugs and performance: Oban chain, 0-cent prices, duplicate queries, regex fragility
- Fix Oban self-scheduling chain silently dying (remove unique constraint so after block can schedule next job)
- Fix Natchez and TrueShotAmmo extract_price defaulting to 0 for missing price data
- Fix caliber_matcher redundant String.downcase inside Enum.find (pre-downcase outside loop)
- Fix runner.ex double iteration (Enum.map + Enum.count merged into single Enum.reduce)
- Fix price_stats_for_caliber firing 3 queries instead of 2 (merge all_time + thirty_day via FILTER)
- Fix PSA duplicate Floki.find on same selector (merge extract_title + extract_url)
- Fix PubSub broadcast carrying no caliber IDs (now includes IDs; clients skip irrelevant reloads)
- Fix BulkAmmo ^ anchor skipping non-leading round counts
- Fix TargetSportsUSA fragile exact text match 'Add To Cart' (use case-insensitive contains)
- Fix SgAmmo dead destructure {_, _, _} = row
- Fix text_detector brass/nickel detection order
- Fix Natchez GraphQL string interpolation (add escape_gql_string)
- Fix chart data triple Enum.map merged into single reduce
- Change Oban scraping queue workers from 2 to 1 (only one needed with Process.sleep)
2026-06-21 18:14:49 -05:00

51 lines
1.9 KiB
Elixir

defmodule AmmopricesWeb.HomeLiveTest do
use AmmopricesWeb.ConnCase, async: true
import Ammoprices.Fixtures
import Phoenix.LiveViewTest
describe "HomeLive" do
test "renders category sections", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")
assert has_element?(view, "#category-handgun")
assert has_element?(view, "#category-rifle")
assert has_element?(view, "#category-rimfire")
assert has_element?(view, "#category-shotgun")
end
test "renders caliber links", %{conn: conn} do
caliber_fixture(%{name: "9mm Luger", slug: "9mm-luger", category: "handgun"})
{:ok, view, _html} = live(conn, "/")
assert has_element?(view, "#caliber-9mm-luger")
end
test "shows cheapest price when snapshots exist", %{conn: conn} do
caliber = caliber_fixture(%{name: "9mm Luger", slug: "9mm-luger", category: "handgun"})
retailer = retailer_fixture()
product = product_fixture(%{caliber: caliber, retailer: retailer, in_stock: true})
now = DateTime.truncate(DateTime.utc_now(), :second)
snapshot_fixture(%{product: product, price_per_round_cents: 25, in_stock: true, recorded_at: now})
{:ok, _view, html} = live(conn, "/")
assert html =~ "25"
end
test "updates cheapest prices via PubSub broadcast", %{conn: conn} do
caliber = caliber_fixture(%{name: "9mm Luger", slug: "9mm-luger", category: "handgun"})
retailer = retailer_fixture()
product = product_fixture(%{caliber: caliber, retailer: retailer, in_stock: true})
now = DateTime.truncate(DateTime.utc_now(), :second)
{:ok, view, _html} = live(conn, "/")
snapshot_fixture(%{product: product, price_per_round_cents: 17, in_stock: true, recorded_at: now})
Phoenix.PubSub.broadcast(Ammoprices.PubSub, "prices:updated", {:prices_updated, [caliber.id]})
assert render(view) =~ "0.17"
end
end
end