From b004c8ed1aee125163e6bf12ed09618ce0dcd2e7 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sun, 16 May 2021 17:09:12 -0500 Subject: [PATCH] Test that local sitemap matches production --- test/main.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/main.js b/test/main.js index d342235..7150ca8 100644 --- a/test/main.js +++ b/test/main.js @@ -2,6 +2,7 @@ import { promises as fs } from 'fs' import test from 'ava' import parser from 'fast-xml-parser' +import axios from 'axios' import { structuredDataTest } from 'structured-data-testing-tool' import { Google, Twitter } from 'structured-data-testing-tool/presets' @@ -77,6 +78,45 @@ test('Sitemap contains no double slashes in paths', (t) => { t.pass() }) + +test('Sitemap mostly matches production', async (t) => { + // console.log('t.context.sitemapUrls', t.context.sitemapUrls) + + const theshold = 1 + + const urlsNotOnLive = new Set() + // const newLocalUrls = new Set() + + const liveSitemapXml = await axios( 'https://doesitarm.com/sitemap.xml' ).then( response => response.data ) + const liveSitemap = parser.parse( liveSitemapXml ) + + // Store sitemap urls to context + const liveSitemapUrls = new Map( liveSitemap.urlset.url.map( tag => [ tag.loc, new URL( tag.loc )] ) ) + + + for ( const localUrl of t.context.sitemapUrls ) { + const theoreticalLiveUrl = `https://doesitarm.com${ localUrl.pathname }` + + if ( liveSitemapUrls.has( theoreticalLiveUrl ) ) { + liveSitemapUrls.delete( theoreticalLiveUrl ) + continue + } + + // localUrl is either: Missing or New + urlsNotOnLive.add( theoreticalLiveUrl ) + + } + + const message = `${ urlsNotOnLive.size } new or missing from live and ${ liveSitemapUrls.size } not found locally` + + if ( (urlsNotOnLive.size + liveSitemapUrls.size) >= theshold ) { + t.fail( message ) + } + + t.log( message ) + t.pass() +}) + test('All Category pages have valid FAQPage structured data', async (t) => { const categoryUrls = t.context.sitemapUrls.filter( url => url.pathname.startsWith('/kind/') )