defmodule Berrypod.Sync.ScheduledSyncWorkerTest do
  use Berrypod.DataCase, async: false
  use Oban.Testing, repo: Berrypod.Repo

  alias Berrypod.Sync.ScheduledSyncWorker
  alias Berrypod.Sync.ProductSyncWorker

  import Berrypod.ProductsFixtures

  describe "perform/1" do
    test "enqueues sync for enabled connections" do
      conn = provider_connection_fixture(%{enabled: true})

      Oban.Testing.with_testing_mode(:manual, fn ->
        assert :ok = perform_job(ScheduledSyncWorker, %{})

        assert_enqueued(
          worker: ProductSyncWorker,
          args: %{provider_connection_id: conn.id}
        )
      end)
    end

    test "skips disabled connections" do
      _conn = provider_connection_fixture(%{enabled: false})

      Oban.Testing.with_testing_mode(:manual, fn ->
        assert :ok = perform_job(ScheduledSyncWorker, %{})
        refute_enqueued(worker: ProductSyncWorker)
      end)
    end

    test "handles no connections gracefully" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        assert :ok = perform_job(ScheduledSyncWorker, %{})
        refute_enqueued(worker: ProductSyncWorker)
      end)
    end
  end
end
