require 'spec_helper'
require 'stringio'

=begin
example output:

  <directory>
    <yp-url-timeout>15</yp-url-timeout>
    <yp-url>http://dir.xiph.org/cgi-bin/yp-cgi</yp-url>
  </directory>
=end
describe IcecastDirectory do

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

  before(:each) do

  end

  it "save error" do
    dir.save.should be_false
    dir.errors[:yp_url].length.should == 1
    dir.errors[:yp_url_timeout].length.should == 0
  end
  
  it "save" do
    dir.yp_url = Faker::Lorem.characters(10)
    dir.yp_url_timeout = 20
    dir.save.should be_true
    dir.dumpXml(builder)

    output.rewind
    xml = Nokogiri::XML(output)
    xml.css('directory yp-url-timeout').text.should == dir.yp_url_timeout.to_s
    xml.css('directory yp-url-timeout').length.should == 1
    xml.css('directory yp-url').text.should == dir.yp_url
    xml.css('directory yp-url').length.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.directory = FactoryGirl.create(:icecast_directory)
      server.template.directory = FactoryGirl.create(:icecast_directory)
      server.template.save!
      server.save!
      server.config_updated
      server.reload
    end

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

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

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

    it "destroy via server" do
      server.directory.destroy
      server.reload
      server.config_changed.should == 1
    end
  end

end
