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|
22 cust.jid.to_s == jid.to_s
23 } || raise("No Customer")
24 end
25 end
26
27 def find_by_tel(tel)
28 EMPromise.resolve(nil).then do
29 @customers.find { |cust| cust.tel == tel } || 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