defmodule Microwaveprop.Workers.PskrCalibrationWorkerTest do use Microwaveprop.DataCase, async: true use Oban.Testing, repo: Microwaveprop.Repo alias Microwaveprop.Pskr.CalibrationSample alias Microwaveprop.Pskr.SpotHourly alias Microwaveprop.Repo alias Microwaveprop.Workers.PskrCalibrationWorker defp insert_spot!(hour_utc) do {:ok, _} = %SpotHourly{} |> SpotHourly.changeset(%{ hour_utc: hour_utc, band: "10000", sender_grid: "EM12KL", receiver_grid: "DM43ST", spot_count: 1, midpoint_lat: 33.0, midpoint_lon: -97.0, distance_km: 200.0, modes: ["FT8"] }) |> Repo.insert() end test "perform/1 with explicit hour_utc samples that hour" do hour = ~U[2026-05-04 18:00:00Z] insert_spot!(hour) assert :ok = perform_job(PskrCalibrationWorker, %{"hour_utc" => DateTime.to_iso8601(hour)}) [sample] = Repo.all(CalibrationSample) assert sample.hour_utc == hour end test "perform/1 with no args defaults to the previous full hour" do # Pick the hour just before "now" — the same logic the worker # uses internally — so the spot we insert lines up with what the # default arg path will pull. target = DateTime.utc_now() |> DateTime.add(-3600, :second) |> Map.put(:minute, 0) |> Map.put(:second, 0) |> Map.put(:microsecond, {0, 0}) insert_spot!(target) assert :ok = perform_job(PskrCalibrationWorker, %{}) assert Repo.aggregate(CalibrationSample, :count) == 1 end test "perform/1 returns :ok even when no spots exist for the hour" do assert :ok = perform_job(PskrCalibrationWorker, %{ "hour_utc" => "2026-05-04T03:00:00Z" }) assert Repo.aggregate(CalibrationSample, :count) == 0 end end