require "rails_helper"

describe AttachContractToReleasableJob do
  describe ".perform_now" do
    it "generates contract PDF" do
      release = build_stubbed(:appearance_release)
      contract = mock_contract_new
      pdf = mock_contract_to_pdf(contract)
      allow(release.contract).to receive(:attach)

      AttachContractToReleasableJob.perform_now release

      expect(Contract).to have_received(:new).with(release)
    end

    it "attaches it to a releaseable record" do
      release = build_stubbed(:appearance_release)
      contract = mock_contract_new(filename: "contract.pdf")
      pdf = mock_contract_to_pdf(contract)
      allow(release.contract).to receive(:attach)

      AttachContractToReleasableJob.perform_now release

      expect(release.contract).to(
        have_received(:attach).
        with({
          io: pdf,
          filename: "contract.pdf",
          content_type: "application/pdf",
        })
      )
    end
  end

  private

  def mock_contract_new(filename: "")
    double("contract", filename: filename).tap do |contract|
      allow(Contract).to receive(:new).and_return(contract)
    end
  end

  def mock_contract_to_pdf(contract)
    double("pdf").tap do |pdf|
      allow(contract).to receive(:to_pdf).and_return(pdf)
    end
  end
end
