UNPKG

@blockfrost/blockfrost-cardano-cli

Version:

Drop-in(ish) replacement for cardano-cli powered by Blockfrost

124 lines (123 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestClientCommand = exports.TestCommand = void 0; const base_command_1 = require("../base-command"); const stdout_stderr_1 = require("stdout-stderr"); const util_1 = require("util"); const fs = require("fs"); const blockfrostService = require("../../services/blockfrost"); const format_1 = require("../../utils/format"); const constants_1 = require("../../constants"); const errors_1 = require("../../constants/errors"); class TestCommand extends base_command_1.BaseCommand { constructor() { super(...arguments); this.prettyPrint = (data) => { console.log(data.payload); }; this.doWork = async () => { const { flags } = await this.parse(TestCommand); return { payload: 'test complete', flags: flags, }; }; } } exports.TestCommand = TestCommand; class TestClientCommand extends base_command_1.BaseCommand { constructor() { super(...arguments); this.doWork = async () => { await this.parse(TestCommand); await this.getClient(); await this.getClient(); await this.getClient(); return 'test complete'; }; } } exports.TestClientCommand = TestClientCommand; describe('BaseCommand', () => { const ORIGINAL_ENV = process.env; beforeEach(() => { jest.resetModules(); process.env = Object.assign({}, ORIGINAL_ENV); }); afterAll(() => { process.env = ORIGINAL_ENV; jest.restoreAllMocks(); }); it('should prettyPrint response from doWork', async () => { stdout_stderr_1.stdout.start(); await TestCommand.run([]); stdout_stderr_1.stdout.stop(); expect(stdout_stderr_1.stdout.output.includes('test complete')).toBe(true); }); it('should print raw response from doWork', async () => { stdout_stderr_1.stdout.start(); await TestCommand.run(['--json']); stdout_stderr_1.stdout.stop(); const output = stdout_stderr_1.stdout.output; expect(JSON.parse(output)).toMatchObject({ payload: 'test complete', }); }); it('should save the response to --out-file', async () => { jest.spyOn(fs, 'writeFileSync').mockImplementation((_path, _data) => { // do nothing }); await TestCommand.run(['--out-file', 'filename']); expect(fs.writeFileSync).toHaveBeenCalledTimes(1); expect(fs.writeFileSync).toHaveBeenCalledWith('filename', (0, format_1.stringify)({ payload: 'test complete', flags: { 'out-file': 'filename', }, })); }); it('should set --testnet to true if --testnet-magic TESTNET_MAGIC is set', async () => { stdout_stderr_1.stdout.start(); await TestCommand.run(['--json', '--testnet']); stdout_stderr_1.stdout.stop(); const output = stdout_stderr_1.stdout.output; expect(JSON.parse(output).flags['testnet-magic']).toBe(constants_1.NETWORK_MAGIC.testnet); }); it('should throw on unsupported testnet magic in --testnet-magic', async () => { expect(() => TestCommand.run(['--json', '--testnet-magic', '123'])).rejects.toHaveProperty('message', errors_1.ERROR.FLAG_UNSUPPORTED_TESTNET_MAGIC); }); it('should create only one BlockfrostApi instance', async () => { process.env.BLOCKFROST_PROJECT_ID_MAINNET = 'mainnet123'; const spy = jest.spyOn(blockfrostService, 'createBlockfrostClient'); await TestClientCommand.run([]); expect(spy).toHaveBeenCalledTimes(1); spy.mockRestore(); }); }); describe('BaseCommand - 2nd part (runs in series)', () => { // process.env cant be isolated across tests running in parallel, // but we can put tests in another describe which is run only after previous is finished const ORIGINAL_ENV = process.env; beforeEach(() => { jest.resetModules(); // reset process.env process.env = {}; }); afterAll(() => { process.env = ORIGINAL_ENV; }); it('should print error when BLOCKFROST_PROJECT_ID_MAINNET is not set', async () => { expect(async () => TestClientCommand.run([])).rejects.toHaveProperty('message', (0, util_1.format)(errors_1.ERROR.ENV_PROJECT_ID_NOT_SET, 'BLOCKFROST_PROJECT_ID_MAINNET')); // workaround ReferenceError: You are trying to `import` a file after the Jest environment has been torn down await new Promise(resolve => { setTimeout(() => resolve(true), 500); }); }); it('should print error when BLOCKFROST_PROJECT_ID_TESTNET is not set', async () => { expect(async () => TestClientCommand.run(['--testnet'])).rejects.toHaveProperty('message', (0, util_1.format)(errors_1.ERROR.ENV_PROJECT_ID_NOT_SET, 'BLOCKFROST_PROJECT_ID_TESTNET')); // workaround ReferenceError: You are trying to `import` a file after the Jest environment has been torn down await new Promise(resolve => { setTimeout(() => resolve(true), 500); }); }); });