# frozen_string_literal: true

require "test_helper"
require_relative "../../sgx-bwmsgsv2"
require "rantly/minitest_extensions"
require_relative "generators/stanza"

class MaxMediaSizePropertyTest < Minitest::Test
	def test_oversized_media_inlined_as_text
		property_of {
			MessageStanza.new(REDIS)
				.recv_nums { [nanpa_phone] }
				.media { [[deliverable_media_url, range(MAX_MEDIA_SIZE + 1, MAX_MEDIA_SIZE << 1)]] }
				.body { message_body(segments: 1) }
				.bandwidth_stub { |_registered, media|
					(url, *), * = media
					WebMock::API.stub_request(:post, BW_MESSAGES_URL).with(
						body: WebMock::API.hash_including(text: /#{Regexp.escape(url)}/)
					).to_return(
						status: 202, body: JSON.dump(id: "bw-msg-oversized")
					)
				}
				.generate(ARGV[0])
		}.check { |_metadata, example|
			process_stanza(example["stanza"])
			example["assertions"].each { |a| instance_exec(&a) }
		}
	end
	em :test_oversized_media_inlined_as_text

	def test_undersized_media_sent_as_mms
		property_of {
			MessageStanza.new(REDIS)
				.recv_nums { [nanpa_phone] }
				.media { [[deliverable_media_url, range(1, MAX_MEDIA_SIZE)]] }
				.http_stubs { |media, _registered|
					media.map do |u, s|
						ext = File.extname(URI.parse(u).path)
						WebMock::API.stub_request(:head, u).to_return(
							status: 200,
							headers: {
								"Content-Length" => s.to_s,
								"Content-Type" => Rack::Mime.mime_type(ext)
							}
						)
					end
				}
				.bandwidth_stub { |_registered, media|
					(url, *), * = media
					WebMock::API.stub_request(:post, BW_MESSAGES_URL).with(
						body: WebMock::API.hash_including(media: url)
					).to_return(
						status: 202, body: JSON.dump(id: "bw-msg-undersized")
					)
				}
				.generate(ARGV[0])
		}.check { |_metadata, example|
			process_stanza(example["stanza"])
			example["assertions"].each { |a| instance_exec(&a) }
		}
	end
	em :test_undersized_media_sent_as_mms
end
