test_electrum.rb

  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	em :test_getaddresshistory
 30	def getaddresshistory(address)
 31		req = stub_rpc("getaddresshistory", address: address)
 32			.to_return(body: { result: "result" }.to_json)
 33		assert_equal "result", @electrum.getaddresshistory(address).sync
 34		assert_requested(req)
 35	end
 36
 37	property(:get_tx_status) { string(:alnum) }
 38	em :test_get_tx_status
 39	def get_tx_status(tx_hash)
 40		req = 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).sync
 43		assert_requested(req)
 44	end
 45
 46	property(:gettransaction) { [string(:alnum), string(:xdigit)] }
 47	em :test_gettransaction
 48	def gettransaction(tx_hash, dummy_tx)
 49		req1 = stub_rpc("gettransaction", txid: tx_hash)
 50			.to_return(body: { result: dummy_tx }.to_json)
 51		req2 = stub_rpc("deserialize", [dummy_tx])
 52			.to_return(body: { result: { outputs: [] } }.to_json)
 53		assert_kind_of Electrum::Transaction, @electrum.gettransaction(tx_hash).sync
 54		assert_requested(req1)
 55		assert_requested(req2)
 56	end
 57
 58	class TransactionTest < Minitest::Test
 59		def transaction(outputs=[])
 60			electrum_mock = Minitest::Mock.new("Electrum")
 61			[
 62				electrum_mock,
 63				Electrum::Transaction.new(
 64					electrum_mock,
 65					"txhash",
 66					"outputs" => outputs
 67				)
 68			]
 69		end
 70
 71		def test_confirmations
 72			electrum_mock, tx = transaction
 73			electrum_mock.expect(
 74				:get_tx_status,
 75				EMPromise.resolve("confirmations" => 1234),
 76				["txhash"]
 77			)
 78			assert_equal 1234, tx.confirmations.sync
 79		end
 80		em :test_confirmations
 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