Skip to content

Commit 44206f6

Browse files
authored
Merge pull request #91 from utxostack/amountless-invoice-tests
Add PaymentInfo and invoice tests
2 parents 1b924ed + 3cd7d1d commit 44206f6

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mutiny-core/src/event.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct PaymentInfo {
3232
pub secret: Option<[u8; 32]>,
3333
pub status: HTLCStatus,
3434
#[serde(skip_serializing_if = "MillisatAmount::is_none")]
35+
#[serde(default)]
3536
pub amt_msat: MillisatAmount,
3637
#[serde(skip_serializing_if = "Option::is_none")]
3738
pub fee_paid_msat: Option<u64>,
@@ -44,7 +45,7 @@ pub struct PaymentInfo {
4445
pub last_update: u64,
4546
}
4647

47-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
48+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
4849
pub struct MillisatAmount(pub Option<u64>);
4950

5051
impl MillisatAmount {
@@ -914,6 +915,7 @@ mod test {
914915
use crate::event::{HTLCStatus, MillisatAmount, PaymentInfo};
915916
use crate::{utils, PrivacyLevel};
916917
use bitcoin::secp256k1::PublicKey;
918+
use lightning_invoice::Bolt11Invoice;
917919
use std::str::FromStr;
918920

919921
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
@@ -947,4 +949,32 @@ mod test {
947949
let deserialized: PaymentInfo = serde_json::from_value(serialized).unwrap();
948950
assert_eq!(payment_info, deserialized);
949951
}
952+
953+
#[test]
954+
fn test_payment_info_without_amount() {
955+
let pubkey = PublicKey::from_str(
956+
"02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b",
957+
)
958+
.unwrap();
959+
960+
let payment_info = PaymentInfo {
961+
preimage: None,
962+
status: HTLCStatus::Succeeded,
963+
privacy_level: PrivacyLevel::Anonymous,
964+
amt_msat: MillisatAmount(None),
965+
fee_paid_msat: None,
966+
bolt11: Some(Bolt11Invoice::from_str("lntb1pnmghqhdqqnp4qty5slw3t6d6gt43tndkq6p6ut9ewrqrfq2nj67wnmk6dqzefweqcpp5fk6cxcwnjdrzw5zm9mzjuhfrwnee3feewmtycj5nk7klngava7gqsp5qajl23w8dluhxn90duny44ar0syrxqa4w3ap8635aat78lvdvfds9qyysgqcqzptxqyz5vqrzjqg7s0fwc76ky6umpgeuh7p7qm4l4jljw0uxa3uu5vrupjzjlpeny0apyqqqqqqqqsgqqqqlgqqqqlgqqjqr5p4cd64qa80ksthgdff908gxmjwvrwwmhnxnxlsrc0c2weuzcw3kthknu6cgalqdk0cnqsugvmcz9dvgr5l9rtphgm37ycg362s9sspwvxmj0").unwrap()),
967+
payee_pubkey: Some(pubkey),
968+
secret: None,
969+
last_update: utils::now().as_secs(),
970+
};
971+
972+
let serialized = serde_json::to_string(&payment_info).unwrap();
973+
let deserialized: PaymentInfo = serde_json::from_str(&serialized).unwrap();
974+
assert_eq!(payment_info, deserialized);
975+
976+
let serialized = serde_json::to_value(&payment_info).unwrap();
977+
let deserialized: PaymentInfo = serde_json::from_value(serialized).unwrap();
978+
assert_eq!(payment_info, deserialized);
979+
}
950980
}

mutiny-core/src/node.rs

+42
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,48 @@ mod wasm_test {
30453045
assert_eq!(label_item.invoices.into_iter().collect_vec(), vec![invoice]);
30463046
}
30473047

3048+
#[test]
3049+
async fn test_create_invoice_without_amount() {
3050+
let storage = MemoryStorage::default();
3051+
let node = create_node(storage.clone()).await;
3052+
let logger = Arc::new(MutinyLogger::default());
3053+
3054+
let now = crate::utils::now().as_secs();
3055+
3056+
let amount_sats = None;
3057+
let label = "test".to_string();
3058+
let labels = vec![label.clone()];
3059+
3060+
let (invoice, _) = node
3061+
.create_invoice(amount_sats, None, labels.clone(), None)
3062+
.await
3063+
.unwrap();
3064+
3065+
assert_eq!(invoice.amount_milli_satoshis(), amount_sats);
3066+
match invoice.description() {
3067+
Bolt11InvoiceDescription::Direct(desc) => {
3068+
assert_eq!(desc.to_string(), "test");
3069+
}
3070+
_ => panic!("unexpected invoice description"),
3071+
}
3072+
3073+
let from_storage = get_invoice_by_hash(invoice.payment_hash(), &storage, &logger).unwrap();
3074+
let by_hash = get_invoice_by_hash(invoice.payment_hash(), &storage, &logger).unwrap();
3075+
3076+
assert_eq!(from_storage, by_hash);
3077+
assert_eq!(from_storage.bolt11, Some(invoice.clone()));
3078+
assert_eq!(from_storage.description, Some("test".to_string()));
3079+
assert_eq!(from_storage.payment_hash, invoice.payment_hash().to_owned());
3080+
assert_eq!(from_storage.preimage, None);
3081+
assert_eq!(from_storage.payee_pubkey, None);
3082+
assert_eq!(from_storage.amount_sats, amount_sats);
3083+
assert_eq!(from_storage.status, HTLCStatus::Pending);
3084+
assert_eq!(from_storage.fees_paid, None);
3085+
assert_eq!(from_storage.labels, labels.clone());
3086+
assert!(from_storage.inbound);
3087+
assert!(from_storage.last_updated >= now);
3088+
}
3089+
30483090
#[test]
30493091
async fn test_fail_own_invoice() {
30503092
let storage = MemoryStorage::default();

mutiny-wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cargo-features = ["per-package-target"]
22

33
[package]
44
name = "mutiny-wasm"
5-
version = "1.12.0"
5+
version = "1.12.1"
66
edition = "2021"
77
authors = ["utxostack"]
88
forced-target = "wasm32-unknown-unknown"

0 commit comments

Comments
 (0)