-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcall.test.js
More file actions
258 lines (247 loc) · 10.1 KB
/
Copy pathcall.test.js
File metadata and controls
258 lines (247 loc) · 10.1 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Command } from 'commander';
import AElf from 'aelf-sdk';
import inquirer from 'inquirer';
import { CallCommand } from '../../src/command';
import { callCommandUsages, callCommandParameters } from '../../src/utils/constants.js';
import { userHomeDir } from '../../src/utils/userHomeDir.js';
import { logger } from '../../src/utils/myLogger.js';
import { endpoint as endPoint, account, password, dataDir, csvDir, jsonDir } from '../constants.js';
const sampleRc = { getConfigs: jest.fn() };
jest.mock('../../src/utils/myLogger');
describe('CallCommand', () => {
let callCommand;
let backup, mockOraInstance;
const aelf = new AElf(new AElf.providers.HttpProvider(endPoint));
const wallet = AElf.wallet.getWalletByPrivateKey('943df6d39fd1e1cc6ae9813e54f7b9988cf952814f9c31e37744b52594cb4096');
const address = 'ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx';
beforeEach(() => {
backup = inquirer.prompt;
mockOraInstance = {
start: jest.fn(),
succeed: jest.fn(),
fail: jest.fn()
};
callCommand = new CallCommand(sampleRc);
callCommand.oraInstance = mockOraInstance;
});
test('with default params', async () => {
expect(callCommand.commandName).toBe('call');
expect(callCommand.parameters).toEqual(callCommandParameters);
expect(callCommand.description).toBe('Call a read-only method on a contract.');
expect(callCommand.options).toEqual([]);
expect(callCommand.usage).toEqual(callCommandUsages);
expect(callCommand.rc).toEqual(sampleRc);
});
test('should call method successfully', async () => {
const tokenContract = await aelf.chain.contractAt(address, wallet);
const method = tokenContract.GetTokenInfo;
const params = {
symbol: 'ELF'
};
const result = await callCommand.callMethod(method, params);
expect(mockOraInstance.start).toHaveBeenCalledWith('Calling method...');
expect(mockOraInstance.succeed).toHaveBeenCalledWith('Calling method successfully!');
});
test('should process address after prompt', async () => {
const answerInput = { contractAddress: address };
const result = await callCommand.processAddressAfterPrompt(aelf, wallet, answerInput);
expect(result.address).toBe(address);
});
test('should run with valid inputs', async () => {
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir]);
await callCommand.run(
commander,
'AElf.ContractNames.Token',
'GetTokenInfo',
JSON.stringify({
symbol: 'ELF'
})
);
expect(logger.info).toHaveBeenCalled();
});
test('should run without account', async () => {
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-d', dataDir]);
await callCommand.run(
commander,
'AElf.ContractNames.Token',
'GetTokenInfo',
JSON.stringify({
symbol: 'ELF'
})
);
expect(logger.info).toHaveBeenCalled();
});
test('should run without contractAddress', async () => {
inquirer.prompt = questions => Promise.resolve('');
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir]);
await callCommand.run(commander);
expect(logger.fatal).toHaveBeenCalled();
});
test('should run without params', async () => {
inquirer.prompt = questions => Promise.resolve({ symbol: 'ELF' });
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir]);
await callCommand.run(commander, 'AElf.ContractNames.Token', 'GetTokenInfo');
expect(logger.info).toHaveBeenCalled();
});
test('should run with csv', async () => {
inquirer.prompt = questions =>
Promise.resolve({
symbol: 'ELF',
owner: 'GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk'
});
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.option('-c, --csv <csv>', 'The location of the CSV file containing the parameters.');
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir, '-c', csvDir]);
await callCommand.run(commander, 'AElf.ContractNames.Token', 'GetBalance');
expect(logger.info).toHaveBeenCalled();
});
test('should run with json', async () => {
inquirer.prompt = questions =>
Promise.resolve({
symbol: 'ELF',
owner: 'GyQX6t18kpwaD9XHXe1ToKxfov8mSeTLE9q9NwUAeTE8tULZk'
});
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.option('-j, --json <json>', 'The location of the JSON file containing the parameters.');
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir, '-j', jsonDir]);
await callCommand.run(commander, 'AElf.ContractNames.Token', 'GetBalance');
expect(logger.info).toHaveBeenCalled();
});
test('should run with invalid parameters', async () => {
inquirer.prompt = backup;
callCommand = new CallCommand(sampleRc, 'call', 'Call a read-only method on a contract.', [
...callCommandParameters,
{
type: 'input',
name: 'fake-prop',
message: 'This is a fake prop',
suffix: ':'
}
]);
const commander = new Command();
commander.option('-e, --endpoint <URI>', 'The URI of an AElf node. Eg: http://127.0.0.1:8000');
commander.option('-a, --account <account>', 'The address of AElf wallet');
commander.option('-p, --password <password>', 'The password of encrypted keyStore');
commander.option(
'-d, --datadir <directory>',
`The directory that contains the AElf related files. Default to be ${userHomeDir}/aelf`
);
commander.parse([process.argv[0], '', 'call', '-e', endPoint, '-a', account, '-p', password, '-d', dataDir]);
await callCommand.run(
commander,
'AElf.ContractNames.Token',
'GetTokenInfo',
JSON.stringify({
symbol: 'ELF'
})
);
expect(logger.info).toHaveBeenCalled();
});
afterEach(() => {
inquirer.prompt = backup;
});
});
describe('run call method when only account is provided', () => {
let callCommand;
let mockCommander;
let mockOraInstance;
let mockInquirer;
let getWallet;
let AElf;
beforeEach(() => {
jest.resetModules();
jest.mock('inquirer');
jest.mock('../../src/utils/wallet.js');
jest.mock('aelf-sdk');
mockInquirer = require('inquirer');
mockOraInstance = {
start: jest.fn(),
succeed: jest.fn(),
fail: jest.fn()
};
getWallet = require('../../src/utils/wallet.js').getWallet;
AElf = require('aelf-sdk');
mockCommander = {
name: 'call',
opts: jest.fn(() => ({
account,
endpoint: endPoint,
datadir: dataDir,
password: null
}))
};
callCommand = new CallCommand(sampleRc, 'call', 'Test description', [], [], []);
callCommand.oraInstance = mockOraInstance;
});
afterEach(() => {
jest.resetAllMocks();
});
test('should prompt for password when only account is provided', async () => {
// Mock getWallet to ensure it's called correctly
getWallet.mockReturnValueOnce({
address: 'testAddress'
});
// Mock AElf instance creation
AElf.providers.HttpProvider.mockImplementation(() => ({
send: jest.fn()
}));
inquirer.prompt = jest.fn();
// Run the method
await callCommand.run(mockCommander);
// Assertions
expect(inquirer.prompt).toHaveBeenCalledWith(
expect.objectContaining({
type: 'password',
name: 'password',
message: 'Please enter your password:',
mask: '*'
})
);
});
});