mirror of https://github.com/lukechilds/polar.git
jamaljsr
5 years ago
9 changed files with 264 additions and 17 deletions
@ -0,0 +1,60 @@ |
|||||
|
import React from 'react'; |
||||
|
import { wait } from '@testing-library/react'; |
||||
|
import { LndNode } from 'shared/types'; |
||||
|
import * as files from 'utils/files'; |
||||
|
import { getNetwork, renderWithProviders } from 'utils/tests'; |
||||
|
import { ConnectionInfo } from '../ConnectTab'; |
||||
|
import { EncodedStrings } from './'; |
||||
|
|
||||
|
jest.mock('utils/files'); |
||||
|
|
||||
|
const filesMock = files as jest.Mocked<typeof files>; |
||||
|
|
||||
|
describe('EncodedStrings', () => { |
||||
|
const network = getNetwork(); |
||||
|
|
||||
|
const renderComponent = ( |
||||
|
credentials: ConnectionInfo['credentials'], |
||||
|
encoding: 'hex' | 'base64', |
||||
|
) => { |
||||
|
const cmp = <EncodedStrings credentials={credentials} encoding={encoding} />; |
||||
|
return renderWithProviders(cmp); |
||||
|
}; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
filesMock.read.mockResolvedValue('file-content'); |
||||
|
}); |
||||
|
|
||||
|
it('should display credentials', async () => { |
||||
|
const lnd = network.nodes.lightning[0] as LndNode; |
||||
|
const lndCreds: ConnectionInfo['credentials'] = { |
||||
|
admin: lnd.paths.adminMacaroon, |
||||
|
readOnly: lnd.paths.readonlyMacaroon, |
||||
|
cert: lnd.paths.tlsCert, |
||||
|
}; |
||||
|
const { getByText } = renderComponent(lndCreds, 'hex'); |
||||
|
await wait(); |
||||
|
expect(getByText('TLS Cert')).toBeInTheDocument(); |
||||
|
expect(getByText('Admin Macaroon')).toBeInTheDocument(); |
||||
|
expect(getByText('Read-only Macaroon')).toBeInTheDocument(); |
||||
|
expect(filesMock.read).toBeCalledWith(expect.stringContaining('tls.cert'), 'hex'); |
||||
|
expect(filesMock.read).toBeCalledWith( |
||||
|
expect.stringContaining('admin.macaroon'), |
||||
|
'hex', |
||||
|
); |
||||
|
expect(filesMock.read).toBeCalledWith( |
||||
|
expect.stringContaining('readonly.macaroon'), |
||||
|
'hex', |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
it('should handle all missing credentials', async () => { |
||||
|
const missingCreds = {} as ConnectionInfo['credentials']; |
||||
|
const { queryByText } = renderComponent(missingCreds, 'hex'); |
||||
|
await wait(); |
||||
|
expect(queryByText('TLS Cert')).not.toBeInTheDocument(); |
||||
|
expect(queryByText('Admin Macaroon')).not.toBeInTheDocument(); |
||||
|
expect(queryByText('Read-only Macaroon')).not.toBeInTheDocument(); |
||||
|
expect(filesMock.read).not.toBeCalled(); |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,42 @@ |
|||||
|
import { EclairNode } from 'shared/types'; |
||||
|
import * as ipc from 'lib/ipc/ipcService'; |
||||
|
import { getNetwork } from 'utils/tests'; |
||||
|
import { httpPost } from './eclairApi'; |
||||
|
|
||||
|
jest.mock('lib/ipc/ipcService'); |
||||
|
|
||||
|
const ipcMock = ipc as jest.Mocked<typeof ipc>; |
||||
|
|
||||
|
describe('EclairApi', () => { |
||||
|
const node = getNetwork().nodes.lightning[2] as EclairNode; |
||||
|
|
||||
|
it('should throw an error for an incorrect node implementation', async () => { |
||||
|
const lnd = getNetwork().nodes.lightning[0]; |
||||
|
await expect(httpPost(lnd, 'get-ok')).rejects.toThrow( |
||||
|
"EclairService cannot be used for 'LND' nodes", |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
it('should perform an unsuccessful httpPost', async () => { |
||||
|
const sender = jest.fn().mockRejectedValue(new Error('api-error')); |
||||
|
ipcMock.createIpcSender.mockReturnValue(sender); |
||||
|
await expect(httpPost(node, 'getinfo')).rejects.toThrow('api-error'); |
||||
|
}); |
||||
|
|
||||
|
it('should perform a successful httpPost', async () => { |
||||
|
const sender = jest.fn().mockResolvedValue('asdf'); |
||||
|
ipcMock.createIpcSender.mockReturnValue(sender); |
||||
|
await expect(httpPost(node, 'getinfo')).resolves.toBe('asdf'); |
||||
|
expect(sender).toBeCalledWith( |
||||
|
'http', |
||||
|
expect.objectContaining({ |
||||
|
url: 'http://127.0.0.1:8283/getinfo', |
||||
|
method: 'POST', |
||||
|
headers: { |
||||
|
'Content-Type': 'application/x-www-form-urlencoded', |
||||
|
Authorization: 'Basic OmVjbGFpcnB3', |
||||
|
}, |
||||
|
}), |
||||
|
); |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,85 @@ |
|||||
|
import { WalletInfo } from 'bitcoin-core'; |
||||
|
import bitcoindService from 'lib/bitcoin/bitcoindService'; |
||||
|
import { defaultStateBalances, defaultStateInfo, getNetwork } from 'utils/tests'; |
||||
|
import { eclairService } from './'; |
||||
|
import * as eclairApi from './eclairApi'; |
||||
|
import * as ELN from './types'; |
||||
|
|
||||
|
jest.mock('./eclairApi'); |
||||
|
jest.mock('lib/bitcoin/bitcoindService'); |
||||
|
|
||||
|
const eclairApiMock = eclairApi as jest.Mocked<typeof eclairApi>; |
||||
|
const bitcoindServiceMock = bitcoindService as jest.Mocked<typeof bitcoindService>; |
||||
|
|
||||
|
describe('EclairService', () => { |
||||
|
const network = getNetwork(); |
||||
|
const node = network.nodes.lightning[2]; |
||||
|
const backend = network.nodes.bitcoin[0]; |
||||
|
|
||||
|
it('should get node info', async () => { |
||||
|
const infoResponse: Partial<ELN.GetInfoResponse> = { |
||||
|
nodeId: 'asdf', |
||||
|
alias: '', |
||||
|
publicAddresses: ['1.1.1.1:9735'], |
||||
|
blockHeight: 0, |
||||
|
}; |
||||
|
eclairApiMock.httpPost.mockResolvedValue(infoResponse); |
||||
|
const expected = defaultStateInfo({ |
||||
|
pubkey: 'asdf', |
||||
|
rpcUrl: 'asdf@1.1.1.1:9735', |
||||
|
syncedToChain: true, |
||||
|
}); |
||||
|
const actual = await eclairService.getInfo(node); |
||||
|
expect(actual).toEqual(expected); |
||||
|
}); |
||||
|
|
||||
|
it('should get wallet balance', async () => { |
||||
|
const ballanceResponse: Partial<WalletInfo> = { |
||||
|
balance: 0.00001, |
||||
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
|
unconfirmed_balance: 0, |
||||
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
|
immature_balance: 0, |
||||
|
}; |
||||
|
bitcoindServiceMock.getWalletInfo.mockResolvedValue(ballanceResponse as any); |
||||
|
|
||||
|
const expected = defaultStateBalances({ confirmed: '1000', total: '1000' }); |
||||
|
const actual = await eclairService.getBalances(node, backend); |
||||
|
expect(actual).toEqual(expected); |
||||
|
}); |
||||
|
|
||||
|
it('should get new address', async () => { |
||||
|
const expected = { address: 'abcdef' }; |
||||
|
eclairApiMock.httpPost.mockResolvedValue(expected.address); |
||||
|
const actual = await eclairService.getNewAddress(node); |
||||
|
expect(actual).toEqual(expected); |
||||
|
}); |
||||
|
|
||||
|
it('should get a list of channels', async () => { |
||||
|
const chanResponse: ELN.ChannelResponse = { |
||||
|
nodeId: 'abcdef', |
||||
|
channelId: '65sdfd7', |
||||
|
state: ELN.ChannelState.NORMAL, |
||||
|
data: { |
||||
|
commitments: { |
||||
|
localParams: { |
||||
|
isFunder: true, |
||||
|
}, |
||||
|
localCommit: { |
||||
|
spec: { |
||||
|
toLocal: 100000000, |
||||
|
toRemote: 50000000, |
||||
|
}, |
||||
|
}, |
||||
|
commitInput: { |
||||
|
amountSatoshis: 150000, |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
}; |
||||
|
eclairApiMock.httpPost.mockResolvedValue([chanResponse]); |
||||
|
const expected = [expect.objectContaining({ pubkey: 'abcdef' })]; |
||||
|
const actual = await eclairService.getChannels(node); |
||||
|
expect(actual).toEqual(expected); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue