Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-command",
"version": "0.1.51-beta.1",
"version": "0.1.52",
"description": "A CLI tools for AElf",
"main": "src/index.js",
"type": "module",
Expand Down
13 changes: 11 additions & 2 deletions src/command/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ class CallCommand extends BaseSubCommand {
try {
let { contractAddress, method, params } = subOptions;
let wallet;
if (!account || !password) {
// no need to provide account and password
if (!account) {
// No account and password provided, create a new wallet
wallet = AElf.wallet.createNewWallet();
} else if (account && !password) {
// Account provided but no password, prompt user for password
const passwordPrompt = await inquirer.prompt({
type: 'password',
name: 'password',
message: 'Please enter your password:',
mask: '*'
});
wallet = getWallet(datadir, account, passwordPrompt.password);
} else {
wallet = getWallet(datadir, account, password);
}
Expand Down
62 changes: 61 additions & 1 deletion test/command/call.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Command } from 'commander';
import path from 'path';
import AElf from 'aelf-sdk';
import inquirer from 'inquirer';
import { CallCommand } from '../../src/command';
Expand Down Expand Up @@ -196,3 +195,64 @@ describe('CallCommand', () => {
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: '*'
})
);
});
});
Loading