hue_spec.rb (2974B)
1 # frozen_string_literal: true 2 3 require 'spec_helper' 4 5 describe Hue do 6 before do 7 mock_application_config_path 8 mock_bridge_config_path 9 mock_udp_replies 10 end 11 12 after do 13 create_test_bridge_config 14 end 15 16 it 'reports the device type as itself' do 17 expect(described_class.device_type).to eq(described_class::DEVICE_TYPE) 18 end 19 20 it 'returns the default application' do 21 expect(described_class.application).to be_a(described_class::Bridge) 22 end 23 24 context 'when discovering a bridge on the network' do 25 it 'returns a list discovered bridges' do 26 bridges = described_class.discover 27 expect(bridges).to eq({ TEST_UDP_BRIDGE_UUID => TEST_UDP_BRIDGE_URI }) 28 end 29 30 it 'allows registering of discovered bridges' do 31 expect(Hue::Config::Bridge.find(TEST_UDP_BRIDGE_UUID)).to be_nil 32 registered = described_class.register_bridges 33 new_bridge = registered[TEST_UDP_BRIDGE_UUID] 34 expect(new_bridge.id).to eq(TEST_UDP_BRIDGE_UUID) 35 expect(new_bridge.uri).to eq(TEST_UDP_BRIDGE_URI) 36 end 37 end 38 39 context 'when attempting discover without a bridge on the network' do 40 before do 41 mock_udp_no_reply 42 mock_nupnp_empty_reply 43 end 44 45 it 'returns an empty list of bridges' do 46 bridges = described_class.discover 47 expect(bridges).to eq({}) 48 end 49 end 50 51 context 'after discovering bridges' do 52 before do 53 mock_udp_replies(TEST_BRIDGE_UUID, 'new_host') 54 end 55 56 it 'updates already registered bridges' do 57 bridge = Hue::Config::Bridge.find(TEST_BRIDGE_UUID) 58 expect(bridge).not_to be_nil 59 60 registered = described_class.register_bridges 61 62 updated_bridge = registered[TEST_BRIDGE_UUID] 63 expect(updated_bridge.id).to eq(TEST_BRIDGE_UUID) 64 expect(updated_bridge.uri).not_to eq(bridge.uri) 65 end 66 end 67 68 context 'when registering or un-registering the application' do 69 it 'throws and error if the default already exists' do 70 expect do 71 described_class.register_default 72 end.to raise_error(described_class::Error, described_class::ERROR_DEFAULT_EXISTS) 73 end 74 75 it "allows a new default if one doesn't exist" do 76 with_temp_config_path do 77 with_fake_post(nil, {}, 'post_success', TEST_UDP_BRIDGE_URI) 78 with_stdout(/Registering new app...$/) do 79 described_class.register_default 80 end 81 end 82 end 83 84 it 'allows un-registering the default' do 85 with_temp_config_path(true) do 86 with_fake_delete("config/whitelist/#{TEST_APPLICATION_UUID}") 87 described_class.remove_default 88 end 89 end 90 end 91 92 context 'contains some utility methods' do 93 it '#percent_to_unit_interval should convert a string with percent to a unit interval' do 94 expect(described_class.percent_to_unit_interval('1%')).to eq(0.01) 95 expect(described_class.percent_to_unit_interval('10%')).to eq(0.1) 96 expect(described_class.percent_to_unit_interval('100%')).to eq(1.00) 97 end 98 end 99 end