Test that local sitemap matches production

This commit is contained in:
Sam Carlton 2021-05-16 17:09:12 -05:00
parent 3a92fd3613
commit b004c8ed1a

View file

@ -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/') )