import { cloneDeep } from 'lodash';
import sinon from 'sinon';
import { diff } from '../diff';
import { expect } from '../../../test-utils';
import * as readProductModuleDefinition from '../../helpers/read-product-module-definition';
import * as fetchProductModuleDefinition from '../../helpers/fetch-product-module-definition';
import * as checkPackageAndNodeVersion from '../../helpers/version-check';
import * as readAuthAndConfig from '../../helpers/read-auth-and-config';
import * as checkValidProductModuleDirectory from '../../helpers/check-valid-product-module-directory';
import { cliDefinitionExample, rootConfigFileExample } from '../../../test-utils/test-root-funeral-pm';
import {
CLIProductModuleDefinition,
NetworkProductModuleDefinitionResponse,
productModuleDefinitionFromNetwork,
} from '../../domain/product-module-definition';
const validCheckDirectoryOutput = {
apiKey: '000000000000000',
config: rootConfigFileExample,
};
const validCheckVersionOutput = { outdated: false, latest: '', current: '' };
const sandbox = sinon.createSandbox();
const getConsoleLogOutput = (consoleLogStub: sinon.SinonStub) => {
let consoleLogOutput = '';
for (let i = 0; i <= consoleLogStub.callCount - 1; i += 1) {
consoleLogOutput += consoleLogStub.getCall(i).args[0];
}
return consoleLogOutput;
};
describe('rp diff', function () {
let localDefinition: CLIProductModuleDefinition;
let remoteDefinition: NetworkProductModuleDefinitionResponse;
beforeEach(async function () {
sandbox.stub(checkPackageAndNodeVersion, 'checkPackageAndNodeVersion').resolves(validCheckVersionOutput);
sandbox.stub(readAuthAndConfig, 'readAuthAndConfig').returns(validCheckDirectoryOutput);
sandbox.stub(checkValidProductModuleDirectory, 'checkValidProductModuleDirectory');
// Changes are introducted in each individual test
remoteDefinition = cloneDeep(cliDefinitionExample);
localDefinition = productModuleDefinitionFromNetwork(cloneDeep(cliDefinitionExample));
});
afterEach(async function () {
sandbox.verifyAndRestore();
});
it('--code | should display lines added to local code files', async function () {
const fileName = 'test-add-local.js';
remoteDefinition.code_files.push({
file_name: fileName,
file_content: 'First line of code\n',
});
localDefinition.codeFiles.push({
fileName,
fileContent: 'First line of code\n Second line of code\n',
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
// Using stub instead of spy to suppress output
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ code: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
// Resetting as soon as possible so that console log can be used for dev purposes
consoleLogStub.reset();
expect(consoleLogOutput).to.include(fileName);
expect(consoleLogOutput).to.include('+ Second line of code');
});
it('--unit-tests | should display lines added to local unit test files', async function () {
const fileName = 'test-add-local-ut.js';
remoteDefinition.unit_test_code_files.push({
file_name: fileName,
file_content: 'First line of code\n',
});
localDefinition.unitTestCodeFiles.push({
fileName,
fileContent: 'First line of code\n Second line of code yay\n',
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
// Using stub instead of spy to suppress output
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ unitTests: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
// Resetting as soon as possible so that console log can be used for dev purposes
consoleLogStub.reset();
expect(consoleLogOutput).to.include(fileName);
expect(consoleLogOutput).to.include('+ Second line of code yay');
});
it('--documents | should display differences in the welcome letter ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
welcome_letter_html: '',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('
Welome
');
});
it('--documents | should display differences in the policy schedule ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
policy_schedule_html: 'Policy schedule updated
\n',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('policy-schedule.html');
});
it('--documents | should show that a new terms file has been added/removed', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
terms_pdf_base_64: '',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('terms.pdf has been updated');
});
it('--documents | should show updates to supplementary terms files', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
supplementary_terms: [
{
type: 'test-type',
pdf_base_64: 'changed',
},
],
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('test-type.pdf has been updated');
});
it('--documents | should display differences in the anniversary letter ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
anniversary_letter_html: 'Anniversary Letter
',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('Anniversary Letter
');
});
it('--documents | should display differences in the quote summary ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
quote_summary_html: 'Quote Summary
',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('Quote Summary
');
});
it('--documents | should display differences in the member certificate ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
member_certificate_html: '\n \n added line',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('added line');
});
it('--documents | should display differences in the certificate ', async function () {
remoteDefinition = {
...remoteDefinition,
documents: {
...remoteDefinition.documents,
certificate_html: ' New Certificate
\n',
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ documents: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('New');
});
it('--documents | should display differences in the API Docs Schema ', async () => {
const localApiDocs = { version: '1.0', endpoints: [] };
const remoteApiDocs = {
version: '1.0',
endpoints: [
{
id: 'policy-object-dinosure-parameters',
title: 'module object',
},
],
};
localDefinition = { ...localDefinition, apiDocsSchema: localApiDocs };
remoteDefinition = { ...remoteDefinition, api_docs_schema: remoteApiDocs };
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ apiDocs: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('policy-object-dinosure-parameters');
});
it('--documents | should display differences in the ReadMe File', async function () {
remoteDefinition = {
...remoteDefinition,
readme_markdown: '#This is markdown. \n This is the readMe File\n',
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ readMe: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('This is the readMe File');
});
it('--workflows | should display differences in the alteration workflows', async function () {
const alterationHook = remoteDefinition.workflows.alteration_hooks[0];
alterationHook.schema.push({
key: 'name',
type: 'text',
label: 'Name',
validators: [
{
validation: {
type: 'required',
},
},
],
outputPath: 'name',
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ workflows: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('--- alteration_hook_1');
});
it('--workflows | should display differences in the embed workflows', async function () {
remoteDefinition = {
...remoteDefinition,
workflows: {
...remoteDefinition.workflows,
product_module_embed_config: {
...remoteDefinition.workflows.product_module_embed_config,
prePaymentCompliance: {
wording: {
description: 'Read the following terms and conditions before proceeding...',
content: '**prePaymentCompliance**\n',
},
images: {},
links: {},
displayOptionalSections: {
displayPrePaymentCompliance: true,
},
},
},
},
};
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ workflows: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('--- embed-config');
});
it('--workflows | should display differences in the claims workflow', async function () {
remoteDefinition.workflows.claims_blocks.push({
block: {
key: 'header_one',
type: 'heading',
title: 'Details of the deceased',
},
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ workflows: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('--- claims-blocks');
});
it('--workflows | should display differences in the quote workflow', async function () {
remoteDefinition.workflows.quote_schema.push({
key: 'start_date',
type: 'date-picker',
label: 'Start Date *',
outputPath: 'start_date',
validators: [
{
validation: {
type: 'required',
},
},
],
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ workflows: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('--- quote-schema');
});
it('--workflows | should display differences in the application workflow', async function () {
remoteDefinition.workflows.application_schema.push({
key: 'start_date',
type: 'date-picker',
label: 'Start Date *',
outputPath: 'start_date',
validators: [
{
validation: {
type: 'required',
},
},
],
});
sandbox.stub(readProductModuleDefinition, 'readProductModuleDefinition').resolves(localDefinition);
sandbox.stub(fetchProductModuleDefinition, 'fetchProductModuleDefinition').resolves(remoteDefinition);
const consoleLogStub = sandbox.stub(console, 'log');
await diff({ workflows: true });
const consoleLogOutput = getConsoleLogOutput(consoleLogStub);
consoleLogStub.reset();
expect(consoleLogOutput).to.include('--- application-schema');
});
});