require 'spec_helper'

describe IcecastRelay do

  let(:relay) { IcecastRelay.new }
  let(:output){ StringIO.new }
  let(:builder)  { ::Builder::XmlMarkup.new(:target => output, :indent => 1) }


  it  "should not save" do
    relay.save.should be_false
    relay.errors[:mount].should == ["can't be blank"]
    relay.errors[:server].should == ["can't be blank"]
  end

  it "save" do
    relay.mount = Faker::Lorem.characters(10)
    relay.server = Faker::Lorem.characters(10)
    relay.relay_shoutcast_metadata = false
    relay.save!

    relay.dumpXml(builder)

    output.rewind
    xml = Nokogiri::XML(output)
    xml.css('relay port').text.should == relay.port.to_s
    xml.css('relay mount').text.should == relay.mount
    xml.css('relay server').text.should == relay.server
    xml.css('relay local-mount').length.should == 0
    xml.css('relay username').length.should == 0
    xml.css('relay password').length.should == 0
    xml.css('relay relay-shoutcast-metadata').text.should == "0"
    xml.css('relay on-demand').text.should == "1"
  end

  describe "poke configs" do
    let(:server) { a = FactoryGirl.create(:icecast_server_with_overrides); a.config_updated; IcecastServer.find(a.id) }

    before(:each) do
      server.relays << FactoryGirl.create(:icecast_relay)
      server.save!
      server.config_updated
      server.reload
    end

    it "success via server" do
      server.relays.first.save!
      server.reload
      server.config_changed.should == 1
    end

    it "delete via server" do
      server.relays.first.destroy
      server.reload
      server.config_changed.should == 1
    end
  end
end