From b0e44423a12e3403cb9237b4d364d6a039338e8f Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 13 Aug 2026 11:59:11 -0500 Subject: [PATCH] test: verify CHRNX publication rollback --- scripts/capture-visual-review.mjs | 30 +++- tests/smoke/visual-integration.test.mjs | 199 ++++++++++++++++++++++-- 2 files changed, 210 insertions(+), 19 deletions(-) diff --git a/scripts/capture-visual-review.mjs b/scripts/capture-visual-review.mjs index e7f681d..6b8af71 100644 --- a/scripts/capture-visual-review.mjs +++ b/scripts/capture-visual-review.mjs @@ -183,6 +183,9 @@ export async function publishReviewEvidence( { allowedParentDirectory, capturePlan, captureImage, + renameEntry = rename, + removeEntry = rm, + reportCleanupFailure = ( message ) => process.stderr.write( `${ message }\n` ), } ) { const { outputPath, parentPath, outputExists } = await assertSafeOutput( outputDirectory, @@ -203,29 +206,42 @@ export async function publishReviewEvidence( { if ( await optionalLstat( backupPath ) ) { throw new Error( 'A previous review backup must be resolved before publication.' ); } - await rename( outputPath, backupPath ); + await renameEntry( outputPath, backupPath ); backupCreated = true; } try { - await rename( stagingPath, outputPath ); + await renameEntry( stagingPath, outputPath ); published = true; } catch ( error ) { if ( backupCreated ) { - await rename( backupPath, outputPath ); + await renameEntry( backupPath, outputPath ); backupCreated = false; } throw error; } if ( backupCreated ) { - await rm( backupPath, { recursive: true } ); - backupCreated = false; + let cleanupError; + for ( let attempt = 0; attempt < 2; attempt++ ) { + try { + await removeEntry( backupPath, { recursive: true } ); + backupCreated = false; + break; + } catch ( error ) { + cleanupError = error; + } + } + if ( backupCreated ) { + const detail = + cleanupError instanceof Error ? cleanupError.message : String( cleanupError ); + reportCleanupFailure( `Published review backup remains at ${ backupPath}: ${ detail }` ); + } } } finally { if ( ! published ) { await rm( stagingPath, { recursive: true, force: true } ); } - if ( backupCreated && ! await optionalLstat( outputPath ) ) { - await rename( backupPath, outputPath ); + if ( backupCreated && ! ( await optionalLstat( outputPath ) ) ) { + await renameEntry( backupPath, outputPath ); } } } diff --git a/tests/smoke/visual-integration.test.mjs b/tests/smoke/visual-integration.test.mjs index 7d91ed5..df3c296 100644 --- a/tests/smoke/visual-integration.test.mjs +++ b/tests/smoke/visual-integration.test.mjs @@ -10,6 +10,8 @@ import { mkdirSync, readFileSync, readdirSync, + realpathSync, + renameSync, rmSync, statSync, symlinkSync, @@ -30,6 +32,17 @@ import { 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' ) ); @@ -114,6 +127,35 @@ test( 'grunge stage capture exposes exactly 24 approved routes, widths, themes, } } ); +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, /52 generated files: 42 binary derivatives and 10 manifests/ ); + assert.match( readme, /nine portrait families at 384 px and 768 px/ ); + assert.match( readme, /Podcast\s+background family at 384 px, 768 px, and 1536 px/ ); + for ( const budget of [ '160 KiB', '260 KiB', '400 KiB', '340 KiB', '620 KiB', '2.75 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' ); @@ -140,20 +182,9 @@ test( 'grunge stage capture isolates every image and waits for stable rendered a 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 () => { +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' ); - 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, @@ -188,6 +219,77 @@ test( 'grunge stage publication is atomic across a failure and removes private s } } ); +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-' ) ); @@ -551,6 +653,79 @@ printf 'partial-build-%s\n' "$count" > "$output" } } ); +test( 'responsive promotion failure restores all 52 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, 52 ); + 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)/ );