Skip to content

Commit be286d6

Browse files
committed
cln-grpc: clean up notification tests
1 parent e4cff67 commit be286d6

1 file changed

Lines changed: 31 additions & 50 deletions

File tree

cln-grpc/src/test.rs

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ fn test_keysend() {
321321
assert_eq!(v, v2);
322322
}
323323

324+
/// Verify serde round-trip: serialize to JSON, deserialize back, and
325+
/// check the re-serialized value matches the first serialization.
326+
macro_rules! assert_serde_roundtrip {
327+
($value:expr, $type:ty) => {{
328+
let v = serde_json::to_value(&$value).unwrap();
329+
let rt: $type = serde_json::from_value(v.clone()).unwrap();
330+
let v2 = serde_json::to_value(&rt).unwrap();
331+
assert_eq!(v, v2);
332+
}};
333+
}
334+
324335
#[test]
325336
fn test_balance_snapshot() {
326337
let j: serde_json::Value = json!({
@@ -341,18 +352,12 @@ fn test_balance_snapshot() {
341352
]
342353
});
343354
let u: cln_rpc::notifications::BalanceSnapshotNotification =
344-
serde_json::from_value(j.clone()).unwrap();
355+
serde_json::from_value(j).unwrap();
345356
assert_eq!(u.accounts.len(), 2);
346357
assert_eq!(u.accounts[0].account_id, "wallet");
347358
assert_eq!(u.blockheight, 103);
348-
let _pb: crate::pb::BalanceSnapshotNotification = u.clone().into();
349-
350-
// Serde round-trip
351-
let v = serde_json::to_value(u.clone()).unwrap();
352-
let u2: cln_rpc::notifications::BalanceSnapshotNotification =
353-
serde_json::from_value(v.clone()).unwrap();
354-
let v2 = serde_json::to_value(u2).unwrap();
355-
assert_eq!(v, v2);
359+
assert_serde_roundtrip!(u, cln_rpc::notifications::BalanceSnapshotNotification);
360+
let _pb: crate::pb::BalanceSnapshotNotification = u.into();
356361
}
357362

358363
#[test]
@@ -376,12 +381,12 @@ fn test_coin_movement() {
376381
"extra_tags": ["keysend"]
377382
});
378383
let u: cln_rpc::notifications::CoinMovementNotification =
379-
serde_json::from_value(j.clone()).unwrap();
384+
serde_json::from_value(j).unwrap();
380385
assert_eq!(u.version, 2);
381386
assert_eq!(u.item_type, cln_rpc::notifications::CoinMovementType::CHANNEL_MVT);
382387
assert_eq!(u.primary_tag, Some(cln_rpc::notifications::CoinMovementPrimaryTag::INVOICE));
383388
assert_eq!(u.extra_tags, Some(vec!["keysend".to_string()]));
384-
let _pb: crate::pb::CoinMovementNotification = u.clone().into();
389+
let _pb: crate::pb::CoinMovementNotification = u.into();
385390

386391
// Also test chain_mvt with utxo
387392
let j2: serde_json::Value = json!({
@@ -423,17 +428,11 @@ fn test_channel_state_changed() {
423428
"message": "Lockin complete"
424429
});
425430
let u: cln_rpc::notifications::ChannelStateChangedNotification =
426-
serde_json::from_value(j.clone()).unwrap();
431+
serde_json::from_value(j).unwrap();
427432
assert_eq!(u.cause, cln_rpc::notifications::ChannelStateChangedCause::REMOTE);
428433
assert_eq!(u.message, Some("Lockin complete".to_string()));
429-
let _pb: crate::pb::ChannelStateChangedNotification = u.clone().into();
430-
431-
// Serde round-trip
432-
let v = serde_json::to_value(u.clone()).unwrap();
433-
let u2: cln_rpc::notifications::ChannelStateChangedNotification =
434-
serde_json::from_value(v.clone()).unwrap();
435-
let v2 = serde_json::to_value(u2).unwrap();
436-
assert_eq!(v, v2);
434+
assert_serde_roundtrip!(u, cln_rpc::notifications::ChannelStateChangedNotification);
435+
let _pb: crate::pb::ChannelStateChangedNotification = u.into();
437436

438437
// Also test without optional fields
439438
let j_minimal: serde_json::Value = json!({
@@ -471,11 +470,11 @@ fn test_forward_event() {
471470
"style": "tlv"
472471
});
473472
let u: cln_rpc::notifications::ForwardEventNotification =
474-
serde_json::from_value(j.clone()).unwrap();
473+
serde_json::from_value(j).unwrap();
475474
assert_eq!(u.status, cln_rpc::notifications::ForwardEventStatus::SETTLED);
476475
assert_eq!(u.style, Some(cln_rpc::notifications::ForwardEventStyle::TLV));
477476
assert!(u.out_channel.is_some());
478-
let _pb: crate::pb::ForwardEventNotification = u.clone().into();
477+
let _pb: crate::pb::ForwardEventNotification = u.into();
479478

480479
// Failed forward with failure info
481480
let j_failed: serde_json::Value = json!({
@@ -524,7 +523,7 @@ fn test_sendpay_failure() {
524523
}
525524
});
526525
let u: cln_rpc::notifications::SendPayFailureNotification =
527-
serde_json::from_value(j.clone()).unwrap();
526+
serde_json::from_value(j).unwrap();
528527
assert_eq!(u.code, 204);
529528
assert_eq!(
530529
u.data.status,
@@ -555,19 +554,13 @@ fn test_sendpay_success() {
555554
"label": "test-payment-1"
556555
});
557556
let u: cln_rpc::notifications::SendPaySuccessNotification =
558-
serde_json::from_value(j.clone()).unwrap();
557+
serde_json::from_value(j).unwrap();
559558
assert_eq!(u.status, cln_rpc::notifications::SendpaySuccessStatus::COMPLETE);
560559
assert_eq!(u.id, 1);
561560
assert_eq!(u.groupid, 1);
562561
assert!(u.payment_preimage.is_some());
563-
let _pb: crate::pb::SendPaySuccessNotification = u.clone().into();
564-
565-
// Serde round-trip
566-
let v = serde_json::to_value(u.clone()).unwrap();
567-
let u2: cln_rpc::notifications::SendPaySuccessNotification =
568-
serde_json::from_value(v.clone()).unwrap();
569-
let v2 = serde_json::to_value(u2).unwrap();
570-
assert_eq!(v, v2);
562+
assert_serde_roundtrip!(u, cln_rpc::notifications::SendPaySuccessNotification);
563+
let _pb: crate::pb::SendPaySuccessNotification = u.into();
571564
}
572565

573566
#[test]
@@ -580,17 +573,11 @@ fn test_warning() {
580573
"log": "Something unexpected happened"
581574
});
582575
let u: cln_rpc::notifications::WarningNotification =
583-
serde_json::from_value(j.clone()).unwrap();
576+
serde_json::from_value(j).unwrap();
584577
assert_eq!(u.level, cln_rpc::notifications::WarningLevel::WARN);
585578
assert_eq!(u.source, "lightningd(1234)");
586-
let _pb: crate::pb::WarningNotification = u.clone().into();
587-
588-
// Serde round-trip
589-
let v = serde_json::to_value(u.clone()).unwrap();
590-
let u2: cln_rpc::notifications::WarningNotification =
591-
serde_json::from_value(v.clone()).unwrap();
592-
let v2 = serde_json::to_value(u2).unwrap();
593-
assert_eq!(v, v2);
579+
assert_serde_roundtrip!(u, cln_rpc::notifications::WarningNotification);
580+
let _pb: crate::pb::WarningNotification = u.into();
594581

595582
// Test error level
596583
let j_err: serde_json::Value = json!({
@@ -620,18 +607,12 @@ fn test_pay_part_end() {
620607
}
621608
});
622609
let u: cln_rpc::notifications::PayPartEndNotification =
623-
serde_json::from_value(j.clone()).unwrap();
610+
serde_json::from_value(j).unwrap();
624611
assert_eq!(u.origin, "xpay");
625612
assert_eq!(u.payload.status, cln_rpc::notifications::PayPartEndPayloadStatus::SUCCESS);
626613
assert!(u.payload.failed_node_id.is_none());
627-
let _pb: crate::pb::PayPartEndNotification = u.clone().into();
628-
629-
// Serde round-trip
630-
let v = serde_json::to_value(u.clone()).unwrap();
631-
let u2: cln_rpc::notifications::PayPartEndNotification =
632-
serde_json::from_value(v.clone()).unwrap();
633-
let v2 = serde_json::to_value(u2).unwrap();
634-
assert_eq!(v, v2);
614+
assert_serde_roundtrip!(u, cln_rpc::notifications::PayPartEndNotification);
615+
let _pb: crate::pb::PayPartEndNotification = u.into();
635616

636617
// Failure case with error details
637618
let j_fail: serde_json::Value = json!({

0 commit comments

Comments
 (0)