Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
PR Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 committed Jun 3, 2024
1 parent 2861d37 commit a8bf4ba
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
2 changes: 1 addition & 1 deletion bulk-update/bulk-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
}
Expand Down
6 changes: 1 addition & 5 deletions test/bulk-update/reporter/excel-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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();
Expand All @@ -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');
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion test/validation/deep-compare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down Expand Up @@ -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',
});
Expand Down
45 changes: 20 additions & 25 deletions test/validation/link-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -139,8 +142,6 @@ describe('Link Validator', () => {
newText: 'Link 2',
},
];
const comparison = ['source', 'updated'];
const entry = '/entry';
const expected = [
{
newText: 'Link 2',
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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');
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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`;

Expand Down
34 changes: 16 additions & 18 deletions validation/deep-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;
}
Expand Down

0 comments on commit a8bf4ba

Please sign in to comment.