test: harden CHRNX visual evidence

This commit is contained in:
chrnx committed 2026-08-13 10:38:29 -05:00
1 parent 4e496b241a
commit 16b6a75b35
3 files changed
+386 -62

No files matched your search

+156 -20
View File
@@ -1,11 +1,13 @@
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,
rmSync,
@@ -17,7 +19,13 @@ import {
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';
@@ -92,23 +100,18 @@ test( 'the visual layer is separately enqueued and covers responsive accessible
assert.match( components, /html\[data-theme="light"\]/ );
} );
test( 'grunge stage capture stays bounded to approved routes, widths, themes, and filenames', () => {
const capture = read( 'scripts/capture-visual-review.mjs' );
for ( const target of [ 'music', 'podcasts', 'podcasts-long-copy' ] ) {
assert.match( capture, new RegExp( `name: '${ target }'` ) );
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`,
);
}
assert.match( capture, /path: '\/music\/'/ );
assert.equal( capture.match( /path: '\/podcasts\/'/g )?.length, 2 );
for ( const width of [ 375, 768, 1440, 2560 ] ) {
assert.match( capture, new RegExp( `width: ${ width }` ) );
}
assert.match( capture, /const THEMES = \[ 'light', 'dark' \]/ );
assert.match(
capture,
/`grunge-stage-\$\{\s*target\.name\s*\}-\$\{\s*viewport\.width\s*\}-\$\{\s*theme\s*\}\.png`/,
);
assert.doesNotMatch( capture, /name: 'home'|name: 'post'|name: 'footer'|editorial-polish/ );
} );
test( 'grunge stage capture isolates every image and waits for stable rendered assets', () => {
@@ -134,10 +137,143 @@ test( 'grunge stage capture isolates every image and waits for stable rendered a
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 target[\s\S]*for \( const viewport[\s\S]*for \( const theme[\s\S]*await capture\( baseURL, target, viewport, theme \)/,
);
assert.match( capture, /for \( const capture of capturePlan \)[\s\S]*await captureImage/ );
} );
test( 'grunge stage publication is atomic across a failure and removes private staging', async () => {
const parent = mkdtempSync( join( tmpdir(), 'chrnx-review-parent-' ) );
const output = join( parent, 'grunge-stage-review.png' );
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;
};
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 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', () => {