diff --git a/package.json b/package.json index 79944f3..124d65c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/command/call.js b/src/command/call.js index 8674501..f9a7765 100644 --- a/src/command/call.js +++ b/src/command/call.js @@ -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); } diff --git a/test/command/call.test.js b/test/command/call.test.js index ad0f818..0fe888b 100644 --- a/test/command/call.test.js +++ b/test/command/call.test.js @@ -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'; @@ -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: '*' + }) + ); + }); +});