import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import test from 'node:test'; const templates = [ 'home', 'single', 'page', 'archive', 'search', '404' ]; const readTemplate = (name) => readFile(new URL(`../../templates/${name}.html`, import.meta.url), 'utf8'); function assertBalancedBlockMarkup(markup, name) { const stack = []; for (const match of markup.matchAll(//gs)) { const [, closing, blockName, selfClosing] = match; if (selfClosing) continue; if (!closing) { stack.push(blockName); continue; } assert.equal(stack.pop(), blockName, `${name} must close ${blockName} in order`); } assert.deepEqual(stack, [], `${name} must close every WordPress block`); } for (const name of templates) { test(`${name}.html is a complete portable page template`, async () => { const markup = await readTemplate(name); assertBalancedBlockMarkup(markup, name); assert.equal((markup.match(/wp:template-part \{"slug":"header","tagName":"header"\}/g) ?? []).length, 1); assert.equal((markup.match(/wp:template-part \{"slug":"footer","tagName":"footer"\}/g) ?? []).length, 1); assert.equal((markup.match(//g) ?? []).length, 1); assert.match(markup, /]*\bid="chrnx-main"[^>]*>/); assert.doesNotMatch(markup, /wp:(?:legacy-widget|widget-group)| { const markup = await readTemplate('home'); const orderedMarkers = [ '"slug":"header"', 'wp:pattern {"slug":"chrnx/home-intro"}', 'wp:chrnx/featured-stories', 'wp:chrnx/journal-stream', 'wp:pattern {"slug":"chrnx/home-projects"}', 'wp:pattern {"slug":"chrnx/home-music"}', 'wp:chrnx/latest-podcast', 'chrnx-home-about-preview', 'wp:chrnx/subscribe-cta', '"slug":"footer"', ]; let previous = -1; for (const marker of orderedMarkers) { const index = markup.indexOf(marker); assert.ok(index > previous, `${marker} must appear in the approved home order`); previous = index; } }); test('single provides readable content, neutral metadata, and post-only components', async () => { const markup = await readTemplate('single'); assert.match(markup, /chrnx-entry--single/); assert.match(markup, /wp:post-title/); assert.match(markup, /wp:post-terms \{"term":"chrnx_topic"/); assert.match(markup, /wp:post-terms \{"term":"chrnx_language"/); assert.match(markup, /wp:post-date/); assert.match(markup, /wp:chrnx\/reading-time/); assert.match(markup, /wp:post-featured-image/); assert.match(markup, /wp:post-content/); assert.match(markup, /wp:chrnx\/podcast-meta/); assert.match(markup, /wp:chrnx\/related-posts/); assert.match(markup, /wp:chrnx\/subscribe-cta/); }); test('page remains a readable title and content shell without post metadata', async () => { const markup = await readTemplate('page'); assert.match(markup, /chrnx-entry--page/); assert.match(markup, /wp:post-title/); assert.match(markup, /wp:post-content/); assert.doesNotMatch(markup, /wp:post-(?:date|terms|featured-image)|wp:chrnx\/(?:podcast-meta|related-posts)/); }); test('archive offers context, safe filters, stream, and a useful empty state', async () => { const markup = await readTemplate('archive'); assert.match(markup, /wp:query-title/); assert.match(markup, /wp:term-description/); assert.match(markup, /wp:chrnx\/topic-hero/); assert.match(markup, /wp:chrnx\/journal-stream/); }); test('search offers search context, filters, and a Journal recovery path', async () => { const markup = await readTemplate('search'); assert.match(markup, /wp:query-title \{"type":"search"/); assert.match(markup, /wp:search/); assert.match(markup, /wp:chrnx\/journal-stream/); assert.match(markup, /wp:home-link/); assert.doesNotMatch(markup, /"url":"\//); }); test('404 offers concise recovery with search, Journal, and home links', async () => { const markup = await readTemplate('404'); assert.match(markup, /Page not found/); assert.match(markup, /wp:search/); assert.match(markup, /wp:home-link/); assert.match(markup, /wp:chrnx\/journal-link/); assert.doesNotMatch(markup, /"url":"\//); });