require 'spec_helper'

describe Jamblaster do

  let(:jamblaster) { FactoryGirl.create(:jamblaster) }
  let(:user) { FactoryGirl.create(:user) }

  it "can be created" do
    FactoryGirl.create(:jamblaster)
  end

  it "can associate to users" do
    jamblaster.users.should eq([])
    user.jamblasters.should eq([])
  end

  describe "bootstrap" do

    it "creates new" do
      jb = Jamblaster.bootstrap('abcd')
      jb.should_not be_nil
      jb.errors.any?.should be_false

      found = Jamblaster.find_by_serial_no('abcd')

      found.should_not be_nil
      jb.id.should eql found.id
    end

    it "reuses existing" do
      jb = Jamblaster.bootstrap('abcd')
      jb.should_not be_nil

      found = Jamblaster.bootstrap('abcd')
      found.should_not be_nil

      jb.id.should eql found.id
    end

  end

  describe "most_recent_pairings" do
    it "basic funnction" do

      jamblaster.most_recent_pairing.should be nil

      pairing1 = FactoryGirl.create(:jamblaster_pairing_request, user: user, jamblaster: jamblaster, vtoken: 'token2', sibling_key: nil)

      jamblaster.most_recent_pairing.should be nil

      pairing1.activate('abc')

      jamblaster.most_recent_pairing.should eql pairing1

      pairing2 = FactoryGirl.create(:jamblaster_pairing_request, user: user, jamblaster: jamblaster, sibling_key: 'key1', vtoken: 'token1')

      jamblaster.most_recent_pairing.should eql pairing1

      pairing2.activate('key2')

      jamblaster.most_recent_pairing.should eql pairing2
    end
  end
end