forked from jamaljsr/polar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightning.spec.ts
188 lines (170 loc) · 6.78 KB
/
lightning.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { createStore } from 'easy-peasy';
import { LndNode } from 'shared/types';
import {
LightningNodeBalances,
LightningNodeChannel,
LightningNodeInfo,
} from 'lib/lightning/types';
import { BitcoindLibrary } from 'types';
import * as asyncUtil from 'utils/async';
import { initChartFromNetwork } from 'utils/chart';
import {
defaultStateInfo,
getNetwork,
injections,
lightningServiceMock,
mockProperty,
} from 'utils/tests';
import appModel from './app';
import bitcoindModel from './bitcoind';
import designerModel from './designer';
import lightningModel from './lightning';
import networkModel from './network';
jest.mock('utils/async');
const asyncUtilMock = asyncUtil as jest.Mocked<typeof asyncUtil>;
const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;
describe('Lightning Model', () => {
const rootModel = {
app: appModel,
network: networkModel,
lightning: lightningModel,
bitcoind: bitcoindModel,
designer: designerModel,
};
const network = getNetwork();
const initialState = {
network: {
networks: [network],
},
designer: {
activeId: 1,
allCharts: {
1: initChartFromNetwork(network),
},
},
};
// initialize store for type inference
let store = createStore(rootModel, { injections, initialState });
const node = initialState.network.networks[0].nodes.lightning[0] as LndNode;
beforeEach(() => {
// reset the store before each test run
store = createStore(rootModel, { injections, initialState });
asyncUtilMock.delay.mockResolvedValue(Promise.resolve());
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
lightningServiceMock.getNewAddress.mockResolvedValue({ address: 'bc1aaaa' });
lightningServiceMock.getInfo.mockResolvedValue(
defaultStateInfo({
alias: 'my-node',
pubkey: 'abcdef',
syncedToChain: true,
}),
);
lightningServiceMock.getBalances.mockResolvedValue({
confirmed: '100',
unconfirmed: '200',
total: '300',
});
lightningServiceMock.getChannels.mockResolvedValueOnce([]);
});
it('should have a valid initial state', () => {
expect(store.getState().lightning.nodes).toEqual({});
});
it('should update state with getInfo response', async () => {
const { getInfo } = store.getActions().lightning;
await getInfo(node);
const nodeState = store.getState().lightning.nodes[node.name];
expect(nodeState.info).toBeDefined();
const info = nodeState.info as LightningNodeInfo;
expect(info.alias).toEqual('my-node');
expect(info.pubkey).toEqual('abcdef');
expect(info.syncedToChain).toEqual(true);
});
it('should update state with getBalance response', async () => {
const { getWalletBalance } = store.getActions().lightning;
await getWalletBalance(node);
const nodeState = store.getState().lightning.nodes[node.name];
expect(nodeState.walletBalance).toBeDefined();
const balances = nodeState.walletBalance as LightningNodeBalances;
expect(balances.confirmed).toEqual('100');
expect(balances.unconfirmed).toEqual('200');
expect(balances.total).toEqual('300');
});
it('should update state with getChannels response', async () => {
const { getChannels } = store.getActions().lightning;
await getChannels(node);
const nodeState = store.getState().lightning.nodes[node.name];
expect(nodeState.channels).toBeDefined();
const channels = nodeState.channels as LightningNodeChannel[];
expect(channels).toEqual([]);
});
it('should be able to deposit funds using the backend bitcoin node', async () => {
const { depositFunds } = store.getActions().lightning;
await depositFunds({ node, sats: '50000' });
const nodeState = store.getState().lightning.nodes[node.name];
expect(nodeState.walletBalance).toBeDefined();
const balances = nodeState.walletBalance as LightningNodeBalances;
expect(balances.confirmed).toEqual('100');
expect(balances.unconfirmed).toEqual('200');
expect(balances.total).toEqual('300');
});
it('should be able to deposit funds using the first bitcoin node', async () => {
const { depositFunds } = store.getActions().lightning;
const modifiedNode = { ...node, backendName: 'not-valid' };
await depositFunds({ node: modifiedNode, sats: '50000' });
const nodeState = store.getState().lightning.nodes[node.name];
expect(nodeState.walletBalance).toBeDefined();
const balances = nodeState.walletBalance as LightningNodeBalances;
expect(balances.confirmed).toEqual('100');
expect(balances.unconfirmed).toEqual('200');
expect(balances.total).toEqual('300');
});
it('should not throw an error when connecting peers', async () => {
const { connectAllPeers } = store.getActions().lightning;
lightningServiceMock.getInfo.mockResolvedValue(
defaultStateInfo({ alias: 'alice', pubkey: 'xyz', rpcUrl: 'asdf' }),
);
lightningServiceMock.getInfo.mockRejectedValueOnce(new Error('getInfo-error'));
await expect(connectAllPeers(network)).resolves.not.toThrow();
});
it('should open a channel successfully', async () => {
lightningServiceMock.getInfo.mockResolvedValueOnce(
defaultStateInfo({
pubkey: 'abcdef',
syncedToChain: true,
rpcUrl: '[email protected]:9735',
}),
);
const [from, to] = store.getState().network.networks[0].nodes.lightning;
const sats = '1000';
const { openChannel, getInfo } = store.getActions().lightning;
await getInfo(to);
await openChannel({ from, to, sats, autoFund: false, isPrivate: false });
expect(lightningServiceMock.getInfo).toBeCalledTimes(1);
expect(lightningServiceMock.openChannel).toBeCalledTimes(1);
expect(bitcoindServiceMock.mine).toBeCalledTimes(1);
});
it('should open a channel and mine on the first bitcoin node', async () => {
lightningServiceMock.getInfo.mockResolvedValueOnce(
defaultStateInfo({
pubkey: 'abcdef',
syncedToChain: true,
rpcUrl: '[email protected]:9735',
}),
);
const [from, to] = store.getState().network.networks[0].nodes.lightning;
from.backendName = 'invalid';
const sats = '1000';
const { openChannel, getInfo } = store.getActions().lightning;
await getInfo(to);
await openChannel({ from, to, sats, autoFund: false, isPrivate: false });
const btcNode = store.getState().network.networks[0].nodes.bitcoin[0];
expect(bitcoindServiceMock.mine).toBeCalledWith(6, btcNode);
});
it('should cause some delay waiting for nodes', async () => {
mockProperty(process, 'env', { NODE_ENV: 'production' } as any);
const { waitForNodes } = store.getActions().lightning;
await waitForNodes(network.nodes.lightning);
expect(asyncUtilMock.delay).toBeCalledWith(2000);
mockProperty(process, 'env', { NODE_ENV: 'test' } as any);
});
});