fix: harden CHRNX responsive image builds

This commit is contained in:
chrnx committed 2026-08-13 01:22:17 -05:00
1 parent 6443a46126
commit 06d2c88b81
2 files changed
+286 -17

No files matched your search

+128 -1
View File
@@ -1,5 +1,19 @@
import assert from 'node:assert/strict';
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import {
chmodSync,
existsSync,
linkSync,
lstatSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
statSync,
symlinkSync,
unlinkSync,
writeFileSync,
} from 'node:fs';
import { execFileSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
@@ -7,6 +21,7 @@ import { test } from 'node:test';
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' );
test( 'local visual fixtures define stable pages and representative editorial states', () => {
const pages = JSON.parse( read( 'tests/fixtures/pages.json' ) );
@@ -263,6 +278,118 @@ test( 'all approved responsive media rebuild byte-identically within payload bou
}
} );
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( '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)/ );