Skip to content

Commit

Permalink
test(tapd): update unit tests for paying asset invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
jamaljsr committed Oct 30, 2024
1 parent cb8fd8d commit 3e463c5
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LightningNodeChannelAsset } from 'lib/lightning/types';
import { Network } from 'types';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import { createNetwork, mapToTapd } from 'utils/network';
import {
defaultStateChannel,
getNetwork,
Expand Down Expand Up @@ -161,14 +161,15 @@ describe('CreateInvoiceModal', () => {
lightningServiceMock.getChannels.mockResolvedValue([
defaultStateChannel({ assets: [asset] }),
]);
lightningServiceMock.createInvoice.mockResolvedValue('lnbc1invoice');
lightningServiceMock.decodeInvoice.mockResolvedValue({
amountMsat: '20000',
expiry: '3600',
paymentHash: 'payment-hash',
});
tapServiceMock.assetRoots.mockResolvedValue([
{ id: 'abcd', name: 'test asset', rootSum: 100 },
]);
tapServiceMock.addAssetBuyOrder.mockResolvedValue({
scid: 'abcd',
askPrice: '100',
});
tapServiceMock.addInvoice.mockResolvedValue('lnbc1invoice');
});

it('should display the asset dropdown', async () => {
Expand Down Expand Up @@ -200,22 +201,14 @@ describe('CreateInvoiceModal', () => {
expect(await findByText('Successfully Created the Invoice')).toBeInTheDocument();
expect(getByDisplayValue('lnbc1invoice')).toBeInTheDocument();
const node = network.nodes.lightning[0];
expect(lightningServiceMock.createInvoice).toHaveBeenCalledWith(node, 200, '', {
msats: '20000',
nodeId: '',
scid: 'abcd',
});
});

it('should display a warning for large asset amounts', async () => {
const { getByLabelText, findByText, changeSelect } = await renderComponent();
expect(await findByText('Node')).toBeInTheDocument();
changeSelect('Asset to Receive', 'test asset');
fireEvent.change(getByLabelText('Amount'), { target: { value: '10000' } });
const warning =
'With the default mock exchange rate of 100 sats to 1 asset, ' +
'it is best to use amounts below 5,000 to reduce the chances of payment failures.';
expect(await findByText(warning)).toBeInTheDocument();
const tapNode = mapToTapd(node);
expect(tapServiceMock.addInvoice).toHaveBeenCalledWith(
tapNode,
'abcd',
200,
'',
3600,
);
});

it('should display an error when creating an asset invoice with a high balance', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('OpenChannelModal', () => {
lightningServiceMock.getBalances.mockResolvedValue(balances('0'));
const { findByText } = await renderComponent();
expect(
await findByText('Deposit enough funds to alice to open the channel'),
await findByText('Deposit enough sats to alice to fund the channel'),
).toBeInTheDocument();
});
});
Expand All @@ -203,7 +203,7 @@ describe('OpenChannelModal', () => {
it('should open a channel successfully', async () => {
const { getByText, getByLabelText, store } = await renderComponent('bob', 'alice');
fireEvent.change(getByLabelText('Capacity'), { target: { value: '1000' } });
fireEvent.click(getByLabelText('Deposit enough funds to bob to open the channel'));
fireEvent.click(getByLabelText('Deposit enough sats to bob to fund the channel'));
fireEvent.click(getByText('Open Channel'));
await waitFor(() => {
expect(store.getState().modals.openChannel.visible).toBe(false);
Expand All @@ -221,7 +221,7 @@ describe('OpenChannelModal', () => {
it('should open a private channel successfully', async () => {
const { getByText, getByLabelText, store } = await renderComponent('bob', 'alice');
fireEvent.change(getByLabelText('Capacity'), { target: { value: '1000' } });
fireEvent.click(getByLabelText('Deposit enough funds to bob to open the channel'));
fireEvent.click(getByLabelText('Deposit enough sats to bob to fund the channel'));
fireEvent.click(getByText('Make the channel private'));
fireEvent.click(getByText('Open Channel'));
await waitFor(() => {
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('OpenChannelModal', () => {
fireEvent.change(getByLabelText('Capacity'), { target: { value: '1000' } });
changeSelect('Destination', 'alice');
fireEvent.click(
await findByLabelText('Deposit enough funds to bob to open the channel'),
await findByLabelText('Deposit enough sats to bob to fund the channel'),
);
fireEvent.click(getByText('Open Channel'));
await waitFor(() => {
Expand Down Expand Up @@ -310,16 +310,11 @@ describe('OpenChannelModal', () => {
total: '300',
});
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
lightningServiceMock.createInvoice.mockResolvedValue('lnbc1invoice');
tapServiceMock.listBalances.mockResolvedValue([
defaultTapBalance({ id: 'abcd', name: 'test asset', balance: '1000' }),
defaultTapBalance({ id: 'efgh', name: 'other asset', balance: '5000' }),
]);
tapServiceMock.syncUniverse.mockResolvedValue({ syncedUniverses: [] });
tapServiceMock.addAssetBuyOrder.mockResolvedValue({
scid: 'abcd',
askPrice: '100',
});
});

it('should display the asset dropdown', async () => {
Expand Down
25 changes: 11 additions & 14 deletions src/components/designer/lightning/actions/PayInvoiceModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LightningNodeChannelAsset } from 'lib/lightning/types';
import { Network } from 'types';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import { createNetwork, mapToTapd } from 'utils/network';
import {
defaultStateChannel,
getNetwork,
Expand Down Expand Up @@ -163,18 +163,13 @@ describe('PayInvoiceModal', () => {
amountMsat: '400000',
expiry: '123456',
});
lightningServiceMock.payInvoice.mockResolvedValue({
preimage: 'preimage',
amount: 1000,
destination: 'asdf',
});
tapServiceMock.assetRoots.mockResolvedValue([
{ id: 'abcd', name: 'test asset', rootSum: 100 },
]);
tapServiceMock.addAssetSellOrder.mockResolvedValue({
id: 'abcd',
bidPrice: '100',
scid: '12345',
tapServiceMock.sendPayment.mockResolvedValue({
preimage: 'preimage',
amount: 1000,
destination: 'asdf',
});
});

Expand All @@ -195,11 +190,13 @@ describe('PayInvoiceModal', () => {
expect(store.getState().modals.payInvoice.visible).toBe(false);
});
const node = network.nodes.lightning[1];
expect(lightningServiceMock.payInvoice).toHaveBeenCalledWith(
node,
const tapdNode = mapToTapd(node);
expect(tapServiceMock.sendPayment).toHaveBeenCalledWith(
tapdNode,
'abcd',
'lnbc1',
400,
undefined,
400000,
'',
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('MintAssetModal', () => {
total: '0',
});
const { findByText, getByText } = await renderComponent();
fireEvent.click(getByText('Deposit enough funds to alice'));
fireEvent.click(getByText('Deposit enough sats to alice to pay on-chain fees'));
expect(
await findByText('Insufficient balance on lnd node alice'),
).toBeInTheDocument();
Expand Down
10 changes: 6 additions & 4 deletions src/components/designer/tap/actions/SendAssetModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ describe('SendAssetModal', () => {
expect(
await findByText('Insufficient balance on lnd node alice'),
).toBeInTheDocument();
expect(getByText('Deposit enough funds to alice')).toBeInTheDocument();
expect(
getByText('Deposit enough sats to alice to pay on-chain fees'),
).toBeInTheDocument();
});

it('should disable the alert when auto deposit is enabled', async () => {
const { queryByText, getByText } = await renderComponent();
await waitFor(() => {
expect(lightningServiceMock.getBalances).toBeCalled();
});
fireEvent.click(getByText('Deposit enough funds to alice'));
fireEvent.click(getByText('Deposit enough sats to alice to pay on-chain fees'));
expect(
queryByText('Insufficient balance on lnd node alice'),
).not.toBeInTheDocument();
Expand All @@ -163,7 +165,7 @@ describe('SendAssetModal', () => {
await waitFor(() => {
expect(tapServiceMock.decodeAddress).toBeCalled();
});
fireEvent.click(getByText('Deposit enough funds to alice'));
fireEvent.click(getByText('Deposit enough sats to alice to pay on-chain fees'));

expect(getByText('Address Info')).toBeInTheDocument();
fireEvent.click(getByText('Send'));
Expand Down Expand Up @@ -199,7 +201,7 @@ describe('SendAssetModal', () => {
await waitFor(() => {
expect(tapServiceMock.decodeAddress).toBeCalled();
});
fireEvent.click(getByText('Deposit enough funds to carol'));
fireEvent.click(getByText('Deposit enough sats to carol to pay on-chain fees'));

expect(getByText('Address Info')).toBeInTheDocument();
fireEvent.click(getByText('Send'));
Expand Down
51 changes: 20 additions & 31 deletions src/lib/tap/tapd/tapProxyClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,46 +135,35 @@ describe('TapdProxyClient', () => {
});
});

it('should call the addAssetBuyOrder ipc', async () => {
const req: TAP.AddAssetBuyOrderRequestPartial = {
peerPubKey: 'A3C9nqQPL7Tp0PgFHNrMSz3tM3I+kiFK+/+us5C0o/2g',
assetSpecifier: { assetId: 'i0bx/3yDykK4sKGamaD8zPPPt/GoPeowpf3VUgAiV9o=' },
minAssetAmount: 25,
expiry: 1718091371,
timeoutSeconds: 60,
it('should call the addInvoice ipc', () => {
const req: TAP.tapchannelrpc.AddInvoiceRequestPartial = {
assetId: 'test asset id',
assetAmount: '1000',
peerPubkey: 'test peer pubkey',
invoiceRequest: {
paymentRequest: 'lnbc1test',
},
};
tapdProxyClient.addAssetBuyOrder(node, req);
expect(tapdProxyClient.ipc).toHaveBeenCalledWith(ipcChannels.tapd.addAssetBuyOrder, {
tapdProxyClient.addInvoice(node, req);
expect(tapdProxyClient.ipc).toHaveBeenCalledWith(ipcChannels.tapd.addInvoice, {
node,
req,
});
});

it('should call the addAssetSellOrder ipc', async () => {
const req: TAP.AddAssetSellOrderRequestPartial = {
peerPubKey: 'A812P5AqcMrifQcBL+fx4jUyOJMmdvH13uLpe9ctKrJV',
assetSpecifier: { assetId: 'i0bx/3yDykK4sKGamaD8zPPPt/GoPeowpf3VUgAiV9o=' },
minAsk: '2500000',
maxAssetAmount: '225',
expiry: '86400',
timeoutSeconds: 60,
it('should call the sendPayment ipc', () => {
const req: TAP.tapchannelrpc.SendPaymentRequestPartial = {
assetId: 'test asset id',
assetAmount: '1000',
peerPubkey: 'test peer pubkey',
paymentRequest: {
paymentRequest: 'lnbc1test',
},
};
tapdProxyClient.addAssetSellOrder(node, req);
expect(tapdProxyClient.ipc).toHaveBeenCalledWith(ipcChannels.tapd.addAssetSellOrder, {
tapdProxyClient.sendPayment(node, req);
expect(tapdProxyClient.ipc).toHaveBeenCalledWith(ipcChannels.tapd.sendPayment, {
node,
req,
});
});

it('should call the encodeCustomRecords ipc', async () => {
const req: TAP.EncodeCustomRecordsRequestPartial = {
routerSendPayment: { rfqId: 'n9Z7TIrXJBlcHplRtYFPds4hvGYFPIfF3Z3durWH/yo=' },
input: 'routerSendPayment',
};
tapdProxyClient.encodeCustomRecords(node, req);
expect(tapdProxyClient.ipc).toHaveBeenCalledWith(
ipcChannels.tapd.encodeCustomRecords,
{ node, req },
);
});
});
Loading

0 comments on commit 3e463c5

Please sign in to comment.