feat: add CHRNX templates and editorial patterns

This commit is contained in:
chrnx committed 2026-08-11 18:10:18 -05:00
1 parent 282a055cf1
commit 247f9a7915
20 files changed
+561 -4

No files matched your search

+8 -3
View File
@@ -201,8 +201,8 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringNotContainsString( '<iframe', $output );
}
/** Topic hero uses a mapped explicit topic and falls back accessibly if art is absent. */
public function test_topic_hero_rejects_paths_and_uses_css_fallback_for_missing_art() {
/** Topic hero uses approved decorative art for mapped topics and rejects arbitrary paths. */
public function test_topic_hero_rejects_paths_and_uses_approved_decorative_art() {
$output = render_block(
array(
'blockName' => 'chrnx/topic-hero',
@@ -218,7 +218,12 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringContainsString( '<h2', $output );
$this->assertStringContainsString( 'Research', $output );
$this->assertStringContainsString( 'aria-hidden="true"', $output );
$this->assertStringNotContainsString( '<img', $output );
$this->assertStringContainsString( '<img', $output );
$this->assertStringContainsString( 'alt=""', $output );
$this->assertStringContainsString( 'width="1122"', $output );
$this->assertStringContainsString( 'height="1402"', $output );
$this->assertStringContainsString( 'loading="lazy"', $output );
$this->assertStringContainsString( 'decoding="async"', $output );
$unsafe = render_block(
array(
+129
View File
@@ -0,0 +1,129 @@
<?php
/**
* Tests for editable editorial patterns.
*
* @package CHRNX
*/
/** Verify pattern metadata, parsing, safety, and approved starter content. */
class PatternsTest extends WP_UnitTestCase {
/** Required pattern slugs. */
const PATTERNS = array(
'home-intro',
'home-projects',
'home-music',
'about',
'project-detail',
'music-landing',
'music-release',
'podcast-landing',
'podcast-show',
'podcast-episode',
);
/** Every pattern has valid metadata, parseable content, localization, and no unsafe material. */
public function test_all_patterns_are_parseable_localized_and_safe() {
foreach ( self::PATTERNS as $name ) {
$path = get_theme_file_path( 'patterns/' . $name . '.php' );
$this->assertFileExists( $path, $name );
$source = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
$this->assertMatchesRegularExpression( '/^\s*<\?php\s*\/\*\*.*?\*\sTitle:\s*.+$/ms', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sSlug:\s*chrnx\/' . preg_quote( $name, '/' ) . '\s*$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sCategories:\s*\S.+$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sDescription:\s*\S.+$/m', $source, $name );
$this->assertMatchesRegularExpression( '/^\s*\*\sInserter:\s*(?:yes|no)\s*$/m', $source, $name );
if ( preg_match( '/(?:__|_e)\s*\(/', $source ) ) {
$this->assertStringContainsString( "'chrnx'", $source, $name );
}
$content = $this->render_pattern( $path );
$blocks = parse_blocks( $content );
$this->assertNotEmpty( $blocks, $name );
$this->assertSame( trim( $content ), trim( serialize_blocks( $blocks ) ), $name );
foreach ( $blocks as $parsed_block ) {
if ( null === $parsed_block['blockName'] ) {
$this->assertSame( '', trim( $parsed_block['innerHTML'] ), $name . ' has non-block content' );
}
}
$this->assertStringContainsString( '<!-- wp:', $content, $name );
$this->assertStringNotContainsString( '<?php', $content, $name );
$this->assertDoesNotMatchRegularExpression( '/\b(?:TODO|lorem|example\.com)\b/i', $source . $content, $name );
$this->assertDoesNotMatchRegularExpression( '#(?:/Users/|/home/|\bSuno\b|\bstems?\b|voice model|draft lyrics?|prompt directory)#i', $source . $content, $name );
$this->assertDoesNotMatchRegularExpression( '#<(?:script|iframe|audio|form)\b|\[/?[a-z][^\]]*\]#i', $content, $name );
$this->assertDoesNotMatchRegularExpression( '/(?:Spotify|Apple Music|YouTube Music|Bandcamp)/i', $content, $name );
$this->assertDoesNotMatchRegularExpression( '/(?:Silicon Ranch|mailto:)/i', $content, $name );
$this->assertDoesNotMatchRegularExpression( '/<p[^>]*>[^<]*(?:add|enter|insert|replace|select|choose|TODO)[^<]*<\/p>/i', $content, $name );
}
}
/** Local art is generated through escaped theme URIs with intrinsic dimensions. */
public function test_art_patterns_use_only_escaped_local_theme_images() {
foreach ( array( 'home-music', 'music-landing', 'podcast-landing' ) as $name ) {
$source = file_get_contents( get_theme_file_path( 'patterns/' . $name . '.php' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
$this->assertStringContainsString( 'get_theme_file_uri(', $source, $name );
$this->assertStringContainsString( 'esc_url(', $source, $name );
$this->assertMatchesRegularExpression( '/<img\b[^>]*\bwidth="1122"[^>]*\bheight="1402"/i', $content, $name );
$this->assertMatchesRegularExpression( '/<img\b[^>]*\bloading="lazy"[^>]*\bdecoding="async"/i', $content, $name );
}
}
/** Music copy contains only the exact approved public facts and release statuses. */
public function test_music_patterns_use_approved_honest_copy() {
$content = $this->render_pattern( get_theme_file_path( 'patterns/music-landing.php' ) );
foreach ( array(
'Grunge mexicano para cerebros que no se callan.',
'grunge en español mexicano desde Sinaloa',
'estudio crudo, cero live',
'El Hermoso Caos que habita en mi cabeza',
'13-track debut album',
'Ando Bien Arriba',
'six-track bright mirrored EP',
'Aquí se Rompió una Jerga',
'six-track dark mirrored EP',
'draft/in production',
'generative AI',
'Chrnx',
) as $copy ) {
$this->assertStringContainsString( $copy, wp_strip_all_tags( $content ), $copy );
}
$this->assertDoesNotMatchRegularExpression( '/concert|rehearsal|coming live|tour/i', $content );
}
/** About copy is dignified, bounded, and contains the one approved external link. */
public function test_about_pattern_uses_approved_profile_copy_and_link() {
$source = file_get_contents( get_theme_file_path( 'patterns/about.php' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local test fixture.
$content = $this->render_pattern( get_theme_file_path( 'patterns/about.php' ) );
foreach ( array( 'autistic Level 1', 'independent researcher', 'software developer', 'emotion modeling', 'game AI', 'Lead Developer @Expressive Tea', 'JavaScript', 'Python', 'open source', 'amateur data science', 'father of three wonderful children', 'musician', 'gamer', 'pet-friendly' ) as $copy ) {
$this->assertStringContainsString( $copy, wp_strip_all_tags( $content ), $copy );
}
$this->assertStringContainsString( 'https://arxiv.org/abs/2603.05352', $content );
$this->assertStringContainsString( 'esc_url(', $source );
$this->assertStringContainsString( 'target="_blank"', $content );
$this->assertStringContainsString( 'rel="noopener noreferrer external"', $content );
$this->assertDoesNotMatchRegularExpression( '/diagnos|inspir|overcame|despite/i', $content );
}
/** Reusable project/release/show/episode structures do not invent visible starter data. */
public function test_structural_patterns_expose_named_editable_regions_without_fake_claims() {
$expectations = array(
'project-detail' => array( 'chrnx/project-status', 'chrnx/project-summary', 'chrnx/project-links', 'chrnx/project-technologies', 'chrnx/project-publications', 'chrnx/project-related-posts' ),
'music-release' => array( 'chrnx/release-title', 'chrnx/release-status', 'chrnx/release-summary', 'chrnx/release-artwork', 'chrnx/release-tracklist' ),
'podcast-show' => array( 'chrnx/show-cover', 'chrnx/show-description', 'chrnx/show-hosts', 'chrnx/show-latest-episode', 'chrnx/show-archive', 'chrnx/show-listening-destinations' ),
'podcast-episode' => array( 'chrnx/episode-show', 'chrnx/episode-number', 'chrnx/episode-duration', 'chrnx/episode-participants', 'chrnx/episode-canonical-url', 'chrnx/episode-embed', 'chrnx/episode-show-notes' ),
);
foreach ( $expectations as $name => $labels ) {
$content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) );
foreach ( $labels as $label ) {
$this->assertStringContainsString( '"metadata":{"name":"' . $label . '"}', $content, $name . ':' . $label );
}
}
}
/** Render a pattern file exactly as WordPress does. */
private function render_pattern( $path ) {
ob_start();
include $path;
return (string) ob_get_clean();
}
}
+104
View File
@@ -0,0 +1,104 @@
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(/<!--\s+(\/?)wp:([a-z0-9-]+(?:\/[a-z0-9-]+)?)(?:\s+\{.*?\})?\s*(\/?)-->/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(/<main\b/g) ?? []).length, 1);
assert.equal((markup.match(/<\/main>/g) ?? []).length, 1);
assert.match(markup, /<main\b[^>]*\bid="chrnx-main"[^>]*>/);
assert.doesNotMatch(markup, /wp:(?:legacy-widget|widget-group)|<aside\b|sidebar/i);
assert.doesNotMatch(markup, /<\?php|https?:\/\//i);
});
}
test('home composes editorial sections in the approved order', async () => {
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: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.doesNotMatch(markup, /"url":"\//);
});