~singpolyma/sgx-jmp

ref: 045da39f25b74034e7bba44cb5f1db48a935ba84 sgx-jmp/test/test_electrum.rb -rw-r--r-- 2.7 KiB
045da39fStephen Paul Weber Hotfix: arguments in wrong order 2 years 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
# frozen_string_literal: true

require "test_helper"
require "electrum"

class ElectrumTest < Minitest::Test
	RPC_URI = "http://example.com"

	def setup
		@electrum = Electrum.new(
			rpc_uri: RPC_URI,
			rpc_username: "username",
			rpc_password: "password"
		)
	end

	def stub_rpc(method, params)
		stub_request(:post, RPC_URI).with(
			headers: { "Content-Type" => "application/json" },
			basic_auth: ["username", "password"],
			body: hash_including(
				method: method,
				params: params
			)
		)
	end

	property(:getaddresshistory) { string(:alnum) }
	em :test_getaddresshistory
	def getaddresshistory(address)
		req =
			stub_rpc("getaddresshistory", address: address)
			.to_return(body: { result: "result" }.to_json)
		assert_equal "result", @electrum.getaddresshistory(address).sync
		assert_requested(req)
	end

	property(:get_tx_status) { string(:alnum) }
	em :test_get_tx_status
	def get_tx_status(tx_hash)
		req =
			stub_rpc("get_tx_status", txid: tx_hash)
			.to_return(body: { result: "result" }.to_json)
		assert_equal "result", @electrum.get_tx_status(tx_hash).sync
		assert_requested(req)
	end

	property(:gettransaction) { [string(:alnum), string(:xdigit)] }
	em :test_gettransaction
	def gettransaction(tx_hash, dummy_tx)
		req1 =
			stub_rpc("gettransaction", txid: tx_hash)
			.to_return(body: { result: dummy_tx }.to_json)
		req2 =
			stub_rpc("deserialize", [dummy_tx])
			.to_return(body: { result: { outputs: [] } }.to_json)
		assert_kind_of Electrum::Transaction, @electrum.gettransaction(tx_hash).sync
		assert_requested(req1)
		assert_requested(req2)
	end

	class TransactionTest < Minitest::Test
		def transaction(outputs=[])
			electrum_mock = Minitest::Mock.new("Electrum")
			[
				electrum_mock,
				Electrum::Transaction.new(
					electrum_mock,
					"txhash",
					"outputs" => outputs
				)
			]
		end

		def test_confirmations
			electrum_mock, tx = transaction
			electrum_mock.expect(
				:get_tx_status,
				EMPromise.resolve("confirmations" => 1234),
				["txhash"]
			)
			assert_equal 1234, tx.confirmations.sync
		end
		em :test_confirmations

		def test_amount_for_empty
			_, tx = transaction
			assert_equal 0, tx.amount_for
		end

		def test_amount_for_address_not_present
			_, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
			assert_equal 0, tx.amount_for("other_address")
		end

		def test_amount_for_address_present
			_, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
			assert_equal 0.00000001, tx.amount_for("address")
		end

		def test_amount_for_one_of_address_present
			_, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
			assert_equal 0.00000001, tx.amount_for("boop", "address", "lol")
		end
	end
end