defmodule Ammoprices.Scraping.RunnerTest do use Ammoprices.DataCase, async: true import Ammoprices.Fixtures alias Ammoprices.Catalog alias Ammoprices.Scraping.Retailers.LuckyGunner alias Ammoprices.Scraping.Runner @fixture_html File.read!("test/fixtures/lucky_gunner/9mm.html") setup do retailer = retailer_fixture(%{name: "Lucky Gunner", slug: "lucky-gunner", base_url: "https://www.luckygunner.com"}) caliber = caliber_fixture(%{name: "9mm Luger", slug: "9mm-luger", category: "handgun", aliases: ["9mm", "9x19"]}) Req.Test.stub(Ammoprices.Scraping.HttpClient, fn conn -> Req.Test.html(conn, @fixture_html) end) %{retailer: retailer, caliber: caliber} end describe "run/2" do test "creates products and snapshots from scrape", %{caliber: caliber} do scraper = LuckyGunner assert {:ok, stats} = Runner.run(scraper, caliber) assert stats.products_count == 4 assert stats.snapshots_count == 4 products = Catalog.list_products_for_caliber(caliber.id) assert length(products) == 4 end test "updates retailer last_scraped_at", %{caliber: caliber} do scraper = LuckyGunner {:ok, _stats} = Runner.run(scraper, caliber) retailer = Catalog.get_retailer_by_slug!("lucky-gunner") assert retailer.last_scraped_at end test "handles scraper returning nil category_url", %{caliber: _caliber} do caliber = caliber_fixture(%{name: "Unknown", slug: "unknown", category: "handgun"}) scraper = LuckyGunner assert {:ok, stats} = Runner.run(scraper, caliber) assert stats.products_count == 0 end end end