Files
chrnx-dev-wp/tests/smoke/visual-integration.test.mjs
T

244 lines
11 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync } 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 );
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 ) ) );
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( 'editorial review capture stays bounded to approved pages, variants, and filenames', () => {
const capture = read( 'scripts/capture-visual-review.mjs' );
for ( const target of [ 'home', 'music', 'post', 'footer' ] ) {
assert.match( capture, new RegExp( `name: '${ target }'` ) );
}
assert.match( capture, /path: '\/'/ );
assert.match( capture, /path: '\/music\/'/ );
assert.match( capture, /path: '\/una-cuerda-rota-tambien-puede-decidir-el-arreglo\/'/ );
assert.match( capture, /selector: 'footer\.wp-block-template-part'/ );
for ( const variant of [ 'desktop-dark', 'desktop-light', 'mobile-dark', 'mobile-light' ] ) {
assert.match( capture, new RegExp( `name: '${ variant }'` ) );
}
assert.match(
capture,
/`editorial-polish-\$\{\s*target\.name\s*\}-\$\{\s*variant\.name\s*\}\.png`/,
);
assert.doesNotMatch( capture, /name: 'topic-|name: 'podcasts'|name: 'about'/ );
} );
test( 'editorial review capture isolates browser compositing between theme variants', () => {
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*await browser\.close\(\);\s*\}/,
'Every isolated browser must close even when navigation or capture fails',
);
} );
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-home-music__collage-layer/ );
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 portraits 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, 40 );
for ( const name of [
'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',
] ) {
assert.ok( names.includes( name ), `missing ${ name }` );
}
let total = 0;
let collageTotal = 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;
if ( name.startsWith( 'the-no-names-collage-' ) ) {
collageTotal += generated.length;
}
}
}
assert.ok( collageTotal <= 400 * 1024, `collage derivatives exceed 400 KiB: ${ collageTotal }` );
assert.ok( total <= 2.1 * 1024 * 1024, `derivatives exceed 2.1 MiB: ${ total }` );
} finally {
rmSync( output, { recursive: true, force: true } );
rmSync( external, { recursive: true, force: true } );
rmSync( symlink, { 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( '/', '\\/' ) ) );
}
} );