From 531852f1d03a25082a13df74317fdec3e2feb52b Mon Sep 17 00:00:00 2001 From: Divyansh Pratap Date: Mon, 2 Sep 2024 23:27:54 +0530 Subject: [PATCH] feat: adding info log --- src/metatags/handler.js | 12 ++++---- test/audits/metatags.test.js | 54 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/metatags/handler.js b/src/metatags/handler.js index 66db4b2b..942b217a 100644 --- a/src/metatags/handler.js +++ b/src/metatags/handler.js @@ -19,16 +19,16 @@ import SeoChecks from './seo-checks.js'; async function fetchAndProcessPageObject(s3Client, bucketName, key, prefix, log) { const object = await getObjectFromKey(s3Client, bucketName, key, log); - if (!object?.tags || typeof object.tags !== 'object') { + if (!object?.scrapeResult?.tags || typeof object.scrapeResult.tags !== 'object') { log.error(`No Scraped tags found in S3 ${key} object, body ${JSON.stringify(object)}`); return null; } const pageUrl = key.slice(prefix.length - 1).replace('scrape.json', ''); // Remove the prefix and .json suffix return { [pageUrl]: { - title: object.tags.title, - description: object.tags.description, - h1: object.tags.h1 || [], + title: object.scrapeResult.tags.title, + description: object.scrapeResult.tags.description, + h1: object.scrapeResult.tags.h1 || [], }, }; } @@ -66,10 +66,12 @@ export default async function auditMetaTags(message, context) { Object.assign(extractedTags, pageMetadata); } } - if (Object.entries(extractedTags).length === 0) { + const extractedTagsCount = Object.entries(extractedTags).length; + if (extractedTagsCount === 0) { log.error(`Failed to extract tags from scraped content for bucket ${bucketName} and prefix ${prefix}`); return notFound('Site tags data not available'); } + log.info(`Performing SEO checks for ${extractedTagsCount} tags`); // Perform SEO checks const seoChecks = new SeoChecks(log); for (const [pageUrl, pageTags] of Object.entries(extractedTags)) { diff --git a/test/audits/metatags.test.js b/test/audits/metatags.test.js index 48d5f3a5..1fbe5595 100644 --- a/test/audits/metatags.test.js +++ b/test/audits/metatags.test.js @@ -257,9 +257,11 @@ describe('Meta Tags', () => { }))).returns({ Body: { transformToString: () => JSON.stringify({ - tags: { - title: 'Test Page', - description: '', + scrapeResult: { + tags: { + title: 'Test Page', + description: '', + }, }, }), }, @@ -271,11 +273,13 @@ describe('Meta Tags', () => { }))).returns({ Body: { transformToString: () => JSON.stringify({ - tags: { - title: 'Test Page', - h1: [ - 'This is a dummy H1 that is overly length from SEO perspective', - ], + scrapeResult: { + tags: { + title: 'Test Page', + h1: [ + 'This is a dummy H1 that is overly length from SEO perspective', + ], + }, }, }), }, @@ -344,7 +348,7 @@ describe('Meta Tags', () => { ], })); expect(addAuditStub.calledOnce).to.be.true; - expect(logStub.info.calledThrice).to.be.true; + expect(logStub.info.callCount).to.equal(4); }); it('should process site tags and perform SEO checks for pages with invalid H1s', async () => { @@ -378,13 +382,15 @@ describe('Meta Tags', () => { }))).returns({ Body: { transformToString: () => JSON.stringify({ - tags: { - title: 'This is an SEO optimal page1 valid title.', - description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.', - h1: [ - 'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars', - 'This is second h1 tag on same page', - ], + scrapeResult: { + tags: { + title: 'This is an SEO optimal page1 valid title.', + description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.', + h1: [ + 'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars', + 'This is second h1 tag on same page', + ], + }, }, }), }, @@ -396,12 +402,14 @@ describe('Meta Tags', () => { }))).returns({ Body: { transformToString: () => JSON.stringify({ - tags: { - title: 'This is a SEO wise optimised page2 title.', - description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.', - h1: [ - 'This is an overly long H1 tag from SEO perspective', - ], + scrapeResult: { + tags: { + title: 'This is a SEO wise optimised page2 title.', + description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.', + h1: [ + 'This is an overly long H1 tag from SEO perspective', + ], + }, }, }), }, @@ -446,7 +454,7 @@ describe('Meta Tags', () => { ], })); expect(addAuditStub.calledOnce).to.be.true; - expect(logStub.info.calledThrice).to.be.true; + expect(logStub.info.callCount).to.equal(4); }); it('should handle errors and return internalServerError', async () => {