state_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. require 'api_client'
  2. RSpec.describe 'State' do
  3. before(:all) do
  4. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  5. @client.upload_json('/settings', 'settings.json')
  6. end
  7. before(:each) do
  8. @id_params = {
  9. id: @client.generate_id,
  10. type: 'rgb_cct',
  11. group_id: 1
  12. }
  13. end
  14. context 'toggle command' do
  15. it 'should toggle ON to OFF' do
  16. init_state = @client.patch_state({'status' => 'ON'}, @id_params)
  17. expect(init_state['status']).to eq('ON')
  18. next_state = @client.patch_state({'command' => 'toggle'}, @id_params)
  19. expect(next_state['status']).to eq('OFF')
  20. end
  21. it 'should toggle OFF to ON' do
  22. init_state = @client.patch_state({'status' => 'OFF'}, @id_params)
  23. expect(init_state['status']).to eq('OFF')
  24. next_state = @client.patch_state({'command' => 'toggle'}, @id_params)
  25. expect(next_state['status']).to eq('ON')
  26. end
  27. end
  28. context 'persistence' do
  29. it 'should persist parameters' do
  30. desired_state = {
  31. 'status' => 'ON',
  32. 'level' => 100,
  33. 'hue' => 0,
  34. 'saturation' => 100
  35. }
  36. @client.patch_state(desired_state, @id_params)
  37. patched_state = @client.get_state(@id_params)
  38. expect(patched_state.keys).to include(*desired_state.keys)
  39. expect(patched_state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  40. desired_state = {
  41. 'status' => 'ON',
  42. 'level' => 10,
  43. 'hue' => 49,
  44. 'saturation' => 20
  45. }
  46. @client.patch_state(desired_state, @id_params)
  47. patched_state = @client.get_state(@id_params)
  48. expect(patched_state.keys).to include(*desired_state.keys)
  49. expect(patched_state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  50. end
  51. it 'should affect member groups when changing group 0' do
  52. group_0_params = @id_params.merge(group_id: 0)
  53. desired_state = {
  54. 'status' => 'ON',
  55. 'level' => 100,
  56. 'hue' => 0,
  57. 'saturation' => 100
  58. }
  59. @client.patch_state(desired_state, group_0_params)
  60. individual_state = desired_state.merge('level' => 10)
  61. patched_state = @client.patch_state(individual_state, @id_params)
  62. expect(patched_state).to_not eq(desired_state)
  63. expect(patched_state.keys).to include(*individual_state.keys)
  64. expect(patched_state.select { |x| individual_state.include?(x) } ).to eq(individual_state)
  65. group_4_state = @client.get_state(group_0_params.merge(group_id: 4))
  66. expect(group_4_state.keys).to include(*desired_state.keys)
  67. expect(group_4_state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  68. @client.patch_state(desired_state, group_0_params)
  69. group_1_state = @client.get_state(group_0_params.merge(group_id: 1))
  70. expect(group_1_state.keys).to include(*desired_state.keys)
  71. expect(group_1_state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  72. end
  73. # it 'should keep group 0 state' do
  74. # group_0_params = @id_params.merge(group_id: 0)
  75. # desired_state = {
  76. # 'status' => 'ON',
  77. # 'level' => 100,
  78. # 'hue' => 0,
  79. # 'saturation' => 100
  80. # }
  81. # patched_state = @client.patch_state(desired_state, group_0_params)
  82. # expect(patched_state.keys).to include(*desired_state.keys)
  83. # expect(patched_state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  84. # end
  85. end
  86. end