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_by(k, v)
14 @customers.find { |cust| cust.public_send(k).to_s == v.to_s }
15 end
16
17 def find_by_format(s)
18 EMPromise.resolve(nil).then do
19 key = CustomerRepo::QueryKey.for(s)
20 case key
21 when CustomerRepo::QueryKey::ID
22 find_by(:customer_id, key.customer_id)
23 when CustomerRepo::QueryKey::JID
24 find_by(:jid, key.jid)
25 when CustomerRepo::QueryKey::Tel
26 find_by(:tel, key.tel)
27 else
28 raise "Un-faked format: #{s}"
29 end || raise("No Customer")
30 end
31 end
32end
33
34class CustomerInfoFormTest < Minitest::Test
35 def setup
36 @customer_test = OpenStruct.new(
37 customer_id: "test",
38 jid: "test\\40example.com@example.net",
39 tel: "+13334445555"
40 )
41 @customer_v2 = OpenStruct.new(
42 customer_id: "test_v2",
43 jid: "test_v2\\40example.com@example.net",
44 tel: "+14445556666"
45 )
46 @repo = FakeRepo.new([@customer_test, @customer_v2])
47 @info_form = CustomerInfoForm.new(@repo)
48 end
49
50 def test_nothing
51 assert_kind_of(
52 CustomerInfoForm::NoCustomer,
53 @info_form.parse_something("").sync
54 )
55 end
56 em :test_nothing
57
58 def test_find_customer_id
59 result = @info_form.parse_something("test").sync
60 assert_equal @customer_test, result
61 end
62 em :test_find_customer_id
63
64 def test_find_real_jid
65 result = @info_form.parse_something("test@example.com").sync
66 assert_equal @customer_test, result
67 end
68 em :test_find_real_jid
69
70 def test_find_cheo_jid
71 result = @info_form.parse_something(
72 "test\\40example.com@example.net"
73 ).sync
74 assert_equal @customer_test, result
75 end
76 em :test_find_cheo_jid
77
78 def test_find_sgx_jmp_customer_by_phone
79 result = @info_form.parse_something("+13334445555").sync
80 assert_equal @customer_test, result
81 end
82 em :test_find_sgx_jmp_customer_by_phone
83
84 def test_find_sgx_jmp_customer_by_phone_friendly_format
85 result = @info_form.parse_something("13334445555").sync
86 assert_equal @customer_test, result
87
88 result = @info_form.parse_something("3334445555").sync
89 assert_equal @customer_test, result
90
91 result = @info_form.parse_something("(333) 444-5555").sync
92 assert_equal @customer_test, result
93 end
94 em :test_find_sgx_jmp_customer_by_phone_friendly_format
95
96 def test_find_v2_customer_by_phone
97 result = @info_form.parse_something("+14445556666").sync
98 assert_equal @customer_v2, result
99 end
100 em :test_find_v2_customer_by_phone
101
102 def test_missing_customer_by_phone
103 result = @info_form.parse_something("+17778889999").sync
104 assert_kind_of(
105 CustomerInfoForm::NoCustomer,
106 result
107 )
108 end
109 em :test_missing_customer_by_phone
110
111 def test_garbage
112 result = @info_form.parse_something("garbage").sync
113 assert_kind_of(
114 CustomerInfoForm::NoCustomer,
115 result
116 )
117 end
118 em :test_garbage
119end