1# frozen_string_literal: true
2
3require "test_helper"
4require "electrum"
5
6class ElectrumTest < Minitest::Test
7 RPC_URI = "http://example.com"
8
9 def setup
10 @electrum = Electrum.new(
11 rpc_uri: RPC_URI,
12 rpc_username: "username",
13 rpc_password: "password"
14 )
15 end
16
17 def stub_rpc(method, params)
18 stub_request(:post, RPC_URI).with(
19 headers: { "Content-Type" => "application/json" },
20 basic_auth: ["username", "password"],
21 body: hash_including(
22 method: method,
23 params: params
24 )
25 )
26 end
27
28 property(:getaddresshistory) { string(:alnum) }
29 def getaddresshistory(address)
30 req =
31 stub_rpc("getaddresshistory", address: address)
32 .to_return(body: { result: "result" }.to_json)
33 assert_equal "result", @electrum.getaddresshistory(address)
34 assert_requested(req)
35 end
36
37 property(:get_tx_status) { string(:alnum) }
38 def get_tx_status(tx_hash)
39 req =
40 stub_rpc("get_tx_status", txid: tx_hash)
41 .to_return(body: { result: "result" }.to_json)
42 assert_equal "result", @electrum.get_tx_status(tx_hash)
43 assert_requested(req)
44 end
45
46 property(:gettransaction) { [string(:alnum), string(:xdigit)] }
47 def gettransaction(tx_hash, dummy_tx)
48 req1 =
49 stub_rpc("gettransaction", txid: tx_hash)
50 .to_return(body: { result: dummy_tx }.to_json)
51 req2 =
52 stub_rpc("deserialize", [dummy_tx])
53 .to_return(body: { result: { outputs: [] } }.to_json)
54 assert_kind_of Electrum::Transaction, @electrum.gettransaction(tx_hash)
55 assert_requested(req1)
56 assert_requested(req2)
57 end
58
59 class TransactionTest < Minitest::Test
60 def transaction(outputs=[])
61 electrum_mock = Minitest::Mock.new("Electrum")
62 [
63 electrum_mock,
64 Electrum::Transaction.new(
65 electrum_mock,
66 "txhash",
67 "outputs" => outputs
68 )
69 ]
70 end
71
72 def test_confirmations
73 electrum_mock, tx = transaction
74 electrum_mock.expect(
75 :get_tx_status,
76 { "confirmations" => 1234 },
77 ["txhash"]
78 )
79 assert_equal 1234, tx.confirmations
80 end
81
82 def test_amount_for_empty
83 _, tx = transaction
84 assert_equal 0, tx.amount_for
85 end
86
87 def test_amount_for_address_not_present
88 _, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
89 assert_equal 0, tx.amount_for("other_address")
90 end
91
92 def test_amount_for_address_present
93 _, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
94 assert_equal 0.00000001, tx.amount_for("address")
95 end
96
97 def test_amount_for_one_of_address_present
98 _, tx = transaction([{ "address" => "address", "value_sats" => 1 }])
99 assert_equal 0.00000001, tx.amount_for("boop", "address", "lol")
100 end
101 end
102end