mirror of https://github.com/lukechilds/polar.git
jamaljsr
5 years ago
7 changed files with 118 additions and 48 deletions
@ -0,0 +1,55 @@ |
|||
import { ipcChannels } from 'shared'; |
|||
import { IpcSender } from 'lib/ipc/ipcService'; |
|||
import { getNetwork } from 'utils/tests'; |
|||
import lndProxyClient from './lndProxyClient'; |
|||
|
|||
describe('LndService', () => { |
|||
const node = getNetwork().nodes.lightning[0]; |
|||
let actualIpc: IpcSender; |
|||
|
|||
beforeEach(() => { |
|||
actualIpc = lndProxyClient.ipc; |
|||
// mock the ipc dependency
|
|||
lndProxyClient.ipc = jest.fn(); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
// restore the actual ipc implementation
|
|||
lndProxyClient.ipc = actualIpc; |
|||
}); |
|||
|
|||
it('should call the getInfo ipc', () => { |
|||
lndProxyClient.getInfo(node); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.getInfo, { node }); |
|||
}); |
|||
|
|||
it('should call the getWalletBalance ipc', () => { |
|||
lndProxyClient.getWalletBalance(node); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.walletBalance, { node }); |
|||
}); |
|||
|
|||
it('should call the getNewAddress ipc', () => { |
|||
lndProxyClient.getNewAddress(node); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.newAddress, { node }); |
|||
}); |
|||
|
|||
it('should call the listPeers ipc', () => { |
|||
lndProxyClient.listPeers(node); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.listPeers, { node }); |
|||
}); |
|||
|
|||
it('should call the connectPeer ipc', () => { |
|||
const req = { addr: { pubkey: 'abcdef', host: 'lnd-1' } }; |
|||
lndProxyClient.connectPeer(node, req); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.connectPeer, { node, req }); |
|||
}); |
|||
|
|||
it('should call the openChannel ipc', () => { |
|||
const req = { |
|||
nodePubkeyString: 'asdf', |
|||
localFundingAmount: '1000', |
|||
}; |
|||
lndProxyClient.openChannel(node, req); |
|||
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.openChannel, { node, req }); |
|||
}); |
|||
}); |
@ -1,52 +1,76 @@ |
|||
import { IpcSender } from 'lib/ipc/ipcService'; |
|||
import { getNetwork } from 'utils/tests'; |
|||
import lndProxyClient from './lndProxyClient'; |
|||
import lndService from './lndService'; |
|||
|
|||
jest.mock('./lndProxyClient'); |
|||
|
|||
describe('LndService', () => { |
|||
const node = getNetwork().nodes.lightning[0]; |
|||
let actualIpc: IpcSender; |
|||
const [node, node2] = getNetwork().nodes.lightning; |
|||
|
|||
beforeEach(() => { |
|||
actualIpc = lndService.ipc; |
|||
// mock the ipc dependency
|
|||
lndService.ipc = jest.fn(); |
|||
it('should get node info', async () => { |
|||
const expected = { identityPubkey: 'asdf' }; |
|||
lndProxyClient.getInfo = jest.fn().mockResolvedValue(expected); |
|||
const actual = await lndService.getInfo(node); |
|||
expect(actual).toEqual(expected); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
// restore the actual ipc implementation
|
|||
lndService.ipc = actualIpc; |
|||
it('should get wallet balance', async () => { |
|||
const expected = { confirmedBalance: '1000' }; |
|||
lndProxyClient.getWalletBalance = jest.fn().mockResolvedValue(expected); |
|||
const actual = await lndService.getWalletBalance(node); |
|||
expect(actual).toEqual(expected); |
|||
}); |
|||
|
|||
it('should call the getInfo ipc', () => { |
|||
lndService.getInfo(node); |
|||
expect(lndService.ipc).toBeCalledWith('get-info', { node }); |
|||
it('should get new address', async () => { |
|||
const expected = { address: 'abcdef' }; |
|||
lndProxyClient.getNewAddress = jest.fn().mockResolvedValue(expected); |
|||
const actual = await lndService.getNewAddress(node); |
|||
expect(actual).toEqual(expected); |
|||
}); |
|||
|
|||
it('should call the getWalletBalance ipc', () => { |
|||
lndService.getWalletBalance(node); |
|||
expect(lndService.ipc).toBeCalledWith('wallet-balance', { node }); |
|||
}); |
|||
describe('openChannel', () => { |
|||
it('should open the channel successfully', async () => { |
|||
lndProxyClient.getInfo = jest.fn().mockResolvedValue({ identityPubkey: 'asdf' }); |
|||
lndProxyClient.listPeers = jest.fn().mockResolvedValue({ |
|||
peers: [{ pubKey: 'asdf' }], |
|||
}); |
|||
const expected = { fundingTxidStr: 'xyz' }; |
|||
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected); |
|||
const actual = await lndService.openChannel(node, node2, '1000'); |
|||
expect(actual).toEqual(expected); |
|||
expect(lndProxyClient.getInfo).toBeCalledTimes(1); |
|||
expect(lndProxyClient.listPeers).toBeCalledTimes(1); |
|||
expect(lndProxyClient.connectPeer).toBeCalledTimes(0); |
|||
}); |
|||
|
|||
it('should call the getNewAddress ipc', () => { |
|||
lndService.getNewAddress(node); |
|||
expect(lndService.ipc).toBeCalledWith('new-address', { node }); |
|||
it('should connect peer then open the channel', async () => { |
|||
lndProxyClient.getInfo = jest.fn().mockResolvedValue({ identityPubkey: 'asdf' }); |
|||
lndProxyClient.listPeers = jest.fn().mockResolvedValue({ |
|||
peers: [{ pubKey: 'fdsa' }], |
|||
}); |
|||
const expected = { fundingTxidStr: 'xyz' }; |
|||
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected); |
|||
const actual = await lndService.openChannel(node, node2, '1000'); |
|||
expect(actual).toEqual(expected); |
|||
expect(lndProxyClient.getInfo).toBeCalledTimes(1); |
|||
expect(lndProxyClient.listPeers).toBeCalledTimes(1); |
|||
expect(lndProxyClient.connectPeer).toBeCalledTimes(1); |
|||
}); |
|||
}); |
|||
|
|||
describe('waitUntilOnline', () => { |
|||
it('should return true when successful', async () => { |
|||
const ipc = lndService.ipc as jest.Mock; |
|||
ipc.mockResolvedValue({}); |
|||
lndProxyClient.getInfo = jest.fn().mockResolvedValue({}); |
|||
const result = await lndService.waitUntilOnline(node); |
|||
expect(result).toBe(true); |
|||
expect(ipc).toBeCalledTimes(1); |
|||
expect(lndProxyClient.getInfo).toBeCalledTimes(1); |
|||
}); |
|||
|
|||
it('should return false on failure', async () => { |
|||
const ipc = lndService.ipc as jest.Mock; |
|||
ipc.mockRejectedValue(new Error()); |
|||
lndProxyClient.getInfo = jest.fn().mockRejectedValue(new Error()); |
|||
const result = await lndService.waitUntilOnline(node, 0.5, 1); |
|||
expect(result).toBe(false); |
|||
expect(ipc).toBeCalledTimes(5); |
|||
expect(lndProxyClient.getInfo).toBeCalledTimes(5); |
|||
}); |
|||
}); |
|||
}); |
|||
|
Loading…
Reference in new issue