# frozen_string_literal: true require "test_helper" require "forwardable" require "customer_info_form" require "customer_repo" class FakeRepo def initialize(customers) @customers = customers end def find_by(k, v) @customers.find { |cust| cust.public_send(k).to_s == v.to_s } end def find_by_format(s) EMPromise.resolve(nil).then do key = CustomerRepo::QueryKey.for(s) case key when CustomerRepo::QueryKey::ID find_by(:customer_id, key.customer_id) when CustomerRepo::QueryKey::JID find_by(:jid, key.jid) when CustomerRepo::QueryKey::Tel find_by(:tel, key.tel) else raise "Un-faked format: #{s}" end || raise("No Customer") end end end class CustomerInfoFormTest < Minitest::Test def setup @customer_test = OpenStruct.new( customer_id: "test", jid: "test\\40example.com@example.net", tel: "+13334445555" ) @customer_v2 = OpenStruct.new( customer_id: "test_v2", jid: "test_v2\\40example.com@example.net", tel: "+14445556666" ) @repo = FakeRepo.new([@customer_test, @customer_v2]) @info_form = CustomerInfoForm.new(@repo) end def test_nothing assert_kind_of( CustomerInfoForm::NoCustomer, @info_form.parse_something("").sync ) end em :test_nothing def test_find_customer_id result = @info_form.parse_something("test").sync assert_equal @customer_test, result end em :test_find_customer_id def test_find_real_jid result = @info_form.parse_something("test@example.com").sync assert_equal @customer_test, result end em :test_find_real_jid def test_find_cheo_jid result = @info_form.parse_something( "test\\40example.com@example.net" ).sync assert_equal @customer_test, result end em :test_find_cheo_jid def test_find_sgx_jmp_customer_by_phone result = @info_form.parse_something("+13334445555").sync assert_equal @customer_test, result end em :test_find_sgx_jmp_customer_by_phone def test_find_sgx_jmp_customer_by_phone_friendly_format result = @info_form.parse_something("13334445555").sync assert_equal @customer_test, result result = @info_form.parse_something("3334445555").sync assert_equal @customer_test, result result = @info_form.parse_something("(333) 444-5555").sync assert_equal @customer_test, result end em :test_find_sgx_jmp_customer_by_phone_friendly_format def test_find_v2_customer_by_phone result = @info_form.parse_something("+14445556666").sync assert_equal @customer_v2, result end em :test_find_v2_customer_by_phone def test_missing_customer_by_phone result = @info_form.parse_something("+17778889999").sync assert_kind_of( CustomerInfoForm::NoCustomer, result ) end em :test_missing_customer_by_phone def test_garbage result = @info_form.parse_something("garbage").sync assert_kind_of( CustomerInfoForm::NoCustomer, result ) end em :test_garbage end