require 'spec_helper'

describe IcecastListenSocket do

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

  it "save" do
    listen.save!
    listen.dumpXml(builder)

    output.rewind
    xml = Nokogiri::XML(output)
    xml.css('listen-socket port').text.should == "8001"
    xml.css('listen-socket shoutcast-compat').length.should == 0
    xml.css('listen-socket shoutcast-mount').length.should == 0
    xml.css('listen-socket bind-address').length.should == 0
  end

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

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

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

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

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