~singpolyma/sgx-jmp

ref: 045da39f25b74034e7bba44cb5f1db48a935ba84 sgx-jmp/test/test_command_list.rb -rw-r--r-- 2.6 KiB
045da39fStephen Paul Weber Hotfix: arguments in wrong order 1 year, 1 month 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
# frozen_string_literal: true

require "test_helper"
require "command"
require "command_list"

CommandList::Customer = Minitest::Mock.new
CommandList::REDIS = Minitest::Mock.new

class CommandListTest < Minitest::Test
	SETUP = begin
		[
			Command.new("no_customer", "", list_for: ->(**) { true }),
			Command.new("registered", "", list_for: ->(tel:, **) { !!tel }),
			Command.new("fwd", "", list_for: ->(fwd: nil, **) { !!fwd }),
			Command.new(
				"currency", "",
				list_for: ->(customer: nil, **) { !!customer&.currency }
			),
			Command.new(
				"cc", "",
				list_for: ->(payment_methods: [], **) { !payment_methods.empty? }
			)
		].each do |c|
			CommandList.register(c)
		end
	end

	def test_for_no_customer
		assert_equal(
			["no_customer"],
			CommandList.for(nil).sync.map { |c| c[:node] }
		)
	end
	em :test_for_no_customer

	def test_for_unregistered
		customer = OpenStruct.new(registered?: false)
		assert_equal(
			["no_customer"],
			CommandList.for(customer).sync.map { |c| c[:node] }
		)
	end
	em :test_for_unregistered

	def test_for_registered
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_fwd-1"]
		)
		customer = OpenStruct.new(
			registered?: OpenStruct.new(phone: "1"),
			payment_methods: EMPromise.resolve([])
		)
		assert_equal(
			["no_customer", "registered"],
			CommandList.for(customer).sync.map { |c| c[:node] }
		)
	end
	em :test_for_registered

	def test_for_registered_with_fwd
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve("tel:1"),
			["catapult_fwd-1"]
		)
		customer = OpenStruct.new(
			registered?: OpenStruct.new(phone: "1"),
			payment_methods: EMPromise.resolve([])
		)
		assert_equal(
			["no_customer", "registered", "fwd"],
			CommandList.for(customer).sync.map { |c| c[:node] }
		)
	end
	em :test_for_registered_with_fwd

	def test_for_registered_with_credit_card
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_fwd-1"]
		)
		customer = OpenStruct.new(
			registered?: OpenStruct.new(phone: "1"),
			plan_name: "test",
			payment_methods: EMPromise.resolve([:boop])
		)
		assert_equal(
			["no_customer", "registered", "cc"],
			CommandList.for(customer).sync.map { |c| c[:node] }
		)
	end
	em :test_for_registered_with_credit_card

	def test_for_registered_with_currency
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_fwd-1"]
		)
		customer = OpenStruct.new(
			registered?: OpenStruct.new(phone: "1"),
			currency: :USD
		)
		assert_equal(
			["no_customer", "registered", "currency"],
			CommandList.for(customer).sync.map { |c| c[:node] }
		)
	end
	em :test_for_registered_with_currency
end