From 96a0412531536d7db83995849d0d452933dd774a Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 2 Aug 2026 17:19:20 -0500 Subject: [PATCH] test: harden generated SEO verification --- scripts/verify-build.mjs | 126 ++++++++++++++++++++++++++------------- 1 file changed, 84 insertions(+), 42 deletions(-) diff --git a/scripts/verify-build.mjs b/scripts/verify-build.mjs index d2dad28..1ac014c 100644 --- a/scripts/verify-build.mjs +++ b/scripts/verify-build.mjs @@ -36,18 +36,28 @@ function verifyPageMetadata({ label, html }) { assert.equal(canonicals.length, 1, `${label} must have exactly one canonical URL`); const canonicalUrl = attribute(canonicals[0], 'href'); assert.ok(canonicalUrl, `${label} canonical URL must not be empty`); - assert.doesNotThrow(() => new URL(canonicalUrl), `${label} canonical URL must be valid`); + let parsedCanonical; + assert.doesNotThrow( + () => { + parsedCanonical = new URL(canonicalUrl); + }, + `${label} canonical URL must be valid`, + ); + assert.ok( + ['https:', 'http:'].includes(parsedCanonical.protocol), + `${label} canonical URL must use HTTP or HTTPS`, + ); const descriptions = metaTags(html, 'name', 'description'); assert.equal(descriptions.length, 1, `${label} must have exactly one meta description`); assert.ok(attribute(descriptions[0], 'content')?.trim(), `${label} meta description must not be empty`); } -function findHtmlFiles(directory) { +function findGeneratedTextAssets(directory) { return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { const entryPath = join(directory, entry.name); - if (entry.isDirectory()) return findHtmlFiles(entryPath); - return entry.isFile() && entry.name.endsWith('.html') ? [entryPath] : []; + if (entry.isDirectory()) return findGeneratedTextAssets(entryPath); + return entry.isFile() && /\.(?:html|css)$/i.test(entry.name) ? [entryPath] : []; }); } @@ -73,13 +83,23 @@ function parseJsonLd(html, label) { }); } -function containsType(value, expectedType) { - if (Array.isArray(value)) return value.some((item) => containsType(item, expectedType)); - if (value === null || typeof value !== 'object') return false; +function findNodesByType(value, expectedType, matches = []) { + if (Array.isArray(value)) { + for (const item of value) findNodesByType(item, expectedType, matches); + return matches; + } + if (value === null || typeof value !== 'object') return matches; const types = Array.isArray(value['@type']) ? value['@type'] : [value['@type']]; - if (types.includes(expectedType)) return true; - return Object.values(value).some((item) => containsType(item, expectedType)); + if (types.includes(expectedType)) matches.push(value); + for (const item of Object.values(value)) findNodesByType(item, expectedType, matches); + return matches; +} + +function requireValidDate(value, message) { + assert.equal(typeof value, 'string', `${message} must be a string`); + assert.ok(value.trim(), `${message} must not be empty`); + assert.ok(!Number.isNaN(Date.parse(value)), `${message} must be a valid date`); } function requireSingleMeta(html, label, property, expectedContent) { @@ -94,47 +114,68 @@ function requireSingleMeta(html, label, property, expectedContent) { } else { assert.ok(attribute(matching[0], 'content')?.trim(), `${label} ${property} must not be empty`); } + return matching[0]; } -const requiredPages = [ - { label: 'home page', path: join(distDir, 'index.html') }, - { label: '404 page', path: join(distDir, '404.html') }, - { label: 'blog index', path: join(distDir, 'blog', 'index.html') }, -]; - -const blogDir = join(distDir, 'blog'); -assert.ok(existsSync(blogDir), 'Missing generated blog directory'); -const postPages = readdirSync(blogDir, { withFileTypes: true }) - .filter((entry) => entry.isDirectory()) - .map((entry) => ({ - label: `blog post ${entry.name}`, - path: join(blogDir, entry.name, 'index.html'), +assert.ok(existsSync(distDir), 'Missing generated dist directory'); +const generatedTextAssets = findGeneratedTextAssets(distDir); +const htmlPages = generatedTextAssets + .filter((filePath) => filePath.toLowerCase().endsWith('.html')) + .map((filePath) => ({ + label: relative(distDir, filePath), + path: filePath, + html: readHtml(filePath), })); +assert.ok(htmlPages.length > 0, 'Expected at least one generated HTML page'); + +for (const page of htmlPages) verifyPageMetadata(page); + +function requirePage(relativePath, label) { + const page = htmlPages.find((candidate) => candidate.label === relativePath); + assert.ok(page, `Missing generated ${label}: ${relativePath}`); + return { ...page, label }; +} + +const home = requirePage('index.html', 'home page'); +const notFound = requirePage('404.html', '404 page'); +requirePage(join('blog', 'index.html'), 'blog index'); + +const postPages = htmlPages + .filter((page) => /^blog[/\\][^/\\]+[/\\]index\.html$/.test(page.label)) + .map((page) => ({ ...page, label: `blog post ${page.label.split(/[/\\]/)[1]}` })); assert.ok(postPages.length > 0, 'Expected at least one generated blog post'); -const pages = [...requiredPages, ...postPages].map((page) => ({ - ...page, - html: readHtml(page.path), -})); - -for (const page of pages) verifyPageMetadata(page); - -const home = pages.find((page) => page.label === 'home page'); const homeJsonLd = parseJsonLd(home.html, home.label); assert.ok( - containsType(homeJsonLd, 'SoftwareApplication'), + findNodesByType(homeJsonLd, 'SoftwareApplication').length > 0, 'home page JSON-LD must contain SoftwareApplication', ); -for (const post of pages.filter((page) => page.label.startsWith('blog post '))) { +for (const post of postPages) { const jsonLd = parseJsonLd(post.html, post.label); - assert.ok(containsType(jsonLd, 'BlogPosting'), `${post.label} JSON-LD must contain BlogPosting`); + const blogPostings = findNodesByType(jsonLd, 'BlogPosting'); + assert.equal(blogPostings.length, 1, `${post.label} JSON-LD must contain one BlogPosting`); + const [blogPosting] = blogPostings; + requireValidDate(blogPosting.datePublished, `${post.label} BlogPosting datePublished`); + requireValidDate(blogPosting.dateModified, `${post.label} BlogPosting dateModified`); + assert.equal(typeof blogPosting.author, 'object', `${post.label} BlogPosting author must be an object`); + assert.ok( + !Array.isArray(blogPosting.author) && blogPosting.author !== null, + `${post.label} BlogPosting author must be an object`, + ); + assert.ok( + typeof blogPosting.author.name === 'string' && blogPosting.author.name.trim(), + `${post.label} BlogPosting author.name must not be empty`, + ); requireSingleMeta(post.html, post.label, 'og:type', 'article'); - requireSingleMeta(post.html, post.label, 'article:published_time'); + const publishedTime = requireSingleMeta(post.html, post.label, 'article:published_time'); requireSingleMeta(post.html, post.label, 'article:author'); + requireValidDate( + attribute(publishedTime, 'content'), + `${post.label} article:published_time content`, + ); } -const notFound = pages.find((page) => page.label === '404 page'); const robots = metaTags(notFound.html, 'name', 'robots'); assert.equal(robots.length, 1, '404 page must have exactly one robots meta tag'); assert.equal( @@ -143,13 +184,14 @@ assert.equal( '404 page robots metadata must be noindex, follow', ); -for (const htmlPath of findHtmlFiles(distDir)) { - const html = readHtml(htmlPath); - assert.doesNotMatch( - html, - /fonts\.(?:googleapis|gstatic)\.com/i, - `${relative(distDir, htmlPath)} must not reference Google Fonts`, - ); +for (const assetPath of generatedTextAssets) { + const source = readFileSync(assetPath, 'utf8'); + for (const forbiddenHost of ['fonts.googleapis.com', 'fonts.gstatic.com']) { + assert.ok( + !source.toLowerCase().includes(forbiddenHost), + `${relative(distDir, assetPath)} must not reference ${forbiddenHost}`, + ); + } } assert.ok(existsSync(join(distDir, '.htaccess')), 'Missing generated dist/.htaccess');