From 1a9f591b2b8d9de57f4c7b34ec41dbb4b8609c46 Mon Sep 17 00:00:00 2001 From: MPins Date: Tue, 22 Jul 2025 23:36:33 -0300 Subject: [PATCH] itest: add the test for DeleteCanceledInvoice --- itest/list_on_test.go | 4 + itest/lnd_delete_canceled_invoice_test.go | 99 +++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 itest/lnd_delete_canceled_invoice_test.go diff --git a/itest/list_on_test.go b/itest/list_on_test.go index 98198d72a..06a861e82 100644 --- a/itest/list_on_test.go +++ b/itest/list_on_test.go @@ -723,6 +723,10 @@ var allTestCases = []*lntest.TestCase{ Name: "grpc not found", TestFunc: testGRPCNotFound, }, + { + Name: "delete canceled invoice", + TestFunc: testDeleteCanceledInvoice, + }, } // appendPrefixed is used to add a prefix to each test name in the subtests diff --git a/itest/lnd_delete_canceled_invoice_test.go b/itest/lnd_delete_canceled_invoice_test.go new file mode 100644 index 000000000..357433414 --- /dev/null +++ b/itest/lnd_delete_canceled_invoice_test.go @@ -0,0 +1,99 @@ +package itest + +import ( + "bytes" + + "github.com/lightningnetwork/lnd/invoices" + "github.com/lightningnetwork/lnd/lnrpc" + "github.com/lightningnetwork/lnd/lntest" + "github.com/lightningnetwork/lnd/lntypes" + "github.com/stretchr/testify/require" +) + +func testDeleteCanceledInvoice(ht *lntest.HarnessTest) { + bob := ht.NewNode("bob", nil) + + // Create 2 invoices for Bob. + const numInvoices = 2 + invoiceResp := make([]*lnrpc.AddInvoiceResponse, numInvoices) + + for i := range numInvoices { + // Create a unique 32-byte preimage for each invoice. + preimage := bytes.Repeat([]byte{byte(i + 1)}, 32) + + invoice := &lnrpc.Invoice{ + RPreimage: preimage, + Value: paymentAmt, + } + + invoiceResp[i] = bob.RPC.AddInvoice(invoice) + } + + // Cancel all invoices except the last one. + for i := range numInvoices - 1 { + bob.RPC.CancelInvoice(invoiceResp[i].RHash) + } + + // Let's assert an error while providing no invoice hash. + bob.RPC.DeleteCanceledInvoiceAssertErr( + &lnrpc.DelCanceledInvoiceReq{}, + invoices.ErrNoInvoiceHash.Error(), + ) + + // Let's assert an error while providing a hash with an invalid length. + bob.RPC.DeleteCanceledInvoiceAssertErr( + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: "bb02fbfa62983b6b62"}, + "invalid hash string length", + ) + + // Let's assert an error is returned for a hash with invalid encoding. + bob.RPC.DeleteCanceledInvoiceAssertErr( + &lnrpc.DelCanceledInvoiceReq{ + InvoiceHash: "bb02fbfa62983b6b621376bf8230732dd3a6dce" + + "a9f5df803c0935ae6ce7440dg", + }, "encoding/hex: invalid byte", + ) + + // Let's delete one canceled invoice. + invoiceToDelete := lntypes.Hash(invoiceResp[0].RHash).String() + resp := bob.RPC.DeleteCanceledInvoice( + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: invoiceToDelete}, + ) + + require.Contains( + ht, resp.String(), "canceled invoice deleted successfully", + ) + + respList := bob.RPC.ListInvoices(&lnrpc.ListInvoiceRequest{}) + + // Assert that the invoice was deleted. + require.Len(ht, respList.Invoices, numInvoices-1) + + // Let's assert an error while deleting a non-existent invoice. + bob.RPC.DeleteCanceledInvoiceAssertErr( + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: invoiceToDelete}, + invoices.ErrInvoiceNotFound.Error(), + ) + + // Let's assert an error while deleting a non canceled invoice. + notCanceledInvoice := lntypes.Hash(invoiceResp[numInvoices-1].RHash). + String() + + bob.RPC.DeleteCanceledInvoiceAssertErr( + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: notCanceledInvoice}, + invoices.ErrInvoiceNotCanceled.Error(), + ) + + // Assert that the last invoice in the invoiceResp is still present and + // OPEN. + respList = bob.RPC.ListInvoices(&lnrpc.ListInvoiceRequest{}) + require.Equal( + ht, invoiceResp[numInvoices-1].RHash, + respList.Invoices[0].RHash, + ) + + require.Equal(ht, lnrpc.Invoice_OPEN, respList.Invoices[0].State) + + // Assert that just one invoice exists. + require.Len(ht, respList.Invoices, 1) +}