diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index 48e90d240..5ecbe5207 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -1154,7 +1154,7 @@ impl Node { .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), } } diff --git a/mutiny-core/src/nostr/nwc.rs b/mutiny-core/src/nostr/nwc.rs index 63ac7639b..96b16f06a 100644 --- a/mutiny-core/src/nostr/nwc.rs +++ b/mutiny-core/src/nostr/nwc.rs @@ -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}; @@ -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( @@ -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); @@ -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, @@ -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, @@ -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, @@ -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 = storage + let pending: Vec = 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]