require "rails_helper"

RSpec.describe SetTagsForReleasableJob do
  context "for releasables with a single photo" do
    it "detects tags for the photo and adds them to the release" do
      releasable = build(:appearance_release)
      tags = %w(Female Adult)
      stub_brayniac_ai_api_tag_create(releasable.photo, tags)

      SetTagsForReleasableJob.perform_now(releasable)

      expect(releasable.internal_tag_list).to eq(tags)
    end
  end

  context "for releasables with multiple photos" do
    it "detects tags for each photo and adds them all to the release" do
      releasable = build(:location_release_with_photos)
      stub_brayniac_ai_api_tag_create(releasable.photos.first, %w(Restaurant Fireplace))
      stub_brayniac_ai_api_tag_create(releasable.photos.last, %w(Bar Fireplace))

      SetTagsForReleasableJob.perform_now(releasable)

      expect(releasable.internal_tag_list).to eq(%w(Restaurant Fireplace Bar))
    end
  end

  private

  def stub_brayniac_ai_api_tag_create(photo, tags)
    allow(BrayniacAI::Tag).to \
      receive(:create).
      with(bucket_name: ENV['AWS_BUCKET'], object_name: photo.key).
      and_return(tags)
  end
end
