|
| 1 | +import chai from 'chai' |
| 2 | +import sinon from 'sinon' |
| 3 | +import sinonChai from 'sinon-chai' |
| 4 | + |
| 5 | +import { InvoiceStatus, InvoiceUnit } from '../../../src/@types/invoice' |
| 6 | +import { LNbitsPaymentsProcessor } from '../../../src/payments-processors/lnbits-payment-processor' |
| 7 | + |
| 8 | +chai.use(sinonChai) |
| 9 | + |
| 10 | +const { expect } = chai |
| 11 | + |
| 12 | +const invoiceResponse = (overrides: any = {}) => ({ |
| 13 | + data: { |
| 14 | + paid: false, |
| 15 | + details: { |
| 16 | + payment_hash: 'lnbits-payment-hash', |
| 17 | + extra: { |
| 18 | + internalId: 'a'.repeat(64), |
| 19 | + }, |
| 20 | + bolt11: 'lnbc1test', |
| 21 | + amount: 42000, |
| 22 | + memo: 'LNbits test invoice', |
| 23 | + time: Math.floor(Date.now() / 1000), |
| 24 | + expiry: Math.floor((Date.now() + 600000) / 1000), |
| 25 | + ...overrides.details, |
| 26 | + }, |
| 27 | + ...overrides.data, |
| 28 | + }, |
| 29 | +}) |
| 30 | + |
| 31 | +describe('LNbitsPaymentsProcessor', () => { |
| 32 | + const makeProcessor = (response: any) => { |
| 33 | + const httpClient = { |
| 34 | + get: sinon.stub().resolves(response), |
| 35 | + } |
| 36 | + |
| 37 | + return { |
| 38 | + processor: new LNbitsPaymentsProcessor(httpClient as any, (() => ({})) as any), |
| 39 | + httpClient, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + describe('getInvoice', () => { |
| 44 | + it('returns PENDING for unpaid invoices that have not expired', async () => { |
| 45 | + const { processor } = makeProcessor(invoiceResponse()) |
| 46 | + |
| 47 | + const invoice = await processor.getInvoice('lnbits-payment-hash') |
| 48 | + |
| 49 | + expect(invoice.status).to.equal(InvoiceStatus.PENDING) |
| 50 | + expect(invoice.unit).to.equal(InvoiceUnit.SATS) |
| 51 | + }) |
| 52 | + |
| 53 | + it('returns EXPIRED for unpaid invoices past their LNbits expiry time', async () => { |
| 54 | + const { processor } = makeProcessor( |
| 55 | + invoiceResponse({ |
| 56 | + details: { |
| 57 | + expiry: Math.floor((Date.now() - 60000) / 1000), |
| 58 | + }, |
| 59 | + }), |
| 60 | + ) |
| 61 | + |
| 62 | + const invoice = await processor.getInvoice('lnbits-payment-hash') |
| 63 | + |
| 64 | + expect(invoice.status).to.equal(InvoiceStatus.EXPIRED) |
| 65 | + }) |
| 66 | + |
| 67 | + it('keeps paid invoices COMPLETED even if the expiry time has passed', async () => { |
| 68 | + const { processor } = makeProcessor( |
| 69 | + invoiceResponse({ |
| 70 | + data: { |
| 71 | + paid: true, |
| 72 | + }, |
| 73 | + details: { |
| 74 | + expiry: Math.floor((Date.now() - 60000) / 1000), |
| 75 | + }, |
| 76 | + }), |
| 77 | + ) |
| 78 | + |
| 79 | + const invoice = await processor.getInvoice('lnbits-payment-hash') |
| 80 | + |
| 81 | + expect(invoice.status).to.equal(InvoiceStatus.COMPLETED) |
| 82 | + expect(invoice.amountPaid).to.equal(42n) |
| 83 | + }) |
| 84 | + }) |
| 85 | +}) |
0 commit comments