import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { createServer } from 'node:http'; import { chmodSync, existsSync, linkSync, lstatSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync, } from 'node:fs'; import { execFileSync } from 'node:child_process'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { once } from 'node:events'; import { test } from 'node:test'; import { CAPTURE_PLAN, assertFinalNavigation, publishReviewEvidence, } from '../../scripts/capture-visual-review.mjs'; const read = ( path ) => readFileSync( new URL( `../../${ path }`, import.meta.url ), 'utf8' ); const PODCAST_BACKGROUND_SOURCE = 'assets/images/podcast-frequency-grunge.png'; const sha256 = ( path ) => createHash( 'sha256' ).update( readFileSync( path ) ).digest( 'hex' ); const png = ( width, height, marker ) => { const buffer = Buffer.alloc( 33, marker ); Buffer.from( '89504e470d0a1a0a', 'hex' ).copy( buffer, 0 ); buffer.writeUInt32BE( 13, 8 ); buffer.write( 'IHDR', 12, 'ascii' ); buffer.writeUInt32BE( width, 16 ); buffer.writeUInt32BE( height, 20 ); buffer[ 24 ] = 8; buffer[ 25 ] = 2; return buffer; }; test( 'local visual fixtures define stable pages and representative editorial states', () => { const pages = JSON.parse( read( 'tests/fixtures/pages.json' ) ); const posts = JSON.parse( read( 'tests/fixtures/posts.json' ) ); assert.deepEqual( pages.map( ( page ) => page.slug ), [ 'home', 'journal', 'projects', 'music', 'podcasts', 'about', 'subscribe' ], ); assert.ok( pages.every( ( page ) => page.fixtureKey && page.status === 'publish' ) ); assert.equal( posts.length, 18 ); assert.ok( posts.some( ( post ) => post.title.length > 100 ) ); assert.ok( posts.some( ( post ) => post.content.includes( 'wp:code' ) ) ); assert.ok( posts.some( ( post ) => post.fixtureState === 'missing-media' ) ); const hasState = ( post, state ) => post.fixtureState === state || post.fixtureStates?.includes( state ); assert.ok( posts.some( ( post ) => hasState( post, 'missing-podcast-meta' ) ) ); assert.ok( posts.some( ( post ) => hasState( post, 'missing-excerpt' ) && !( 'excerpt' in post ) ) ); assert.ok( posts.some( ( post ) => hasState( post, 'missing-topic' ) && !( 'topic' in post ) ) ); assert.ok( posts.some( ( post ) => hasState( post, 'missing-language' ) && !( 'language' in post ) ) ); assert.ok( posts.some( ( post ) => post.fixtureState === 'unbroken-code' && /[A-Z0-9]{256}/.test( post.content ) ) ); assert.ok( posts.some( ( post ) => /^https:\/\//.test( post.canonicalEpisodeUrl ?? '' ) ) ); assert.ok( posts.some( ( post ) => hasState( post, 'missing-podcast-meta' ) && !( 'canonicalEpisodeUrl' in post ) ) ); assert.equal( new Set( posts.map( ( post ) => post.fixtureKey ) ).size, posts.length ); const incompletePodcast = posts.find( ( post ) => post.fixtureKey === 'post-podcast-missing-meta' ); assert.equal( incompletePodcast?.topic, 'podcasts' ); assert.equal( incompletePodcast?.language, 'es' ); assert.ok( !( 'show' in incompletePodcast ) && !( 'canonicalEpisodeUrl' in incompletePodcast ) ); assert.ok( hasState( incompletePodcast, 'missing-excerpt' ) ); assert.ok( posts.some( ( post ) => post.fixtureKey !== incompletePodcast.fixtureKey && hasState( post, 'missing-topic' ) && !( 'topic' in post ) ) ); assert.ok( posts.some( ( post ) => post.fixtureKey !== incompletePodcast.fixtureKey && hasState( post, 'missing-language' ) && !( 'language' in post ) ) ); assert.ok( posts.some( ( post ) => post.topic === 'projects' && post.projectFeatured === true ) ); assert.ok( posts.some( ( post ) => post.topic === 'projects' && ! ( 'projectFeatured' in post ) ) ); // Tracks must carry a release and an order for the album tracklist to mean anything. const tracks = posts.filter( ( post ) => 'release' in post ); assert.ok( tracks.length >= 3 ); assert.ok( tracks.every( ( post ) => post.topic === 'music' && Number.isInteger( post.trackNumber ) ) ); assert.ok( new Set( tracks.map( ( post ) => post.release ) ).size >= 2 ); // No invented listening data: fixtures never ship platform URLs or audio. assert.ok( ! posts.some( ( post ) => Object.keys( post ).some( ( key ) => key.startsWith( 'trackUrl' ) ) ) ); for ( const topic of [ 'code', 'research', 'gaming', 'literature', 'music', 'podcasts', 'life', 'projects' ] ) { assert.ok( posts.some( ( post ) => post.topic === topic ), `missing ${ topic } fixture` ); } } ); test( 'the seed entrypoint is explicitly local-only and uses a stable fixture key', () => { const shell = read( 'scripts/seed-test-content.sh' ); const php = read( 'scripts/seed-test-content.php' ); assert.match( shell, /localhost|127\.0\.0\.1/ ); assert.match( shell, /wp eval-file/ ); assert.match( shell, /featured-1/ ); assert.match( shell, /subscribe-unavailable/ ); assert.match( php, /_chrnx_fixture_key/ ); assert.match( php, /wp_delete_post/ ); assert.match( php, /canonical_episode_url/ ); assert.match( php, /show_on_front/ ); assert.match( php, /page_on_front/ ); assert.match( php, /page_for_posts/ ); } ); test( 'search and not-found recovery landmarks declare distinct accessible names', () => { const search = read( 'templates/search.html' ); const missing = read( 'templates/404.html' ); assert.match( search, /"ariaLabel":"Search recovery navigation"/ ); assert.match( missing, /"ariaLabel":"Page not found recovery navigation"/ ); assert.notEqual( search.match( /"ariaLabel":"([^"]+)"/ )?.[ 1 ], missing.match( /"ariaLabel":"([^"]+)"/ )?.[ 1 ] ); } ); test( 'the visual layer is separately enqueued and covers responsive accessible states', () => { const assets = read( 'inc/assets.php' ); const components = read( 'assets/css/components.css' ); assert.match( assets, /chrnx-components/ ); assert.match( components, /\.chrnx-section-portrait/ ); assert.match( components, /prefers-reduced-motion/ ); assert.match( components, /@media \(max-width: 599px\)/ ); assert.match( components, /html\[data-theme="light"\]/ ); } ); test( 'grunge stage capture exposes exactly 24 approved routes, widths, themes, and filenames', () => { assert.equal( CAPTURE_PLAN.length, 24 ); assert.equal( new Set( CAPTURE_PLAN.map( ( capture ) => capture.filename ) ).size, 24 ); assert.deepEqual( [ ...new Set( CAPTURE_PLAN.map( ( capture ) => capture.target.path ) ) ].sort(), [ '/music/', '/podcasts/' ] ); assert.deepEqual( [ ...new Set( CAPTURE_PLAN.map( ( capture ) => capture.viewport.width ) ) ], [ 375, 768, 1440, 2560 ] ); assert.deepEqual( [ ...new Set( CAPTURE_PLAN.map( ( capture ) => capture.theme ) ) ], [ 'light', 'dark' ] ); for ( const capture of CAPTURE_PLAN ) { assert.equal( capture.filename, `grunge-stage-${ capture.target.name }-${ capture.viewport.width }-${ capture.theme }.png`, ); } } ); test( 'media and review documentation matches the current inventory and cleanup paths', () => { const readme = read( 'README.md' ); const plan = read( 'docs/superpowers/plans/2026-08-13-music-podcast-grunge-stages.md' ); assert.match( readme, /76 generated files: 62 binary derivatives and 14 manifests/ ); assert.match( readme, /eleven portrait families at 384 px and 768 px/ ); assert.match( readme, /three\s+landscape background families \(Podcast, About, and Projects\) at 384 px, 768 px,\s+and 1536 px/ ); for ( const budget of [ '160 KiB', '260 KiB', '400 KiB', '340 KiB', '620 KiB', '3 MiB' ] ) { assert.ok( readme.includes( budget ), `README missing ${ budget } budget` ); } assert.doesNotMatch( readme, /all 40 outputs|2\.1 MiB/ ); assert.match( readme, /transactional publication with rollback/ ); assert.match( plan, /Create temporarily, never commit: `tmp\/imagegen\/grunge-stage-review\.png\/`/ ); assert.match( plan, /find tmp\/imagegen\/grunge-stage-review\.png -maxdepth 1 -type f -name 'grunge-stage-\*\.png' -print \| sort/, ); assert.match( plan, /test "\$\(find tmp\/imagegen\/grunge-stage-review\.png -maxdepth 1 -type f -name 'grunge-stage-\*\.png' -print \| wc -l \| tr -d ' '\)" -eq 24/, ); assert.match( plan, /lstatSync\( 'tmp\/imagegen\/grunge-stage-review\.png' \)/ ); assert.match( plan, /trash tmp\/imagegen\/grunge-stage-review\.png/ ); assert.match( plan, /test ! -e tmp\/imagegen\/grunge-stage-review\.png/ ); assert.doesNotMatch( plan, /tmp\/imagegen\/grunge-stage-\*\.png/ ); assert.match( plan, /transactional publication with rollback/ ); } ); test( 'grunge stage capture isolates every image and waits for stable rendered assets', () => { const capture = read( 'scripts/capture-visual-review.mjs' ); assert.ok( capture.indexOf( 'const browser = await chromium.launch()' ) > capture.indexOf( 'async function capture' ), 'Chromium must launch inside capture so a dark render cannot corrupt the next light screenshot', ); assert.match( capture, /const browser = await chromium\.launch\(\);[\s\S]*?try \{[\s\S]*?finally \{\s*try \{\s*await context\?\.close\(\);\s*\} finally \{\s*await browser\.close\(\);\s*\}\s*\}/, 'Every isolated browser must close even when navigation, capture, or context cleanup fails', ); assert.match( capture, /await document\.fonts\.ready/ ); assert.match( capture, /image\.complete/ ); assert.match( capture, /image\.naturalWidth/ ); assert.match( capture, /image\.decode\(\)/ ); assert.match( capture, /reducedMotion: 'reduce'/ ); assert.match( capture, /page\.setDefaultTimeout\( 5_000 \)/ ); assert.match( capture, /page\.setDefaultNavigationTimeout\( 15_000 \)/ ); assert.match( capture, /LOCAL_HOSTS/ ); assert.match( capture, /route\.abort\( 'blockedbyclient' \)/ ); assert.equal( capture.match( /const browser = await chromium\.launch\(\)/g )?.length, 1 ); assert.match( capture, /for \( const capture of capturePlan \)[\s\S]*await captureImage/ ); } ); test( 'grunge stage transactional publication preserves the prior set after capture failure', async () => { const parent = mkdtempSync( join( tmpdir(), 'chrnx-review-parent-' ) ); const output = join( parent, 'grunge-stage-review.png' ); try { await publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => writeFileSync( path, png( capture.viewport.width, 10, 1 ) ), } ); const before = new Map( readdirSync( output ).map( ( name ) => [ name, sha256( join( output, name ) ) ] ) ); let count = 0; await assert.rejects( publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => { count++; if ( count === 9 ) { throw new Error( 'injected capture failure' ); } writeFileSync( path, png( capture.viewport.width, 11, 2 ) ); }, } ), /injected capture failure/, ); assert.deepEqual( readdirSync( output ).sort(), CAPTURE_PLAN.map( ( capture ) => capture.filename ).sort() ); for ( const [ name, hash ] of before ) { assert.equal( sha256( join( output, name ) ), hash, `${ name } changed after failed capture` ); } assert.deepEqual( readdirSync( parent ).sort(), [ 'grunge-stage-review.png' ] ); } finally { rmSync( parent, { recursive: true, force: true } ); } } ); test( 'grunge stage promotion failure restores the complete prior set', async () => { const parent = mkdtempSync( join( tmpdir(), 'chrnx-review-promotion-parent-' ) ); const output = join( parent, 'grunge-stage-review.png' ); try { await publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => writeFileSync( path, png( capture.viewport.width, 10, 3 ) ), } ); const before = new Map( readdirSync( output ).map( ( name ) => [ name, sha256( join( output, name ) ) ] ) ); await assert.rejects( publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => writeFileSync( path, png( capture.viewport.width, 11, 4 ) ), renameEntry: async ( source, destination ) => { if ( source.includes( '.grunge-stage-staging-' ) && destination === output ) { throw new Error( 'injected review promotion failure' ); } renameSync( source, destination ); }, } ), /injected review promotion failure/, ); assert.deepEqual( readdirSync( output ).sort(), [ ...before.keys() ].sort() ); for ( const [ name, hash ] of before ) { assert.equal( sha256( join( output, name ) ), hash, `${ name } changed after promotion failure` ); } assert.deepEqual( readdirSync( parent ).sort(), [ 'grunge-stage-review.png' ] ); } finally { rmSync( parent, { recursive: true, force: true } ); } } ); test( 'grunge stage backup cleanup retries without invalidating a published set', async () => { const parent = mkdtempSync( join( tmpdir(), 'chrnx-review-cleanup-parent-' ) ); const output = join( parent, 'grunge-stage-review.png' ); try { await publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => writeFileSync( path, png( capture.viewport.width, 10, 5 ) ), } ); const before = new Map( readdirSync( output ).map( ( name ) => [ name, sha256( join( output, name ) ) ] ) ); let cleanupAttempts = 0; await publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async ( capture, path ) => writeFileSync( path, png( capture.viewport.width, 12, 6 ) ), removeEntry: async ( path, options ) => { if ( path.endsWith( '.grunge-stage-backup' ) && cleanupAttempts++ === 0 ) { throw new Error( 'injected review backup cleanup failure' ); } rmSync( path, options ); }, } ); assert.equal( cleanupAttempts, 2 ); assert.deepEqual( readdirSync( parent ).sort(), [ 'grunge-stage-review.png' ] ); assert.deepEqual( readdirSync( output ).sort(), [ ...before.keys() ].sort() ); for ( const [ name, hash ] of before ) { assert.notEqual( sha256( join( output, name ) ), hash, `${ name } did not publish after cleanup retry` ); } } finally { rmSync( parent, { recursive: true, force: true } ); } } ); test( 'grunge stage publication rejects output and file aliases without touching sentinels', async () => { const parent = mkdtempSync( join( tmpdir(), 'chrnx-review-alias-parent-' ) ); const external = mkdtempSync( join( tmpdir(), 'chrnx-review-alias-external-' ) ); const parentAlias = `${ parent }-alias`; const output = join( parent, 'grunge-stage-review.png' ); const sentinel = join( external, 'sentinel' ); writeFileSync( sentinel, 'unchanged' ); try { symlinkSync( parent, parentAlias ); await assert.rejects( publishReviewEvidence( { outputDirectory: join( parentAlias, 'grunge-stage-review.png' ), allowedParentDirectory: parentAlias, capturePlan: CAPTURE_PLAN, captureImage: async () => {}, } ), /symbolic link/, ); symlinkSync( external, output ); await assert.rejects( publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async () => {}, } ), /symbolic link/, ); assert.equal( readFileSync( sentinel, 'utf8' ), 'unchanged' ); rmSync( output ); mkdirSync( output ); symlinkSync( sentinel, join( output, CAPTURE_PLAN[ 0 ].filename ) ); await assert.rejects( publishReviewEvidence( { outputDirectory: output, allowedParentDirectory: parent, capturePlan: CAPTURE_PLAN, captureImage: async () => {}, } ), /symbolic link/, ); assert.equal( readFileSync( sentinel, 'utf8' ), 'unchanged' ); } finally { rmSync( parentAlias, { force: true } ); rmSync( parent, { recursive: true, force: true } ); rmSync( external, { recursive: true, force: true } ); } } ); test( 'final navigation rejects local redirects to a wrong review path or origin before capture', async () => { const otherServer = createServer( ( request, response ) => { response.writeHead( 200, { 'Content-Type': 'text/plain' } ); response.end( request.url ); } ); otherServer.listen( 0, '127.0.0.1' ); await once( otherServer, 'listening' ); const server = createServer( ( request, response ) => { if ( request.url === '/music/' ) { response.writeHead( 302, { Location: '/wrong/' } ); response.end(); return; } if ( request.url === '/podcasts/' ) { response.writeHead( 302, { Location: `http://127.0.0.1:${ otherServer.address().port }/podcasts/` } ); response.end(); return; } response.writeHead( 200, { 'Content-Type': 'text/plain' } ); response.end( 'wrong path' ); } ); server.listen( 0, '127.0.0.1' ); await once( server, 'listening' ); try { const { port } = server.address(); const baseURL = new URL( `http://127.0.0.1:${ port }` ); const response = await fetch( new URL( '/music/', baseURL ), { redirect: 'follow' } ); assert.throws( () => assertFinalNavigation( baseURL, '/music/', response.url ), /unexpected path/ ); const crossOrigin = await fetch( new URL( '/podcasts/', baseURL ), { redirect: 'follow' } ); assert.throws( () => assertFinalNavigation( baseURL, '/podcasts/', crossOrigin.url ), /unexpected origin/ ); } finally { await Promise.all( [ server.closeAllConnections(), otherServer.closeAllConnections() ] ); await Promise.all( [ new Promise( ( resolve ) => server.close( resolve ) ), new Promise( ( resolve ) => otherServer.close( resolve ) ), ] ); } } ); test( 'long-copy review mutates only the rendered latest podcast title and excerpt', () => { const capture = read( 'scripts/capture-visual-review.mjs' ); const titleSelector = '.chrnx-latest-podcast .chrnx-podcast-card__title'; const excerptSelector = '.chrnx-latest-podcast .chrnx-podcast-card__excerpt'; assert.ok( capture.includes( `titleSelector: '${ titleSelector }'` ) ); assert.ok( capture.includes( `excerptSelector: '${ excerptSelector }'` ) ); assert.match( capture, /target\.longCopy[\s\S]*locator\( target\.longCopy\.titleSelector \)[\s\S]*locator\( target\.longCopy\.excerptSelector \)/ ); assert.match( capture, /element\.textContent = value/ ); assert.doesNotMatch( capture, /wp-json|fetch\(|XMLHttpRequest|wp eval|seed-test-content/ ); } ); test( 'the page canvas is atmospheric rather than a repeating grid', () => { const components = read( 'assets/css/components.css' ); assert.doesNotMatch( components, /linear-gradient\([^)]*1px,\s*transparent\s+1px\)[\s\S]*linear-gradient\(90deg,[^)]*1px,\s*transparent\s+1px\)/, ); assert.doesNotMatch( components, /background-size:\s*[^;]*(?:2|3)rem\s+(?:2|3)rem(?:\s*,\s*(?:2|3)rem\s+(?:2|3)rem)?/, ); } ); test( 'lower home sections preserve editorial order and define layered panel treatments', () => { const front = read( 'templates/front-page.html' ); const homeMusic = read( 'patterns/home-music.php' ); const components = read( 'assets/css/components.css' ); const orderedSections = [ 'chrnx/home-projects', 'chrnx/home-music', 'chrnx/latest-podcast', 'chrnx-home-about-preview', 'chrnx/subscribe-cta', ]; let previousIndex = -1; for ( const section of orderedSections ) { const currentIndex = front.indexOf( section ); assert.ok( currentIndex > previousIndex, `${ section } must keep its semantic position` ); previousIndex = currentIndex; } assert.match( homeMusic, /chrnx_get_topic_portrait_markup\( 'music-collage'/ ); assert.match( homeMusic, /chrnx-character-stage__atmosphere/ ); assert.doesNotMatch( components, /url\([^)]*the-no-names-collage\.jpeg/ ); assert.match( components, /\.chrnx-home-music \.wp-block-column:last-child \.chrnx-responsive-portrait\s*\{[\s\S]*?(?:filter|box-shadow|mask(?:-image)?):/, ); for ( const selector of [ '.chrnx-home-projects', '.chrnx-latest-podcast', '.chrnx-home-about-preview', '.chrnx-subscribe-cta', ] ) { const escapedSelector = selector.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); assert.match( components, new RegExp( `${ escapedSelector }[^,{]*\\{[\\s\\S]*?background(?:-color|-image)?:` ), `${ selector } needs an authored surface`, ); } } ); test( 'all approved responsive media rebuild byte-identically within payload bounds', () => { const readme = read( 'README.md' ); const documentedCommand = 'mktemp -d "${TMPDIR:-/tmp}/chrnx-responsive.XXXXXX"'; assert.ok( readme.includes( documentedCommand ) ); const output = execFileSync( 'sh', [ '-c', documentedCommand ], { encoding: 'utf8' } ).trim(); const unsafeOutput = join( output, 'unsafe-output' ); const external = mkdtempSync( join( tmpdir(), 'chrnx-external-' ) ); const symlink = join( tmpdir(), `chrnx-symlink-${ process.pid }` ); try { assert.throws( () => execFileSync( './scripts/build-responsive-images.sh', [ unsafeOutput ], { cwd: new URL( '../..', import.meta.url ) } ) ); writeFileSync( join( external, 'sentinel' ), 'unchanged' ); symlinkSync( external, symlink ); assert.throws( () => execFileSync( './scripts/build-responsive-images.sh', [ symlink ], { cwd: new URL( '../..', import.meta.url ) } ) ); assert.deepEqual( readdirSync( external ), [ 'sentinel' ] ); execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: new URL( '../..', import.meta.url ) } ); const names = readdirSync( output ).sort(); assert.equal( names.length, 76 ); for ( const name of [ 'alter-ego-gaming-half-body-384.avif', 'alter-ego-gaming-half-body-384.webp', 'alter-ego-gaming-half-body-768.avif', 'alter-ego-gaming-half-body-768.webp', 'alter-ego-gaming-half-body.json', 'the-no-names-collage-384.avif', 'the-no-names-collage-384.webp', 'the-no-names-collage-768.avif', 'the-no-names-collage-768.webp', 'the-no-names-collage.json', 'podcast-frequency-grunge-384.avif', 'podcast-frequency-grunge-384.webp', 'podcast-frequency-grunge-768.avif', 'podcast-frequency-grunge-768.webp', 'podcast-frequency-grunge-1536.avif', 'podcast-frequency-grunge-1536.webp', 'podcast-frequency-grunge.json', 'about-grunge-1536.avif', 'about-grunge-1536.webp', 'about-grunge.json', 'projects-grunge-1536.avif', 'projects-grunge-1536.webp', 'projects-grunge.json', 'placeholder-post-384.avif', 'placeholder-post-768.webp', 'placeholder-post.json', 'placeholder-release-384.avif', 'placeholder-release-768.webp', 'placeholder-release.json', ] ) { assert.ok( names.includes( name ), `missing ${ name }` ); } assert.deepEqual( JSON.parse( readFileSync( join( output, 'podcast-frequency-grunge.json' ), 'utf8' ) ), { source: 'podcast-frequency-grunge.png', formats: [ 'avif', 'webp' ], widths: [ 384, 768, 1536 ], }, ); for ( const backdrop of [ 'about-grunge', 'projects-grunge' ] ) { assert.deepEqual( JSON.parse( readFileSync( join( output, `${ backdrop }.json` ), 'utf8' ) ), { source: `${ backdrop }.png`, formats: [ 'avif', 'webp' ], widths: [ 384, 768, 1536 ], }, ); } let total = 0; let collageTotal = 0; let gamingHalfBodyTotal = 0; let podcastTotal = 0; for ( const name of names ) { const generated = readFileSync( join( output, name ) ); const committed = readFileSync( new URL( `../../assets/images/generated/${ name }`, import.meta.url ) ); assert.deepEqual( generated, committed, `${ name } must rebuild byte-identically` ); if ( /\.(?:avif|webp)$/.test( name ) ) { const binarySize = statSync( join( output, name ) ).size; const expectedWidth = Number( name.match( /-(384|768|1536)\.(?:avif|webp)$/ )[ 1 ] ); const binaryLimit = expectedWidth === 1536 ? 260 * 1024 : 160 * 1024; assert.ok( binarySize <= binaryLimit, `${ name } exceeds ${ binaryLimit } bytes: ${ binarySize } bytes` ); const dimensions = execFileSync( 'sips', [ '-g', 'pixelWidth', '-g', 'pixelHeight', join( output, name ) ], { encoding: 'utf8' } ); assert.match( dimensions, new RegExp( `pixelWidth: ${ expectedWidth }` ) ); assert.match( dimensions, /pixelHeight: [1-9][0-9]*/ ); if ( name.endsWith( '.webp' ) ) { assert.equal( generated.subarray( 0, 4 ).toString(), 'RIFF' ); assert.equal( generated.subarray( 8, 12 ).toString(), 'WEBP' ); } else { assert.equal( generated.subarray( 4, 12 ).toString(), 'ftypavif' ); } total += generated.length; if ( name.startsWith( 'the-no-names-collage-' ) ) { collageTotal += generated.length; } if ( name.startsWith( 'alter-ego-gaming-half-body-' ) ) { gamingHalfBodyTotal += generated.length; } if ( name.startsWith( 'podcast-frequency-grunge-' ) ) { podcastTotal += generated.length; } } } assert.ok( collageTotal <= 400 * 1024, `collage family exceeds 400 KiB: ${ collageTotal } total bytes` ); assert.ok( gamingHalfBodyTotal <= 340 * 1024, `gaming half-body family exceeds 340 KiB: ${ gamingHalfBodyTotal } total bytes`, ); assert.ok( podcastTotal <= 620 * 1024, `podcast-frequency-grunge family exceeds 620 KiB: ${ podcastTotal } total bytes`, ); assert.ok( total <= 3 * 1024 * 1024, `generated binary payload exceeds 3 MiB: ${ total } total bytes` ); } finally { rmSync( output, { recursive: true, force: true } ); rmSync( external, { recursive: true, force: true } ); rmSync( symlink, { force: true } ); } } ); test( 'responsive rebuild replaces output aliases without mutating external files or the master', () => { const output = mkdtempSync( join( tmpdir(), 'chrnx-alias-output-' ) ); const external = mkdtempSync( join( tmpdir(), 'chrnx-alias-external-' ) ); const projectRoot = new URL( '../..', import.meta.url ); const master = new URL( `../../${ PODCAST_BACKGROUND_SOURCE }`, import.meta.url ); const symlinkSentinel = join( external, 'symlink-sentinel.webp' ); const hardlinkSentinel = join( external, 'hardlink-sentinel.webp' ); const symlinkOutput = join( output, 'podcast-frequency-grunge-384.webp' ); const hardlinkOutput = join( output, 'podcast-frequency-grunge-768.webp' ); const symlinkBytes = Buffer.from( 'external-symlink-sentinel' ); const hardlinkBytes = Buffer.from( 'external-hardlink-sentinel' ); const masterHash = sha256( master ); let hardlinkSupported = false; try { execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot } ); writeFileSync( symlinkSentinel, symlinkBytes ); writeFileSync( hardlinkSentinel, hardlinkBytes ); const symlinkHash = sha256( symlinkSentinel ); const hardlinkHash = sha256( hardlinkSentinel ); unlinkSync( symlinkOutput ); symlinkSync( symlinkSentinel, symlinkOutput ); unlinkSync( hardlinkOutput ); try { linkSync( hardlinkSentinel, hardlinkOutput ); hardlinkSupported = true; } catch ( error ) { if ( ! [ 'ENOTSUP', 'EOPNOTSUPP', 'EXDEV' ].includes( error.code ) ) { throw error; } } execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot } ); assert.equal( sha256( symlinkSentinel ), symlinkHash ); assert.equal( lstatSync( symlinkOutput ).isSymbolicLink(), false ); assert.equal( lstatSync( symlinkOutput ).isFile(), true ); assert.notEqual( statSync( symlinkOutput ).ino, statSync( symlinkSentinel ).ino ); if ( hardlinkSupported ) { assert.equal( sha256( hardlinkSentinel ), hardlinkHash ); assert.equal( lstatSync( hardlinkOutput ).isSymbolicLink(), false ); assert.equal( lstatSync( hardlinkOutput ).isFile(), true ); assert.notEqual( statSync( hardlinkOutput ).ino, statSync( hardlinkSentinel ).ino ); } assert.equal( sha256( master ), masterHash ); } finally { rmSync( output, { recursive: true, force: true } ); rmSync( external, { recursive: true, force: true } ); } } ); test( 'encoder failure cannot partially replace an existing responsive inventory', () => { const output = mkdtempSync( join( tmpdir(), 'chrnx-failure-output-' ) ); const fakeBin = mkdtempSync( join( tmpdir(), 'chrnx-fake-bin-' ) ); const projectRoot = new URL( '../..', import.meta.url ); const master = new URL( `../../${ PODCAST_BACKGROUND_SOURCE }`, import.meta.url ); const masterHash = sha256( master ); const fakeCwebp = join( fakeBin, 'cwebp' ); const counter = join( fakeBin, 'counter' ); try { execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot } ); const published = new Map( readdirSync( output ).map( ( name ) => [ name, sha256( join( output, name ) ) ] ) ); writeFileSync( fakeCwebp, `#!/bin/sh set -eu count=0 if [ -f "$CHRNX_TEST_CWEBP_COUNTER" ]; then count=$(cat "$CHRNX_TEST_CWEBP_COUNTER") fi count=$((count + 1)) printf '%s\n' "$count" > "$CHRNX_TEST_CWEBP_COUNTER" if [ "$count" -eq 3 ]; then exit 73 fi output= while [ "$#" -gt 0 ]; do if [ "$1" = "-o" ]; then shift output=$1 fi shift done printf 'partial-build-%s\n' "$count" > "$output" `, ); chmodSync( fakeCwebp, 0o755 ); assert.throws( () => execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot, env: { ...process.env, PATH: `${ fakeBin }:${ process.env.PATH }`, CHRNX_TEST_CWEBP_COUNTER: counter, }, } ), ); assert.deepEqual( readdirSync( output ).sort(), [ ...published.keys() ].sort() ); assert.equal( readFileSync( counter, 'utf8' ).trim(), '3' ); for ( const [ name, hash ] of published ) { assert.equal( sha256( join( output, name ) ), hash, `${ name } changed after encoder failure` ); } assert.equal( sha256( master ), masterHash ); } finally { rmSync( output, { recursive: true, force: true } ); rmSync( fakeBin, { recursive: true, force: true } ); } } ); test( 'responsive promotion failure restores all 76 prior files', () => { const output = mkdtempSync( join( tmpdir(), 'chrnx-promotion-output-' ) ); const fakeBin = mkdtempSync( join( tmpdir(), 'chrnx-promotion-bin-' ) ); const external = mkdtempSync( join( tmpdir(), 'chrnx-promotion-external-' ) ); const projectRoot = new URL( '../..', import.meta.url ); const master = new URL( `../../${ PODCAST_BACKGROUND_SOURCE }`, import.meta.url ); const masterHash = sha256( master ); const sentinel = join( external, 'sentinel' ); const fakeMv = join( fakeBin, 'mv' ); const counter = join( fakeBin, 'counter' ); try { execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot } ); const published = new Map( readdirSync( output ).map( ( name ) => [ name, sha256( join( output, name ) ) ] ) ); assert.equal( published.size, 76 ); writeFileSync( sentinel, 'external-sentinel' ); const sentinelHash = sha256( sentinel ); const realMv = execFileSync( '/bin/sh', [ '-c', 'command -v mv' ], { encoding: 'utf8' } ).trim(); writeFileSync( fakeMv, `#!/bin/sh set -eu source_path=$3 destination_path=$4 case "$source_path" in */.chrnx-stage.*/*) case "$source_path" in */.backup/*|*/.discard/*) ;; *) case "$destination_path" in "$CHRNX_TEST_OUTPUT"/*) count=0 if [ -f "$CHRNX_TEST_MV_COUNTER" ]; then count=$(cat "$CHRNX_TEST_MV_COUNTER"); fi count=$((count + 1)) printf '%s\n' "$count" > "$CHRNX_TEST_MV_COUNTER" if [ "$count" -eq 3 ]; then exit 73; fi ;; esac ;; esac ;; esac exec "$CHRNX_TEST_REAL_MV" "$@" `, ); chmodSync( fakeMv, 0o755 ); assert.throws( () => execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: projectRoot, env: { ...process.env, PATH: `${ fakeBin }:${ process.env.PATH }`, CHRNX_TEST_MV_COUNTER: counter, CHRNX_TEST_OUTPUT: realpathSync( output ), CHRNX_TEST_REAL_MV: realMv, }, } ), ); assert.equal( readFileSync( counter, 'utf8' ).trim(), '3' ); assert.deepEqual( readdirSync( output ).sort(), [ ...published.keys() ].sort() ); for ( const [ name, hash ] of published ) { assert.equal( sha256( join( output, name ) ), hash, `${ name } changed after promotion failure` ); } assert.equal( sha256( master ), masterHash ); assert.equal( sha256( sentinel ), sentinelHash ); } finally { rmSync( output, { recursive: true, force: true } ); rmSync( fakeBin, { recursive: true, force: true } ); rmSync( external, { recursive: true, force: true } ); } } ); test( 'post-card CSS fallbacks never reuse approved section portraits', () => { const components = read( 'assets/css/components.css' ); assert.doesNotMatch( components, /url\([^)]*(?:alter-ego-|the-no-names-musician-cutout)/ ); } ); test( 'podcast background source uses fixed art and stays separate from card fallbacks', () => { assert.ok( existsSync( new URL( `../../${ PODCAST_BACKGROUND_SOURCE }`, import.meta.url ) ), `missing ${ PODCAST_BACKGROUND_SOURCE }`, ); const artAllowlist = read( 'inc/queries.php' ); assert.match( artAllowlist, /'podcast-frequency'\s*=>\s*'podcast-frequency-grunge\.png'/ ); for ( const path of [ 'assets/css/components.css', 'blocks/featured-stories/style.css', 'blocks/journal-stream/style.css', 'blocks/latest-podcast/style.css', 'blocks/related-posts/style.css', ] ) { assert.doesNotMatch( read( path ), /podcast-frequency-grunge\.png/, `${ PODCAST_BACKGROUND_SOURCE } must not become a card fallback in ${ path }`, ); } } ); test( 'front page and section pages connect the real editorial patterns', () => { const front = read( 'templates/front-page.html' ); assert.match( front, /chrnx\/home-intro/ ); assert.match( front, /chrnx\/featured-stories/ ); assert.match( front, /chrnx\/journal-stream/ ); assert.match( front, /chrnx\/latest-podcast/ ); const fixtures = read( 'tests/fixtures/pages.json' ); for ( const slug of [ 'chrnx/about', 'chrnx/music-landing', 'chrnx/podcast-landing' ] ) { assert.match( fixtures, new RegExp( slug.replace( '/', '\\/' ) ) ); } } ); test( 'character-led openings share the exact stage class contract', () => { for ( const path of [ 'patterns/home-projects.php', 'patterns/home-music.php', 'patterns/music-landing.php', 'patterns/podcast-landing.php', 'patterns/about.php', 'blocks/topic-hero/render.php', ] ) { const source = read( path ); const stageDeclaration = path.startsWith( 'patterns/' ) ? /className":"chrnx-character-stage"/g : /class="chrnx-character-stage"/g; assert.equal( ( source.match( stageDeclaration ) || [] ).length, 1, path ); for ( const className of [ 'chrnx-character-stage__atmosphere', 'chrnx-character-stage__content', 'chrnx-character-stage__art', ] ) { assert.match( source, new RegExp( className ), `${ path } missing ${ className }` ); } } const musicLanding = read( 'patterns/music-landing.php' ); assert.match( musicLanding, /chrnx_get_topic_portrait_markup\( 'music-collage', __\( 'The No Names collage', 'chrnx' \), 'chrnx-character-stage__collage' \)/ ); assert.doesNotMatch( musicLanding, / { const music = read( 'patterns/music-landing.php' ); const podcast = read( 'patterns/podcast-landing.php' ); const pages = JSON.parse( read( 'tests/fixtures/pages.json' ) ); const assertLayerOrder = ( source, label ) => { const stage = source.indexOf( 'className":"chrnx-character-stage"' ); const atmosphere = source.indexOf( 'chrnx-character-stage__atmosphere', stage ); const content = source.indexOf( 'chrnx-character-stage__content', atmosphere ); const art = source.indexOf( 'chrnx-character-stage__art', content ); assert.ok( stage >= 0 && atmosphere > stage && content > atmosphere && art > content, label ); }; assertLayerOrder( music, 'Music layer order' ); assert.equal( ( music.match( /chrnx_get_topic_portrait_markup\( 'music-collage'/g ) || [] ).length, 1 ); assert.equal( ( music.match( /chrnx_get_topic_portrait_markup\( 'music',/g ) || [] ).length, 1 ); assert.doesNotMatch( music.match( /chrnx-character-stage__atmosphere[\s\S]*?chrnx-character-stage__content/ )?.[ 0 ] ?? '', /aria-hidden="true"/, ); assert.ok( music.indexOf( 'chrnx_get_topic_portrait_markup( \'music-collage\'' ) < music.indexOf( 'chrnx-character-stage__content' ), ); assert.ok( music.indexOf( 'El proceso usa generative AI' ) > music.indexOf( '', music.indexOf( 'chrnx-character-stage__art' ) ) && music.indexOf( 'El proceso usa generative AI' ) < music.indexOf( 'Releases in progress' ), ); assertLayerOrder( podcast, 'Podcast layer order' ); assert.equal( ( podcast.match( /chrnx_get_atmosphere_markup\( 'podcast-frequency'/g ) || [] ).length, 1 ); assert.equal( ( podcast.match( /chrnx_get_topic_portrait_markup\( 'podcasts'/g ) || [] ).length, 1 ); assert.equal( ( podcast.match( //g ) || [] ).length, 1 ); assert.match( podcast, /chrnx-character-stage__atmosphere" aria-hidden="true"/ ); assert.match( podcast, /chrnx\/podcast-show-discovery/ ); assert.match( podcast, /chrnx\/podcast-archive/ ); assert.doesNotMatch( podcast, /chrnx\/podcast-latest/ ); assert.equal( pages.length, 7 ); const podcastPage = pages.find( ( page ) => page.slug === 'podcasts' ); assert.equal( podcastPage?.content, '', ); } ); test( 'editor pattern validity is derived only from the live block-editor store', () => { const editorValidity = read( 'tests/e2e/pattern-editor-validity.spec.mjs' ); assert.doesNotMatch( editorValidity, /resolvePatternReferences|shouldResolvePatterns/ ); assert.doesNotMatch( editorValidity, /editorSnapshot[\s\S]*?wp\.apiFetch\(\s*\{\s*path:\s*'\/wp\/v2\/block-patterns\/patterns'/, ); assert.match( editorValidity, /const roots = store\.getBlocks\(\)/ ); assert.doesNotMatch( editorValidity, /__experimentalGetParsedPattern/ ); assert.doesNotMatch( editorValidity, /\.resetBlocks\s*\(/ ); assert.doesNotMatch( editorValidity, /\.insertBlocks\s*\(/ ); assert.match( editorValidity, /getByRole\( 'button', \{ name: 'Block Inserter' \} \)/ ); assert.match( editorValidity, /getByRole\( 'tab', \{ name: 'Blocks'/ ); assert.match( editorValidity, /getByRole\( 'option', \{[\s\S]*?name: pattern\.patternTitle/, ); assert.match( editorValidity, /\[ 'POST', 'PUT', 'PATCH', 'DELETE' \]/ ); assert.match( editorValidity, /expect\( templateAfter \)\.toEqual\( templateBefore \)/ ); } ); test( 'Music and Podcast visual states decode every relevant image before measuring', () => { const visualStates = read( 'tests/e2e/visual-states.spec.mjs' ); assert.match( visualStates, /async function waitForDecodedLandingImages/ ); assert.doesNotMatch( visualStates, /elements\.length >= 2/ ); assert.match( visualStates, /for \( const imageExpectation of imageExpectations \)/ ); assert.match( visualStates, /toHaveCount\(\s*imageExpectation\.count/ ); assert.match( visualStates, /latestMedia[^]*?toHaveCount\( 1 \)/ ); assert.match( visualStates, /latestMediaKind === 'IMG'[^]*?decodeImages\( latestMedia \)/ ); assert.match( visualStates, /chrnx-podcast-card__fallback[^]*?aria-hidden[^]*?'true'/ ); assert.match( visualStates, /chrnx-latest-podcast img' \) \)\.toHaveCount\( 0 \)/ ); assert.match( visualStates, /image\.complete/ ); assert.match( visualStates, /image\.naturalWidth > 0/ ); assert.match( visualStates, /image\.naturalHeight > 0/ ); assert.match( visualStates, /typeof image\.decode === 'function'/ ); assert.match( visualStates, /awaitDecode: false/ ); assert.match( visualStates, /latestMedia/ ); } ); test( 'character stages define the shared mockup effect and responsive geometry', () => { const components = read( 'assets/css/components.css' ); const visualStates = read( 'tests/e2e/visual-states.spec.mjs' ); for ( const token of [ '--chrnx-character-accent', '--chrnx-character-halo', '--chrnx-character-wash', '--chrnx-character-shadow', ] ) { assert.match( components, new RegExp( token ), `${ token } must be shared` ); } assert.match( components, /\.chrnx-character-stage\s*\{[\s\S]*?grid-template-columns:\s*minmax\(0,\s*55fr\)\s+minmax\(0,\s*45fr\)/ ); assert.match( components, /\.chrnx-character-stage__atmosphere::before/ ); assert.match( components, /\.chrnx-character-stage__atmosphere::after/ ); assert.match( components, /\.chrnx-character-stage__art\s*>\s*\.chrnx-responsive-portrait/ ); assert.match( components, /@media \(max-width:\s*780px\)[\s\S]*?\.chrnx-character-stage\s*\{[\s\S]*?grid-template-columns:\s*1fr/ ); assert.match( components, /@media \(prefers-reduced-motion:\s*reduce\)[\s\S]*?\.chrnx-character-stage/ ); assert.match( components, /--chrnx-character-copy-surface/ ); assert.match( components, /\.chrnx-music-landing \.chrnx-character-stage__content\s*>\s*\.chrnx-responsive-portrait:has\(\.chrnx-character-stage__collage\)[\s\S]*?inset:\s*0/ ); for ( const timeout of [ '180_000', '120_000', '60_000' ] ) { assert.match( visualStates, new RegExp( `test\\.setTimeout\\( ${ timeout } \\)` ) ); } assert.match( visualStates, /character stage below-fold derivatives wait for approach/ ); } ); test( 'latest podcast owns a content-aware 720px card contract', () => { const podcast = read( 'blocks/latest-podcast/style.css' ); assert.match( podcast, /\.chrnx-podcast-card__link\s*\{[\s\S]*?display:\s*grid;[\s\S]*?grid-template-columns:\s*minmax\(12rem,\s*34%\)\s+minmax\(0,\s*1fr\)/, ); assert.match( podcast, /\.chrnx-podcast-card__content\s*\{[\s\S]*?min-width:\s*0;[\s\S]*?overflow-wrap:\s*anywhere;/, ); assert.match( podcast, /\.chrnx-podcast-card__image,[\s\S]*?\.chrnx-podcast-card__fallback\s*\{[\s\S]*?max-width:\s*100%;[\s\S]*?overflow:\s*hidden;/, ); assert.match( podcast, /\.chrnx-podcast-card__title,[\s\S]*?\.chrnx-podcast-card__excerpt,[\s\S]*?\.chrnx-podcast-card__badge\s*\{[\s\S]*?overflow-wrap:\s*anywhere;[\s\S]*?word-break:\s*normal;/, ); assert.match( podcast, /@media \(max-width:\s*720px\)[\s\S]*?\.chrnx-podcast-card__link\s*\{[\s\S]*?grid-template-columns:\s*minmax\(0,\s*1fr\)/, ); assert.match( podcast, /@media \(min-width:\s*721px\)[\s\S]*?\.chrnx-podcast-card__link\s*\{[\s\S]*?grid-template-columns:\s*minmax\(12rem,\s*34%\)\s+minmax\(0,\s*1fr\)/, ); } ); test( 'Music and Podcast stage specialization cannot leak into immutable surfaces', () => { const components = read( 'assets/css/components.css' ); const block = components.match( /\/\* Music and Podcast landing stage composition\. \*\/[\s\S]*?\/\* End Music and Podcast landing stage composition\. \*\//, )?.[ 0 ]; assert.ok( block, 'missing bounded Music and Podcast composition block' ); assert.match( block, /\.chrnx-music-landing \.chrnx-character-stage/ ); assert.match( block, /\.chrnx-podcast-landing \.chrnx-character-stage/ ); assert.match( block, /\.chrnx-character-stage__atmosphere/ ); assert.match( block, /object-fit:\s*cover/ ); assert.match( block, /\.chrnx-character-stage__content/ ); assert.match( block, /\.chrnx-character-stage__art/ ); for ( const forbidden of [ '.chrnx-home-projects', '.chrnx-about', '.chrnx-related-' ] ) { assert.doesNotMatch( block, new RegExp( forbidden.replaceAll( '.', '\\.' ) ) ); } } ); test( 'Music and Podcast browser tests derive their origin from Playwright configuration', () => { const stages = read( 'tests/e2e/music-podcast-stages.spec.mjs' ); assert.doesNotMatch( stages, /localhost:8908/ ); assert.match( stages, /new URL\( baseURL \)\.origin/ ); assert.match( stages, /new URL\( page\.url\(\) \)\.origin/ ); assert.match( stages, /expect\( resolvedOrigin \)\.toBe\( expectedOrigin \)/ ); } );