# frozen_string_literal: true

require "rack/test"
require "test_helper"
require_relative "../web"

Customer::BLATHER = Minitest::Mock.new

class WebTest < Minitest::Test
	include Rack::Test::Methods

	def app
		Web.opts[:customer_repo] = CustomerRepo.new(
			redis: FakeRedis.new(
				"jmp_customer_jid-customerid" => "customer@example.com",
				"catapult_jid-+15551234567" => "customer_customerid@component"
			),
			db: FakeDB.new,
			sgx_repo: Bwmsgsv2Repo.new(
				redis: FakeRedis.new,
				ibr_repo: FakeIBRRepo.new(
					"sgx" => {
						"customer_customerid@component" => IBR.new.tap do |ibr|
							ibr.phone = "+15551234567"
						end
					}
				)
			)
		)
		Web.app
	end

	def test_outbound_forwards
		post(
			"/outbound/calls",
			{ from: "customerid", to: "+15557654321" }.to_json,
			{ "CONTENT_TYPE" => "application/json" }
		)

		assert last_response.ok?
		assert_equal(
			"<?xml version=\"1.0\" encoding=\"utf-8\" ?><Response>" \
			"<Forward from=\"+15551234567\" to=\"+15557654321\" />" \
			"</Response>",
			last_response.body
		)
	end
	em :test_outbound_forwards

	def test_voicemail
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal "+15557654321@component", stanza.from.to_s
				assert_equal "customer@example.com", stanza.to.to_s
				assert_equal "https://jmp.chat/media", OOB.find_or_create(stanza).url
			end]
		)

		post(
			"/inbound/calls/CALLID/voicemail/audio",
			{
				"startTime" => "2021-01-01T00:00:00Z",
				"endTime" => "2021-01-01T00:00:06Z",
				"mediaUrl" => "https://voice.bandwidth.com/api/v2/accounts/1/media",
				"to" => "+15551234567",
				"from" => "+15557654321"
			}.to_json,
			{ "CONTENT_TYPE" => "application/json" }
		)

		assert last_response.ok?
		assert_mock Customer::BLATHER
	end
	em :test_voicemail

	def test_anonymous_voicemail
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal(
					"16;phone-context=anonymous.phone-context.soprani.ca@component",
					stanza.from.to_s
				)
				assert_equal "customer@example.com", stanza.to.to_s
				assert_equal "https://jmp.chat/media", OOB.find_or_create(stanza).url
			end]
		)

		post(
			"/inbound/calls/CALLID/voicemail/audio",
			{
				"startTime" => "2021-01-01T00:00:00Z",
				"endTime" => "2021-01-01T00:00:06Z",
				"mediaUrl" => "https://voice.bandwidth.com/api/v2/accounts/1/media",
				"to" => "+15551234567",
				"from" => "Anonymous"
			}.to_json,
			{ "CONTENT_TYPE" => "application/json" }
		)

		assert last_response.ok?
		assert_mock Customer::BLATHER
	end
	em :test_anonymous_voicemail

	def test_voicemail_short
		post(
			"/inbound/calls/CALLID/voicemail/audio",
			{
				"startTime" => "2021-01-01T00:00:00Z",
				"endTime" => "2021-01-01T00:00:05Z"
			}.to_json,
			{ "CONTENT_TYPE" => "application/json" }
		)

		assert last_response.ok?
		assert_mock Customer::BLATHER
	end
	em :test_voicemail_short
end
