Sfoglia il codice sorgente

Fix, spruce up tests

Christopher Mullins 6 anni fa
parent
commit
db528afb0c
1 ha cambiato i file con 66 aggiunte e 18 eliminazioni
  1. 66 18
      test/remote/spec/discovery_spec.rb

+ 66 - 18
test/remote/spec/discovery_spec.rb

@@ -4,12 +4,27 @@ RSpec.describe 'MQTT Discovery' do
   before(:all) do
     @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
     @client.upload_json('/settings', 'settings.json')
+
+    @test_id = 1
+    @topic_prefix = mqtt_topic_prefix()
+    @discovery_prefix = "#{@topic_prefix}discovery/"
+
+    @mqtt_client = create_mqtt_client()
+  end
+
+  after(:all) do
+    # Clean up any leftover cruft
+    @mqtt_client.on_message("#{@discovery_prefix}#", 1, false) do |topic, message|
+      if message.length > 0
+        @mqtt_client.publish(topic, '', true)
+      end
+      false
+    end
+    @mqtt_client.wait_for_listeners
   end
 
   before(:each) do
     mqtt_params = mqtt_parameters()
-    @topic_prefix = mqtt_topic_prefix()
-    @discovery_prefix = "#{@topic_prefix}/discovery"
 
     @client.put(
       '/settings',
@@ -21,9 +36,8 @@ RSpec.describe 'MQTT Discovery' do
       type: 'rgb_cct',
       group_id: 1
     }
-    @discovery_suffix = "#{@id_params[:type]}_#{sprintf("%x", @id_params[:id])}_#{@id_params[:group_id]}"
-
-    @mqtt_client = create_mqtt_client()
+    @discovery_suffix = "#{@id_params[:type]}_#{sprintf("0x%04x", @id_params[:id])}_#{@id_params[:group_id]}/config"
+    @test_discovery_prefix = "#{@discovery_prefix}#{@id_params[:id]}/"
   end
 
   context 'when not configured' do
@@ -42,45 +56,46 @@ RSpec.describe 'MQTT Discovery' do
     it 'should send discovery messages' do
       saw_message = false
 
-      @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
+      @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
         saw_message = true
       end
 
       @client.patch_settings(
-        home_assistant_discovery_prefix: @discovery_prefix,
+        home_assistant_discovery_prefix: @test_discovery_prefix,
         group_id_aliases: {
           'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
         }
       )
 
-      expect(saw_message).to be_true
+      @mqtt_client.wait_for_listeners
+
+      expect(saw_message).to be(true)
     end
 
     it 'config should have expected keys' do
       saw_message = false
       config = nil
 
-      @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
+      @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
         config = JSON.parse(message)
         saw_message = true
       end
 
       @client.patch_settings(
-        home_assistant_discovery_prefix: @discovery_prefix,
+        home_assistant_discovery_prefix: @test_discovery_prefix,
         group_id_aliases: {
           'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
         }
       )
 
-      expect(saw_message).to be_true
+      @mqtt_client.wait_for_listeners
+
+      expect(saw_message).to be(true)
       expected_keys = %w(
         schema
         name
         command_topic
         state_topic
-        availability_topic
-        payload_available
-        payload_not_available
         brightness
         rgb
         color_temp
@@ -94,16 +109,16 @@ RSpec.describe 'MQTT Discovery' do
       seen_config = false
       seen_blank_message = false
 
-      @mqtt_client.on_message("#{@discovery_prefix}/light/+/#{@discovery_suffix}") do |topic, message|
-        seen_config = message.length > 0
-        seen_blank_message = message.empty?
+      @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
+        seen_config = seen_config || message.length > 0
+        seen_blank_message = seen_blank_message || message.length == 0
 
         seen_config && seen_blank_message
       end
 
       # This should create the device
       @client.patch_settings(
-        home_assistant_discovery_prefix: @discovery_prefix,
+        home_assistant_discovery_prefix: @test_discovery_prefix,
         group_id_aliases: {
           'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
         }
@@ -113,6 +128,39 @@ RSpec.describe 'MQTT Discovery' do
       @client.patch_settings(
         group_id_aliases: { }
       )
+
+      @mqtt_client.wait_for_listeners
+
+      expect(seen_config).to be(true)
+      expect(seen_blank_message).to be(true), "should see deletion message"
+    end
+
+    it 'should configure devices with an availability topic if client status is configured' do
+      expected_keys = %w(
+        availability_topic
+        payload_available
+        payload_not_available
+      )
+      config = nil
+
+      @mqtt_client.on_message("#{@test_discovery_prefix}light/+/#{@discovery_suffix}") do |topic, message|
+        config = JSON.parse(message)
+        (expected_keys - config.keys).empty?
+      end
+
+      # This should create the device
+      @client.patch_settings(
+        home_assistant_discovery_prefix: @test_discovery_prefix,
+        group_id_aliases: {
+          'test_group' => [@id_params[:type], @id_params[:id], @id_params[:group_id]]
+        },
+        mqtt_client_status_topic: "#{@topic_prefix}status",
+        simple_mqtt_client_status: true
+      )
+
+      @mqtt_client.wait_for_listeners
+
+      expect(config.keys).to include(*expected_keys)
     end
   end
 end