Skip to content

Commit

Permalink
Change NWC test to use mock
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman authored and TonyGiorgio committed Dec 21, 2023
1 parent 01582cb commit f45ab40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ impl<S: MutinyStorage> Node<S> {
.read_payment_info(payment_hash.as_inner(), false, &self.logger)
{
Some(payment_info) => Ok((payment_info, false)),
None => Err(MutinyError::InvoiceInvalid),
None => Err(MutinyError::NotFound),
}
}

Expand Down
103 changes: 33 additions & 70 deletions mutiny-core/src/nostr/nwc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,15 +1117,11 @@ mod wasm_test {
use crate::nodemanager::MutinyInvoice;
use crate::nostr::ProfileType;
use crate::storage::MemoryStorage;
use crate::test_utils::{create_dummy_invoice, create_nwc_request};
use crate::test_utils::{create_dummy_invoice, create_mutiny_wallet, create_nwc_request};
use crate::MockInvoiceHandler;
use crate::{
event::{MillisatAmount, PaymentInfo},
test_utils::create_mutiny_wallet,
};
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::ONE_KEY;
use bitcoin::Network;
use mockall::predicate::eq;
use nostr::key::SecretKey;
use std::sync::{atomic::AtomicBool, Arc};
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
Expand Down Expand Up @@ -1205,15 +1201,15 @@ mod wasm_test {
#[test]
async fn test_process_nwc_event_require_approval() {
let storage = MemoryStorage::default();
let mw = create_mutiny_wallet(storage.clone()).await;
mw.node_manager.new_node().await.unwrap();
let logger = Arc::new(MutinyLogger::default());
let mut node = MockInvoiceHandler::new();
node.expect_logger().return_const(MutinyLogger::default());
storage.set_done_first_sync().unwrap();

let xprivkey = ExtendedPrivKey::new_master(Network::Regtest, &[0; 64]).unwrap();
let stop = Arc::new(AtomicBool::new(false));
let nostr_manager =
NostrManager::from_mnemonic(xprivkey, storage.clone(), mw.logger.clone(), stop)
.unwrap();
NostrManager::from_mnemonic(xprivkey, storage.clone(), logger.clone(), stop).unwrap();

let profile = nostr_manager
.create_new_profile(
Expand All @@ -1235,7 +1231,7 @@ mod wasm_test {
.to_event(&Keys::new(uri.secret))
.unwrap()
};
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
assert_eq!(result.unwrap(), None);
check_no_pending_invoices(&storage);

Expand All @@ -1252,19 +1248,19 @@ mod wasm_test {
.to_event(&Keys::new(uri.secret))
.unwrap()
};
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
assert_eq!(result.unwrap(), None);
check_no_pending_invoices(&storage);

// test invalid invoice
let event = create_nwc_request(&uri, "invalid invoice".to_string());
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
assert_eq!(result.unwrap_err().to_string(), "Failed to parse invoice");
check_no_pending_invoices(&storage);

// test expired invoice
let event = create_nwc_request(&uri, INVOICE.to_string());
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
check_nwc_error_response(
result.unwrap().unwrap(),
&uri.secret,
Expand All @@ -1278,7 +1274,7 @@ mod wasm_test {
// test amount-less invoice
let (invoice, _) = create_dummy_invoice(None, Network::Regtest, None);
let event = create_nwc_request(&uri, invoice.to_string());
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
check_nwc_error_response(
result.unwrap().unwrap(),
&uri.secret,
Expand All @@ -1290,11 +1286,12 @@ mod wasm_test {
check_no_pending_invoices(&storage);

// test hodl invoice
node.expect_skip_hodl_invoices().return_const(true);
let invoice = create_dummy_invoice(Some(10_000), Network::Regtest, Some(ONE_KEY))
.0
.to_string();
let event = create_nwc_request(&uri, invoice);
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
check_nwc_error_response(
result.unwrap().unwrap(),
&uri.secret,
Expand All @@ -1307,78 +1304,44 @@ mod wasm_test {

// test in-flight payment
let (invoice, _) = create_dummy_invoice(Some(1_000), Network::Regtest, None);
let payment_info = PaymentInfo {
preimage: None,
secret: Some(invoice.payment_secret().0),
status: HTLCStatus::InFlight,
amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
fee_paid_msat: None,
bolt11: Some(invoice.clone()),
payee_pubkey: None,
last_update: utils::now().as_secs(),
};
mw.node_manager
.nodes
.lock()
.await
.values()
.next()
.unwrap()
.persister
.persist_payment_info(invoice.payment_hash().as_inner(), &payment_info, false)
.unwrap();
node.expect_get_outbound_payment_status()
.with(eq(invoice.payment_hash().into_32()))
.returning(move |_| Some(HTLCStatus::InFlight));
let event = create_nwc_request(&uri, invoice.to_string());
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
assert_eq!(result.unwrap(), None);
// FIXME: Not working
// check_no_pending_invoices(&storage);
check_no_pending_invoices(&storage);

// test completed payment
let (invoice, _) = create_dummy_invoice(Some(1_000), Network::Regtest, None);
let payment_info = PaymentInfo {
preimage: None,
secret: Some(invoice.payment_secret().0),
status: HTLCStatus::Succeeded,
amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
fee_paid_msat: None,
bolt11: Some(invoice.clone()),
payee_pubkey: None,
last_update: utils::now().as_secs(),
};
mw.node_manager
.nodes
.lock()
.await
.values()
.next()
.unwrap()
.persister
.persist_payment_info(invoice.payment_hash().as_inner(), &payment_info, false)
.unwrap();
node.expect_get_outbound_payment_status()
.with(eq(invoice.payment_hash().into_32()))
.returning(move |_| Some(HTLCStatus::Succeeded));
let event = create_nwc_request(&uri, invoice.to_string());
let result = nwc.handle_nwc_request(event, &mw, &nostr_manager).await;
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
assert_eq!(result.unwrap(), None);
// FIXME: Not working
// check_no_pending_invoices(&storage);
check_no_pending_invoices(&storage);

// test it goes to pending
let (invoice, _) = create_dummy_invoice(Some(1_000), Network::Regtest, None);
node.expect_get_outbound_payment_status()
.with(eq(invoice.payment_hash().into_32()))
.returning(move |_| None);
let event = create_nwc_request(&uri, invoice.to_string());
let result = nwc
.handle_nwc_request(event.clone(), &mw, &nostr_manager)
.handle_nwc_request(event.clone(), &node, &nostr_manager)
.await;
assert_eq!(result.unwrap(), None);

let _pending: Vec<PendingNwcInvoice> = storage
let pending: Vec<PendingNwcInvoice> = storage
.get_data(PENDING_NWC_EVENTS_KEY)
.unwrap()
.unwrap_or_default();
// FIXME: Not working
// assert_eq!(pending.len(), 1);
// assert_eq!(pending[0].invoice, invoice);
// assert_eq!(pending[0].event_id, event.id);
// assert_eq!(pending[0].index, nwc.profile.index);
// assert_eq!(pending[0].pubkey, event.pubkey);
assert_eq!(pending.len(), 1);
assert_eq!(pending[0].invoice, invoice);
assert_eq!(pending[0].event_id, event.id);
assert_eq!(pending[0].index, nwc.profile.index);
assert_eq!(pending[0].pubkey, event.pubkey);
}

#[test]
Expand Down

0 comments on commit f45ab40

Please sign in to comment.