test(webhook): nil text with media, empty body

Phillip Davis created

Change summary

sgx-bwmsgsv2.rb                       |  2 
test/property/test_webhook_handler.rb | 33 +++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)

Detailed changes

sgx-bwmsgsv2.rb 🔗

@@ -1130,7 +1130,7 @@ class WebhookHandler < Goliath::API
 
 			# If text is not empty, but there isn't a msg,
 			# we need to construct a msg to convey that text
-			unless msg || text&.empty?
+			unless msg || text.to_s.empty?
 				msg = Blather::Stanza::Message.new(
 					bare_jid,
 					# TODO: The numbers, what do they mean?

test/property/test_webhook_handler.rb 🔗

@@ -491,4 +491,37 @@ class WebhookPropertyTest < Minitest::Test
 		}
 	end
 	em :test_message_with_empty_to_returns_400
+
+	def test_inbound_nil_text_with_media_multi_recipient_writes_empty_body
+		property_of {
+			Webhook
+				.new(REDIS)
+				.type { "message-received" }
+				.message { |registered, jid, dir, top_level_to|
+					Message
+						.new(REDIS)
+						.to {
+							array(range(1, 3)) { nanpa_phone } +
+								[top_level_to] +
+								array(integer(2)) { nanpa_phone }
+						}
+						.text { nil }
+						.media { array(range(1, 3)) { media_url } }
+						.generate(registered, jid, dir)
+				}
+				.generate
+		}.check { |metadata, example|
+			result = invoke_webhook(example)
+			assert_equal 200, result[0]
+			assert_operator written.length, :>=, 1
+
+			msg = written.last
+			assert_kind_of Blather::Stanza::Message, msg
+			assert(
+				msg.body.to_s.empty?,
+				"Body should be nil/empty when text is nil"
+			)
+		}
+	end
+	em :test_inbound_nil_text_with_media_multi_recipient_writes_empty_body
 end