diff --git a/src/utils/node.ts b/src/utils/node.ts index 8cd97d71..8ab4f3b9 100644 --- a/src/utils/node.ts +++ b/src/utils/node.ts @@ -76,7 +76,7 @@ const copyLegacyDB = async (_this: Command) => { _this.log(`Legacy database for Lisk Core v4 has been created at ${LEGACY_DB_PATH}`); }; -const getFinalConfigPath = async (outputDir: string, network: string) => +export const getFinalConfigPath = async (outputDir: string, network: string) => (await exists(`${outputDir}/config.json`)) ? outputDir : path.resolve(__dirname, '../..', 'config', network); diff --git a/test/unit/utils/node.spec.ts b/test/unit/utils/node.spec.ts index 98347e26..60530a9a 100644 --- a/test/unit/utils/node.spec.ts +++ b/test/unit/utils/node.spec.ts @@ -11,7 +11,15 @@ * * Removal or modification of this copyright notice is prohibited. */ -import { validateStartCommandParams } from '../../../src/utils/node'; +import { join, resolve } from 'path'; +import { validateStartCommandParams, getFinalConfigPath } from '../../../src/utils/node'; + +const clientFilePath = resolve(`${__dirname}/../../../src/client`); + +afterEach(() => { + jest.clearAllMocks(); + jest.resetModules(); +}); describe('Test validateStartCommandParams method', () => { const allowedFlags = [ @@ -127,3 +135,53 @@ describe('Test validateStartCommandParams method', () => { }); }); }); + +describe('Test getFinalConfigPath method', () => { + const network = 'mainnet'; + + it('should return default config filepath when output directory does not exists', async () => { + const outputDir = join(__dirname, '../../..', 'test/unit/fixtures/outputDir'); + const configPath = await getFinalConfigPath(outputDir, network); + + const expectedResponse = join(__dirname, '../../..', 'config/mainnet'); + expect(configPath).toBe(expectedResponse); + }); + + it('should return correct config filepath when output directory exists', async () => { + const outputDir = join(__dirname, '../../..', 'config'); + const configPath = await getFinalConfigPath(outputDir, network); + + const expectedResponse = join(__dirname, '../../..', 'config/mainnet'); + expect(configPath).toBe(expectedResponse); + }); +}); + +describe('Test isLiskCoreV3Running method', () => { + it('should return true when node is running', async () => { + jest.mock(clientFilePath, () => ({ + getAPIClient: jest.fn().mockResolvedValueOnce({ + node: { getNodeInfo: jest.fn().mockReturnValue({}) }, + }), + })); + + /* eslint-disable-next-line global-require, @typescript-eslint/no-var-requires */ + const { isLiskCoreV3Running } = require('../../../src/utils/node'); + const response = await isLiskCoreV3Running(); + + expect(response).toBe(true); + }); + + it('should return false when node is not running', async () => { + jest.mock(clientFilePath, () => ({ + getAPIClient: jest.fn().mockResolvedValueOnce({ + node: { getNodeInfo: undefined }, + }), + })); + + /* eslint-disable-next-line global-require, @typescript-eslint/no-var-requires */ + const { isLiskCoreV3Running } = require('../../../src/utils/node'); + const response = await isLiskCoreV3Running(); + + expect(response).toBe(false); + }); +});