import assert from 'node:assert/strict'; import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs'; import { execFileSync } from 'node:child_process'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { test } from 'node:test'; const read = ( path ) => readFileSync( new URL( `../../${ path }`, import.meta.url ), 'utf8' ); 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, 12 ); 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 ); for ( const topic of [ 'code', 'research', 'gaming', 'literature', 'music', 'podcasts', 'life' ] ) { 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( 'all approved portraits rebuild byte-identically within payload bounds', () => { const output = mkdtempSync( join( tmpdir(), 'chrnx-image-test-' ) ); const unsafeOutput = join( output, 'unsafe-output' ); try { assert.throws( () => execFileSync( './scripts/build-responsive-images.sh', [ unsafeOutput ], { cwd: new URL( '../..', import.meta.url ) } ) ); execFileSync( './scripts/build-responsive-images.sh', [ output ], { cwd: new URL( '../..', import.meta.url ) } ); const names = readdirSync( output ).sort(); assert.equal( names.length, 35 ); let total = 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 ) ) { assert.ok( statSync( join( output, name ) ).size <= 160 * 1024, `${ name } exceeds 160 KiB` ); const expectedWidth = Number( name.match( /-(384|768)\.(?:avif|webp)$/ )[ 1 ] ); 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; } } assert.ok( total <= 1.7 * 1024 * 1024, `derivatives exceed 1.7 MiB: ${ total }` ); } finally { rmSync( output, { 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( '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( '/', '\\/' ) ) ); } } );