defmodule Microwaveprop.Workers.PropagationPruneWorkerTest do
  use Microwaveprop.DataCase, async: false

  alias Microwaveprop.Propagation.ScoresFile
  alias Microwaveprop.Workers.PropagationPruneWorker

  describe "perform/1" do
    test "deletes ScoresFile binaries with valid_time older than 2 hours" do
      now = DateTime.utc_now()
      old = now |> DateTime.add(-3, :hour) |> DateTime.truncate(:second)
      fresh = now |> DateTime.add(-30, :minute) |> DateTime.truncate(:second)
      future = now |> DateTime.add(6, :hour) |> DateTime.truncate(:second)

      ScoresFile.write!(10_000, old, [%{lat: 25.0, lon: -125.0, score: 10}])
      ScoresFile.write!(10_000, fresh, [%{lat: 25.0, lon: -125.0, score: 20}])
      ScoresFile.write!(10_000, future, [%{lat: 25.0, lon: -125.0, score: 30}])

      assert :ok = PropagationPruneWorker.perform(%Oban.Job{})

      assert {:error, :enoent} = ScoresFile.read(10_000, old)
      assert {:ok, _} = ScoresFile.read(10_000, fresh)
      assert {:ok, _} = ScoresFile.read(10_000, future)
    end

    test "is a no-op when nothing is stale" do
      now = DateTime.utc_now()
      fresh = now |> DateTime.add(-30, :minute) |> DateTime.truncate(:second)
      ScoresFile.write!(10_000, fresh, [%{lat: 25.0, lon: -125.0, score: 42}])

      assert :ok = PropagationPruneWorker.perform(%Oban.Job{})
      assert {:ok, _} = ScoresFile.read(10_000, fresh)
    end
  end
end
