diff --git a/bulk-update/bulk-update.js b/bulk-update/bulk-update.js index 99192f3..eabfdf2 100644 --- a/bulk-update/bulk-update.js +++ b/bulk-update/bulk-update.js @@ -95,7 +95,7 @@ export async function loadListData(source, fetchFunction = fetch, fetchWaitMs = * @returns {string} The staged URL. */ export function localizeStagePath(entry, stagePath = '', locales = []) { - const currentLocale = locales.find((locale) => locale && entry.startsWith(`/${locale}/`)); + const currentLocale = locales?.find((locale) => locale && entry.startsWith(`/${locale}/`)); const localizedPath = currentLocale ? entry.replace(`/${currentLocale}/`, `/${currentLocale}${stagePath}/`) : `${stagePath}${entry}`; return localizedPath.replace(/\/+/g, '/'); } diff --git a/test/bulk-update/reporter/excel-reporter.test.js b/test/bulk-update/reporter/excel-reporter.test.js index e96b21f..387969e 100644 --- a/test/bulk-update/reporter/excel-reporter.test.js +++ b/test/bulk-update/reporter/excel-reporter.test.js @@ -18,7 +18,7 @@ describe('ExcelReporter', () => { }); }); - describe('Check reporter and excel js library is called', () => { + describe('Check excel js instantiation and methods', () => { const sandbox = sinon.createSandbox(); it('creates a new workbook', () => { @@ -39,11 +39,9 @@ describe('ExcelReporter', () => { it('appends log to sheet', () => { const reporter = new ExcelReporter(); - sandbox.spy(reporter); sandbox.spy(reporter.workbook); reporter.log('topic', 'status', 'message', 'arg1', 'arg2'); - expect(reporter.log.calledOnce).to.be.true; expect(reporter.workbook.addWorksheet.calledOnce).to.be.true; sandbox.restore(); @@ -53,7 +51,6 @@ describe('ExcelReporter', () => { const filepath = `${pathname}output/append.xlsx`; const reporter = new ExcelReporter(filepath, true); - sandbox.spy(reporter); sandbox.spy(reporter.workbook); reporter.log('topic1', 'status1', 'message1'); @@ -64,7 +61,6 @@ describe('ExcelReporter', () => { await reporter.generateTotals(); - expect(reporter.log.callCount).to.equal(5); expect(reporter.workbook.addWorksheet.callCount).to.equal(2); expect(reporter.workbook.getWorksheet('Totals')).is.not.undefined; diff --git a/test/validation/deep-compare.test.js b/test/validation/deep-compare.test.js index 1076f32..9569f72 100644 --- a/test/validation/deep-compare.test.js +++ b/test/validation/deep-compare.test.js @@ -46,7 +46,7 @@ describe('Deep Compare', () => { const strTwo = 'sitting'; const similarity = lSimilarity(strOne, strTwo); - expect(similarity).to.equal(0.5714285714285714); + expect(similarity.toFixed(4)).to.equal('0.5714'); }); }); @@ -144,6 +144,8 @@ describe('Deep Compare', () => { const observations = observeLinks(oldUrl, newUrl, oldText, newText); expect(observations.url).to.contain({ + [MATCH]: false, + [PATHNAME_MATCH]: false, [SIMILARITY_PERCENTAGE]: '84%', [SIMILARITY]: 'High Similarity', }); diff --git a/test/validation/link-validator.test.js b/test/validation/link-validator.test.js index 89aa574..161f6e4 100644 --- a/test/validation/link-validator.test.js +++ b/test/validation/link-validator.test.js @@ -37,6 +37,9 @@ import { const { pathname } = new URL('.', import.meta.url); describe('Link Validator', () => { + const comparison = ['source', 'updated']; + const entry = '/entry'; + describe('getMarkdownLinks', () => { it('should return an object with sourceLinks and updatedLinks', async () => { const sourceMdast = {}; @@ -139,8 +142,6 @@ describe('Link Validator', () => { newText: 'Link 2', }, ]; - const comparison = ['source', 'updated']; - const entry = '/entry'; const expected = [ { newText: 'Link 2', @@ -163,8 +164,8 @@ describe('Link Validator', () => { [BOTH_EMPTY]: false, [ASCII]: false, [DOUBLE_HASH]: false, + [HOST_MATCH]: false, [HASH_MATCH]: true, - [HOST_MATCH]: true, [LENGTHS_MATCH]: true, [PATHNAME_MATCH]: true, [SEARCH_MATCH]: true, @@ -192,8 +193,6 @@ describe('Link Validator', () => { { url: 'https://example.com', children: [{ type: 'text', value: 'Link 1' }] }, { url: 'https://example.com/entry', children: [{ type: 'text', value: 'Link 2' }] }, ]; - const comparison = ['source', 'updated']; - const entry = '/entry'; const expected = { comparisons: 1, anomalies: 0, @@ -213,8 +212,6 @@ describe('Link Validator', () => { { url: 'https://example.com', children: [{ type: 'text', value: 'Link 1' }] }, { url: 'https://example.com', children: [{ type: 'text', value: 'Link 2' }] }, ]; - const comparison = ['source', 'updated']; - const entry = '/entry'; const expected = { comparisons: 0, anomalies: 0, @@ -246,19 +243,29 @@ describe('Link Validator', () => { }); }); -describe('Validate Markdown', () => { +describe('Validate Markdown MDAST', () => { + const comparison = ['source', 'updated']; + const entry = '/entry'; + const sourceMd = fs.readFileSync(`${pathname}mocks/adobe-experience-manager-source.md`, 'utf-8'); const updatedMd = fs.readFileSync(`${pathname}mocks/adobe-experience-manager-updated.md`, 'utf-8'); const mismatchMd = fs.readFileSync(`${pathname}mocks/adobe-experience-manager-updated-mismatched.md`, 'utf-8'); const shuffledMd = fs.readFileSync(`${pathname}mocks/adobe-experience-manager-shuffled.md`, 'utf-8'); - const comparison = ['source', 'updated']; - const entry = '/entry'; + let sourceMdast; + let updatedMdast; + let mismatchedMdast; + let shuffledMdast; + + beforeEach(() => { + sourceMdast = getMdast(sourceMd); + updatedMdast = getMdast(updatedMd); + mismatchedMdast = getMdast(mismatchMd); + shuffledMdast = getMdast(shuffledMd); + }); describe('getMarkdownLinks', () => { it('Gets markdown links', async () => { - const sourceMdast = await getMdast(sourceMd); - const updatedMdast = await getMdast(updatedMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, updatedMdast); expect(sourceLinks).to.be.an('array'); @@ -271,8 +278,6 @@ describe('Validate Markdown', () => { describe('getLinkLists', () => { it('Gets link lists', async () => { - const sourceMdast = await getMdast(sourceMd); - const updatedMdast = await getMdast(updatedMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, updatedMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); @@ -289,8 +294,6 @@ describe('Validate Markdown', () => { describe('exactMatch', () => { it('All links match when updated correctly', async () => { - const sourceMdast = await getMdast(sourceMd); - const updatedMdast = await getMdast(updatedMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, updatedMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); @@ -299,8 +302,6 @@ describe('Validate Markdown', () => { }); it('All links do not match when links are mismatched', async () => { - const sourceMdast = await getMdast(sourceMd); - const mismatchedMdast = await getMdast(mismatchMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, mismatchedMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); @@ -311,8 +312,6 @@ describe('Validate Markdown', () => { describe('deepCompare', () => { it('Returns an empty array when there are no differences in links', async () => { - const sourceMdast = await getMdast(sourceMd); - const updatedMdast = await getMdast(updatedMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, updatedMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); @@ -322,8 +321,6 @@ describe('Validate Markdown', () => { }); it('Returns an array of observations when there are differences in links', async () => { - const sourceMdast = await getMdast(sourceMd); - const mismatchedMdast = await getMdast(mismatchMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, mismatchedMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); const observations = deepCompare(linkLists, comparison, entry); @@ -343,8 +340,6 @@ describe('Validate Markdown', () => { describe('reportAnomalies', () => { it('Returns an array of anomalies', async () => { - const sourceMdast = await getMdast(sourceMd); - const shuffledMdast = await getMdast(shuffledMd); const { sourceLinks, updatedLinks } = await getMarkdownLinks(sourceMdast, shuffledMdast); const linkLists = getLinkLists(sourceLinks, updatedLinks); const observations = deepCompare(linkLists, comparison, entry); @@ -368,7 +363,7 @@ describe('Validate Markdown', () => { }); }); -describe('Validate Mock Data', () => { +describe('Validate Mock Folders', () => { const listPath = `${pathname}/mocks/list.json`; const mdPath = `${pathname}/mocks/md-mocks`; diff --git a/validation/deep-compare.js b/validation/deep-compare.js index eb2601d..960c52b 100644 --- a/validation/deep-compare.js +++ b/validation/deep-compare.js @@ -93,7 +93,7 @@ export function similarityCategory(similarity) { * @param {string} stringTwo - The second string to compare. * @returns {Object} - An object containing observations about the similarity of the two strings. */ -export function observe(stringOne = '', stringTwo = '') { +export function observeText(stringOne = '', stringTwo = '') { const observations = {}; observations[MATCH] = stringOne === stringTwo; @@ -111,17 +111,6 @@ export function observe(stringOne = '', stringTwo = '') { return observations; } -/** - * Observes the changes between two text values. - * - * @param {string} oldText - The old text value. - * @param {string} newText - The new text value. - * @returns {Object} - The observed changes between the old and new text values. - */ -export function observeText(oldText = '', newText = '') { - return observe(oldText, newText); -} - /** * Observes and compares two URLs. * @@ -130,16 +119,25 @@ export function observeText(oldText = '', newText = '') { * @returns {Object} - An object containing observations about the URL comparison. */ export function observeUrl(oldUrl = '', newUrl = '') { - const observations = observe(oldUrl, newUrl); + const observations = observeText(oldUrl, newUrl); const oldUrlObj = oldUrl ? new URL(oldUrl, DOMAIN) : null; const newUrlObj = newUrl ? new URL(newUrl, DOMAIN) : null; + const validUrl = !!(oldUrlObj?.href && newUrlObj?.href); observations[DOUBLE_HASH] = oldUrl?.match(/#/g)?.length > 1 || newUrl?.match(/#/g)?.length > 1; - observations[VALID_URL] = !!(oldUrlObj?.href && newUrlObj?.href); - observations[HASH_MATCH] = oldUrl?.hash === newUrl?.hash; - observations[HOST_MATCH] = oldUrl?.host === newUrl?.host; - observations[PATHNAME_MATCH] = oldUrl?.pathname === newUrl?.pathname; - observations[SEARCH_MATCH] = oldUrl?.search === newUrl?.search; + observations[VALID_URL] = validUrl; + + if (validUrl) { + observations[HASH_MATCH] = oldUrlObj?.hash === newUrlObj?.hash; + observations[HOST_MATCH] = oldUrlObj?.host === newUrlObj?.host; + observations[PATHNAME_MATCH] = oldUrlObj?.pathname === newUrlObj?.pathname; + observations[SEARCH_MATCH] = oldUrlObj?.search === newUrlObj?.search; + } else { + observations[HASH_MATCH] = false; + observations[HOST_MATCH] = false; + observations[PATHNAME_MATCH] = false; + observations[SEARCH_MATCH] = false; + } return observations; }