require "rails_helper"

describe AddHeadshotCollectionUidToProjectJob do
  describe ".perform_now" do
    it "calls API with headshot collection data" do
      releases = build_stubbed_list(:appearance_release, 2)
      project = build_stubbed(:project, appearance_releases: releases)
      stub_collection_create_api_call(collection_uid: "123abc")
      mock_project_update(project)
      collection = double("collection")
      allow(HeadshotCollection).to receive(:new).and_return(collection)

      AddHeadshotCollectionUidToProjectJob.perform_now(project)

      expect(BrayniacAI::Collection).to(
        have_received(:create!).
        with(collection)
      )
    end

    it "saves the ID of the created collection" do
      project = build_stubbed(:project)
      stub_collection_create_api_call(collection_uid: "123abc")
      mock_project_update(project)

      AddHeadshotCollectionUidToProjectJob.perform_now(project)

      expect(project).to(
        have_received(:update_attribute).
        with(:headshot_collection_uid, "123abc")
      )
    end
  end

  private

  def stub_collection_create_api_call(collection_uid:)
    api_response = double("response", collection_id: collection_uid)
    allow(BrayniacAI::Collection).to receive(:create!).and_return(api_response)
  end

  def mock_project_update(project)
    allow(project).to receive(:update_attribute)
  end
end
