test_customer_info_form.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "forwardable"
  5require "customer_info_form"
  6require "customer_repo"
  7
  8class FakeRepo
  9	def initialize(customers)
 10		@customers = customers
 11	end
 12
 13	def find(id)
 14		EMPromise.resolve(nil).then do
 15			@customers.find { |cust| cust.customer_id == id } || raise("No Customer")
 16		end
 17	end
 18
 19	def find_by_jid(jid)
 20		EMPromise.resolve(nil).then do
 21			@customers.find { |cust| cust.jid.to_s == jid.to_s } || raise("No Customer")
 22		end
 23	end
 24
 25	def find_by_tel(tel)
 26		EMPromise.resolve(nil).then do
 27			@customers.find { |cust| cust.tel == tel } || raise("No Customer")
 28		end
 29	end
 30end
 31
 32class CustomerInfoFormTest < Minitest::Test
 33	def setup
 34		@customer_test = OpenStruct.new(
 35			customer_id: "test",
 36			jid: "test\\40example.com@example.net",
 37			tel: "+13334445555"
 38		)
 39		@customer_v2 = OpenStruct.new(
 40			customer_id: "test_v2",
 41			jid: "test_v2\\40example.com@example.net",
 42			tel: "+14445556666"
 43		)
 44		@repo = FakeRepo.new([@customer_test, @customer_v2])
 45		@info_form = CustomerInfoForm.new(@repo)
 46	end
 47
 48	def test_nothing
 49		assert_kind_of(
 50			CustomerInfoForm::NoCustomer,
 51			@info_form.parse_something("").sync
 52		)
 53	end
 54	em :test_nothing
 55
 56	def test_find_customer_id
 57		result = @info_form.parse_something("test").sync
 58		assert_equal @customer_test, result
 59	end
 60	em :test_find_customer_id
 61
 62	def test_find_real_jid
 63		result = @info_form.parse_something("test@example.com").sync
 64		assert_equal @customer_test, result
 65	end
 66	em :test_find_real_jid
 67
 68	def test_find_cheo_jid
 69		result = @info_form.parse_something(
 70			"test\\40example.com@example.net"
 71		).sync
 72		assert_equal @customer_test, result
 73	end
 74	em :test_find_cheo_jid
 75
 76	def test_find_sgx_jmp_customer_by_phone
 77		result = @info_form.parse_something("+13334445555").sync
 78		assert_equal @customer_test, result
 79	end
 80	em :test_find_sgx_jmp_customer_by_phone
 81
 82	def test_find_sgx_jmp_customer_by_phone_friendly_format
 83		result = @info_form.parse_something("13334445555").sync
 84		assert_equal @customer_test, result
 85
 86		result = @info_form.parse_something("3334445555").sync
 87		assert_equal @customer_test, result
 88
 89		result = @info_form.parse_something("(333) 444-5555").sync
 90		assert_equal @customer_test, result
 91	end
 92	em :test_find_sgx_jmp_customer_by_phone_friendly_format
 93
 94	def test_find_v2_customer_by_phone
 95		result = @info_form.parse_something("+14445556666").sync
 96		assert_equal @customer_v2, result
 97	end
 98	em :test_find_v2_customer_by_phone
 99
100	def test_missing_customer_by_phone
101		result = @info_form.parse_something("+17778889999").sync
102		assert_kind_of(
103			CustomerInfoForm::NoCustomer,
104			result
105		)
106	end
107	em :test_missing_customer_by_phone
108
109	def test_garbage
110		result = @info_form.parse_something("garbage").sync
111		assert_kind_of(
112			CustomerInfoForm::NoCustomer,
113			result
114		)
115	end
116	em :test_garbage
117end