import { constructCommand, extractApiKeyFromCommand, extractHostFromCommand, getApiKey, getHost, getLogUsageData, } from '../log-usage'; import { expect } from '../../../test-utils'; const baseExampleCommand = ['directory', 'node']; const productionUrl = '???productionUrl'; describe('Usage monitor', function () { describe('constructCommand', function () { it('should correctly construct the clone command using -h flag', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '-h', 'https://my_domain', ]; const result = constructCommand(exampleCommand); expect(result.fullCommand).to.equal('rp clone pm_key -h https://my_domain'); expect(result.baseCommand).to.equal('clone'); }); it('should correctly construct the clone command using --host flag', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; const result = constructCommand(exampleCommand); expect(result.fullCommand).to.equal('rp clone pm_key --host https://my_domain'); expect(result.baseCommand).to.equal('clone'); }); it('should correctly construct the rp push command', function () { const exampleCommand = [...baseExampleCommand, 'push']; const result = constructCommand(exampleCommand); expect(result.fullCommand).to.equal('rp push'); expect(result.baseCommand).to.equal('push'); }); it('should correctly construct the rp push command with the force flag', function () { const exampleCommand = [...baseExampleCommand, 'push', '-f']; const result = constructCommand(exampleCommand); expect(result.fullCommand).to.equal('rp push -f'); expect(result.baseCommand).to.equal('push'); }); }); describe('getHostFromCommand', function () { it('should correctly extract the host from a clone command using -h', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '-h', 'https://my_domain', ]; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal('https://my_domain'); }); it('should return the production command if a host argument is not provided', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key']; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal(productionUrl); }); it('should correctly extract the host from a clone command using --host', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal('https://my_domain'); }); it('should return the production url if the index of the -h flag throws an index error', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '-h']; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal(productionUrl); }); it('should return the production url if the index of the --host flag throws an index error', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host']; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal(productionUrl); }); it('should return the production url if the -h argument is not valid', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '-h', 'invalid']; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal(productionUrl); }); it('should return the production url if the --host argument is not valid', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'invalid']; const result = extractHostFromCommand(exampleCommand); expect(result).to.equal(productionUrl); }); }); describe('extractApiKey from Command', function () { it('should correctly extract a production api key from a clone command', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key']; const result = extractApiKeyFromCommand(exampleCommand); expect(result).to.equal('production_some_api_key'); }); it('should correctly extract a sandbox api key from a clone command', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'sandbox_some_api_key', 'pm_key']; const result = extractApiKeyFromCommand(exampleCommand); expect(result).to.equal('sandbox_some_api_key'); }); it('should return an empty string when no valid api key is provided', function () { const exampleCommand = [...baseExampleCommand, 'clone', 'pm_key']; const result = extractApiKeyFromCommand(exampleCommand); expect(result).to.equal(''); }); }); describe('getHost', function () { it('should correctly call the getHost function when the host is in the command', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; const result = getHost('clone', exampleCommand); expect(result).to.equal('https://my_domain'); }); it('should attempt to call the checkValidDirectory function when the push command is called', function () { const exampleCommand = [...baseExampleCommand, 'push']; expect(() => { getHost('push', exampleCommand); }).to.throw(Error); // Cannot get string matching of the error correct here }); }); describe('getApiKey', function () { it('should attempt to call the checkValidDirectory function when when the push command is called', function () { const exampleCommand = [...baseExampleCommand, 'push']; expect(() => { getApiKey('push', exampleCommand); }).to.throw(Error); }); it('should correctly call the getApiKey function when the clone command is called', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; console.log(getApiKey('clone', exampleCommand)); expect(getApiKey('clone', exampleCommand)).to.equal('production_some_api_key'); }); }); describe('getLogUsageData', function () { it('should correctly call the logUsageData command on a clone command', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; const expectedResult = { body: { command: 'rp clone pm_key --host https://my_domain' }, host: 'https://my_domain', apiKey: 'production_some_api_key', }; const result = getLogUsageData({ args: exampleCommand }); expect(result).to.deep.equal(expectedResult); }); it('should correctly call the getLogUsageData function with an error on a clone command', function () { const exampleCommand = [ ...baseExampleCommand, 'clone', 'production_some_api_key', 'pm_key', '--host', 'https://my_domain', ]; const expectedResult = { body: { command: 'rp clone pm_key --host https://my_domain', error: 'Something went wrong' }, host: 'https://my_domain', apiKey: 'production_some_api_key', }; const result = getLogUsageData({ args: exampleCommand, error: 'Something went wrong' }); expect(result).to.deep.equal(expectedResult); }); }); });