require 'spec_helper'

RSpec.describe BoshErrandEnvironment do
  subject(:bosh_errand_environment) { BoshErrandEnvironment.new(config) }

  let(:stdout_sink_enabled) { true }
  let(:config) do
    VCAP::CloudController::Config.new(
      {
        db: DbConfig.new.config,
        logging: { level: 'fatal', stdout_sink_enabled: stdout_sink_enabled }
      },
      context: :rotate_database_key
    )
  end

  describe '#initialize' do
    it 'configures steno logger with stdout sink' do
      bosh_errand_environment.setup_environment
      expect(Steno.logger('cc.errand').instance_variable_get(:@sinks).length).to eq(2)
    end

    context 'when `stdout_sink_enabled` is `false`' do
      let(:stdout_sink_enabled) { false }

      it 'configures steno logger wo stdout sink' do
        bosh_errand_environment.setup_environment
        expect(Steno.logger('cc.errand').instance_variable_get(:@sinks).length).to eq(1)
      end
    end
  end

  describe '#setup_environment' do
    before do
      allow(VCAP::CloudController::DB).to receive(:load_models)
    end

    it 'loads models' do
      expect(VCAP::CloudController::DB).to receive(:load_models)
      bosh_errand_environment.setup_environment
    end

    it 'configures components' do
      expect(config).to receive(:configure_components)
      bosh_errand_environment.setup_environment
    end
  end
end
