require 'spec_helper'
require 'flydata/helper/config_parser'

module Flydata
  module Helper
    describe Config do
      context ".create" do
        subject { Config.create(config_hash) }
        context "initialized with empty hash" do
          let(:config_hash) { Hash.new }
          it "should return a config object and respond to dynamically added methods" do
            is_expected.not_to be_nil
            is_expected.to respond_to(:helper_home)
          end
          it "should return default values correctly" do
            expect(subject.helper_retry_limit).to eq(15)
          end
        end
        context "initialized with non-empty hash" do
          let(:override_helper_home) { "/home/user/.flydata/helper" }
          let(:config_hash) { {helper_home: override_helper_home} }
          it "should return correct value of a property" do
            expect(subject.helper_home).to eq(override_helper_home)
          end
        end
      end

      context '#scheduled_actions' do
        subject { Config.default }
        it "should return the scheduled actions correctly" do
          expect(subject.scheduled_actions).to eq({
            check_remote_actions:
              {
                check_interval: 30,
                name: :check_remote_actions
              }
          })
        end
        it "should return a copy everytime" do
          helper_config = subject
          src = helper_config[:scheduled_actions]
          copy = helper_config.scheduled_actions
          expect(src.object_id).not_to eq(copy.object_id)
          expect(src[:check_remote_actions].object_id).
            not_to eq(copy[:check_remote_actions].object_id)
        end
      end
    end

    describe ConfigParser do
      context ".parse" do
        subject { described_class.parse(conf_content)}
        context "when the parsed content is empty" do
          let(:conf_content) { "" }
          it "should raise an error" do
            expect { subject }.to raise_error
          end
        end
        context "when the parsed content does not contain scheduled action" do
          let(:conf_content) { "workers: 5" }
          it "should return the default scheduled action" do
            is_expected.to eq({
              workers: 5,
              helper: {
                scheduled_actions: {
                  check_remote_actions: {
                    check_interval: '30s'
                  }
                }
              }
            })
          end
        end
        context "when content includes scheduled action" do
          let(:conf_content) { <<EOT
workers: 2
helper:
  scheduled_actions:
    check_remote_actions:
      check_interval: 1m
EOT
          }
          it "should parse to a Config object" do
            is_expected.to eq({
              workers: 2,
              helper: {
                scheduled_actions: {
                  check_remote_actions: {
                    check_interval: '1m'
                  }
                }
              }
            })
          end
        end
      end
    end
  end
end