require 'samepage/zook/config_parser'

describe Samepage::Zook::ConfigParser do
	let(:fixtures) { File.join(%w[spec fixtures zook]) }
	let(:conffile) { File.join(fixtures, filename) }

	before do
		ENV['ZOOK_URI'] = uri
	end

	subject(:config)   { described_class.parse conffile, 'ZOOK_URI' }

	describe '.parse' do
		context 'when ENV is present' do
			let(:filename) { 'absent.cnf' }
			let(:uri)      { 'zook://192.0.2.5/env' }

			it 'returns config from ENV' do
				expect( config.prefix ).to eq '/env'
				expect( config.server ).to match(/192.0.2.5:2181/)
			end
		end

		context 'when file is present' do
			let(:filename) { 'single_server.cnf' }
			let(:uri)      { nil }

			it 'returns config from file' do
				expect( config.prefix ).to eq '/kerio'
				expect( config.server ).to match(/192.0.2.1:2181/)
			end
		end

		context 'when both file and ENV is present' do
			let(:filename) { 'single_server.cnf' }
			let(:uri)      { 'zook://192.0.2.5/env' }

			it 'returns config from ENV' do
				expect( config.prefix ).to eq '/env'
				expect( config.server ).to match(/192.0.2.5:2181/)
			end
		end

		context 'when no config is present' do
			let(:filename) { 'absent.cnf' }
			let(:uri)      { nil }

			it 'returns default server' do
				expect( config.prefix ).to eq '/kerio'
				expect( config.server ).to match(/127.0.0.1:2181/)
			end
		end
	end
end

