# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::IngredientAudioSerializer do
  let(:element) { FactoryBot.build_stubbed(:alchemy_element) }
  let(:attachment) { FactoryBot.build_stubbed(:alchemy_attachment) }
  let(:ingredient) do
    Alchemy::Ingredients::Audio.new(
      role: "audio",
      element: element,
      attachment: attachment,
      autoplay: false,
      controls: true,
      muted: true,
    )
  end

  subject(:serializer) { described_class.new(ingredient) }

  it_behaves_like "an ingredient serializer"

  describe "attributes" do
    subject { serializer.serializable_hash[:data][:attributes] }

    it "has the right keys and values", :aggregate_failures do
      expect(subject[:autoplay]).to eq(false)
      expect(subject[:controls]).to eq(true)
      expect(subject[:muted]).to eq(true)
      expect(subject[:leep]).to eq(nil)
      expect(subject[:value]).to match(/\/attachment\/#{attachment.id}\/show/o)
      expect(subject[:audio_name]).to eq("image")
      expect(subject[:audio_file_name]).to eq("image.png")
      expect(subject[:audio_mime_type]).to eq("image/png")
      expect(subject[:audio_file_size]).to eq(70)
    end
  end

  context "With no audio set" do
    let(:attachment) { nil }

    describe "attributes" do
      subject { serializer.serializable_hash[:data][:attributes] }

      it "has the right keys and values" do
        expect(subject[:ingredient]).to be nil
      end
    end
  end
end
