require 'spec_helper'

module VCAP::CloudController
  RSpec.describe LifecycleProvider do
    let(:package) { PackageModel.make }
    let(:message) { BuildCreateMessage.new(request) }

    context 'when lifecycle type is requested on the message' do
      let(:request) { { lifecycle: { type: type, data: {} } } }

      context 'docker type' do
        let(:type) { 'docker' }

        it 'returns a DockerLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(DockerLifecycle)
        end
      end

      context 'buildpack type' do
        let(:type) { 'buildpack' }

        it 'returns a BuildpackLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(BuildpackLifecycle)
        end
      end

      context 'cnb type' do
        let(:type) { 'cnb' }

        it 'returns a BuildpackLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(CNBLifecycle)
        end
      end
    end

    context 'when lifecycle type is not requested on the message' do
      let(:request) { {} }
      let(:package) { PackageModel.make(app_guid: app.guid) }

      context 'when the app defaults to buildpack' do
        let(:app) { AppModel.make(:buildpack) }

        it 'returns a BuildpackLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(BuildpackLifecycle)
        end
      end

      context 'when the app defaults to docker' do
        let(:app) { AppModel.make(:docker) }

        it 'returns a DockerLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(DockerLifecycle)
        end
      end

      context 'when the app defaults to cnb' do
        let(:app) { AppModel.make(:cnb) }

        it 'returns a CNBLifecycle' do
          expect(LifecycleProvider.provide(package, message)).to be_a(CNBLifecycle)
        end
      end
    end
  end
end
