paid-services
Version:
Lightning Paid Services library
89 lines (74 loc) • 2.39 kB
JavaScript
const {deepStrictEqual, rejects} = require('node:assert/strict');
const {test} = require('node:test');
const {encodeTlvStream} = require('bolt01');
const {makeInvoice} = require('mock-lnd');
const {makeInvoiceSubscription} = require('mock-lnd');
const {makeLnd} = require('mock-lnd');
const {makePaySubscription} = require('mock-lnd');
const {makePayViaRoutesResponse} = require('mock-lnd');
const messagesForResponse = require('./../../respond/messages_for_response');
const method = require('./../../client/make_service_request');
const responseForSchema = require('./../../services/response_for_schema');
const encode = records => encodeTlvStream({records: [records]}).encoded;
const id1 = Buffer.alloc(32).toString('hex');
const id2 = Buffer.alloc(32, 1).toString('hex');
const makeArgs = overrides => {
let requests = 0;
const lnd = makeLnd({
payViaRoutes: ({}, cbk) => {
requests++;
if (requests === 2) {
return cbk(null, makePayViaRoutesResponse({}));
}
return cbk(null, makePayViaRoutesResponse({is_unknown_failure: true}));
},
subscribeToInvoice: makeInvoiceSubscription({
invoice: makeInvoice({
is_confirmed: true,
is_push: true,
payments: [{
is_confirmed: true,
messages: messagesForResponse({
records: [
{type: '1', value: '04'},
{type: '2', value: Buffer.from('description').toString('hex')},
],
}).messages,
}],
}),
}),
});
const args = {
lnd,
id: Buffer.alloc(32).toString('hex'),
ms: 1000,
network: 'btc',
node: Buffer.alloc(33, 3).toString('hex'),
};
Object.keys(overrides).forEach(k => args[k] = overrides[k]);
return args;
};
const tests = [
{
args: makeArgs({id: undefined}),
description: 'Waiting for a response requires a service id',
error: [400, 'ExpectedServiceIdNumberToMakeServiceRequest'],
},
];
tests.forEach(({args, description, error, expected}) => {
test(description, async () => {
if (!!error) {
await rejects(
method(args),
err => {
deepStrictEqual(err, error, 'Got expected error');
return true;
},
'Got expected error'
);
} else {
const res = await method(args);
deepStrictEqual(res, expected, 'Got expected result');
}
});
});