~singpolyma/sgx-jmp

ref: 023c87a11bbaa6bd3eaeb3977330b8ea80ded0cd sgx-jmp/test/test_tel_selections.rb -rw-r--r-- 3.1 KiB
023c87a1Stephen Paul Weber Quieter voicemail beep 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true

require "test_helper"
require "tel_selections"

class TelSelectionsTest < Minitest::Test
	def setup
		@manager = TelSelections.new(redis: FakeRedis.new)
	end

	def test_set_get
		assert_kind_of TelSelections::ChooseTel, @manager["jid@example.com"].sync
		@manager.set("jid@example.com", "+15555550000").sync
		assert_kind_of TelSelections::HaveTel, @manager["jid@example.com"].sync
	end
	em :test_set_get

	def test_choose_tel_have_tel
		jid = "jid@example.com"
		@manager.set(jid, "+15555550000").sync
		assert_equal "+15555550000", @manager[jid].then(&:choose_tel).sync
	end
	em :test_choose_tel_have_tel

	class AvailableNumberTest < Minitest::Test
		def test_for_no_rsm
			form = Blather::Stanza::X.new
			form.fields = [{ var: "q", value: "226" }]
			iris_query =
				TelSelections::ChooseTel::AvailableNumber
				.for(form)
				.instance_variable_get(:@iris_query)
			assert_equal(
				{ areaCode: "226", enableTNDetail: true, quantity: 10 },
				iris_query
			)
		end

		def test_for_rsm
			form = Blather::Stanza::X.new
			form.fields = [{ var: "q", value: "226" }]
			Nokogiri::XML::Builder.with(form) do
				set(xmlns: "http://jabber.org/protocol/rsm") do
					max 500
				end
			end
			iris_query =
				TelSelections::ChooseTel::AvailableNumber
				.for(form)
				.instance_variable_get(:@iris_query)
			assert_equal(
				{ areaCode: "226", enableTNDetail: true, quantity: 500 },
				iris_query
			)
		end
	end

	class TnTest < Minitest::Test
		def setup
			@tn = TelSelections::ChooseTel::Tn.new(
				full_number: "5551234567",
				city: "Toronto",
				state: "ON",
				garbage: "stuff"
			)
		end

		def test_to_s
			assert_equal "(555) 123-4567 (Toronto, ON)", @tn.to_s
		end

		def test_tel
			assert_equal "+15551234567", @tn.tel
		end

		def test_option
			assert_equal(
				Blather::Stanza::X::Field::Option.new(
					label: "(555) 123-4567 (Toronto, ON)",
					value: "+15551234567"
				),
				@tn.option
			)
		end

		def test_option_reference
			ref = @tn.option.find("ns:reference", ns: "urn:xmpp:reference:0").first
			assert_equal(
				@tn.formatted_tel,
				@tn.option.label[ref["begin"].to_i..ref["end"].to_i]
			)
			assert_equal "tel:+15551234567", ref["uri"]
		end
	end

	class QTest < Minitest::Test
		def test_for_area_code
			q = TelSelections::ChooseTel::Q.for("226")
			assert_equal({ areaCode: "226" }, q.iris_query)
		end

		def test_for_npanxx
			q = TelSelections::ChooseTel::Q.for("226666")
			assert_equal({ npaNxx: "226666" }, q.iris_query)
		end

		def test_for_npanxxx
			q = TelSelections::ChooseTel::Q.for("2266667")
			assert_equal({ npaNxxx: "2266667" }, q.iris_query)
		end

		def test_for_zip
			q = TelSelections::ChooseTel::Q.for("90210")
			assert_equal({ zip: "90210" }, q.iris_query)
		end

		def test_for_localvanity
			q = TelSelections::ChooseTel::Q.for("~mboa")
			assert_equal({ localVanity: "mboa" }, q.iris_query)
		end

		def test_for_citystate
			q = TelSelections::ChooseTel::Q.for("Toronto, ON")
			assert_equal({ city: "Toronto", state: "ON" }, q.iris_query)
		end

		def test_for_garbage
			assert_raises { TelSelections::ChooseTel::Q.for("garbage") }
		end
	end
end