diff --git a/scripts/build-responsive-images.sh b/scripts/build-responsive-images.sh index dd83692..c348076 100755 --- a/scripts/build-responsive-images.sh +++ b/scripts/build-responsive-images.sh @@ -1,7 +1,7 @@ #!/bin/sh set -eu -chrnx_image_script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +chrnx_image_script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) chrnx_image_root=$(dirname -- "$chrnx_image_script_dir") chrnx_image_default_output="$chrnx_image_root/assets/images/generated" chrnx_image_output=${1:-"$chrnx_image_default_output"} @@ -25,10 +25,10 @@ if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then echo "Temporary image output does not exist: $chrnx_image_output" >&2 exit 2 fi - chrnx_image_safe_tmp=$(CDPATH= cd -P -- "${TMPDIR:-/tmp}" && pwd) + chrnx_image_safe_tmp=$(CDPATH='' cd -P -- "${TMPDIR:-/tmp}" && pwd) chrnx_image_output_parent=$(dirname -- "$chrnx_image_output") chrnx_image_output_name=$(basename -- "$chrnx_image_output") - chrnx_image_output_parent=$(CDPATH= cd -P -- "$chrnx_image_output_parent" && pwd) + chrnx_image_output_parent=$(CDPATH='' cd -P -- "$chrnx_image_output_parent" && pwd) case "$chrnx_image_output_name" in chrnx-*) ;; *) @@ -40,11 +40,8 @@ if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then echo "Temporary image output must be a direct child of $chrnx_image_safe_tmp." >&2 exit 2 fi - chrnx_image_output=$(CDPATH= cd -P -- "$chrnx_image_output" && pwd) + chrnx_image_output=$(CDPATH='' cd -P -- "$chrnx_image_output" && pwd) fi -chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX") -trap 'rm -rf -- "$chrnx_image_tmp"' EXIT HUP INT TERM - for chrnx_image_tool in sips cwebp avifenc; do if ! command -v "$chrnx_image_tool" >/dev/null 2>&1; then echo "Required image tool is missing: $chrnx_image_tool" >&2 @@ -53,6 +50,88 @@ for chrnx_image_tool in sips cwebp avifenc; do done mkdir -p -- "$chrnx_image_output" +chrnx_image_stage=$(mktemp -d "$chrnx_image_output/.chrnx-stage.XXXXXX") +trap 'rm -rf -- "$chrnx_image_stage"' EXIT +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM +chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX") +trap 'rm -rf -- "$chrnx_image_tmp" "$chrnx_image_stage"' EXIT +chrnx_image_build_output=$chrnx_image_stage +chrnx_image_backup="$chrnx_image_stage/.backup" +chrnx_image_discard="$chrnx_image_stage/.discard" +chrnx_image_promoted_log="$chrnx_image_stage/.promoted" +chrnx_image_promotion_active=0 + +chrnx_rollback_responsive_images() { + chrnx_image_rollback_failed=0 + mkdir -p -- "$chrnx_image_discard" + if [ -f "$chrnx_image_promoted_log" ]; then + while IFS= read -r chrnx_image_name; do + chrnx_image_staged="$chrnx_image_stage/$chrnx_image_name" + chrnx_image_published="$chrnx_image_output/$chrnx_image_name" + if [ ! -e "$chrnx_image_staged" ] && [ ! -L "$chrnx_image_staged" ] && { [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; }; then + if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then + echo "Responsive image rollback could not remove: $chrnx_image_published" >&2 + chrnx_image_rollback_failed=1 + fi + fi + done < "$chrnx_image_promoted_log" + fi + if [ -d "$chrnx_image_backup" ]; then + for chrnx_image_backed_up in "$chrnx_image_backup"/*; do + if [ ! -e "$chrnx_image_backed_up" ] && [ ! -L "$chrnx_image_backed_up" ]; then + continue + fi + chrnx_image_name=$(basename -- "$chrnx_image_backed_up") + chrnx_image_published="$chrnx_image_output/$chrnx_image_name" + if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then + if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then + echo "Responsive image rollback could not clear: $chrnx_image_published" >&2 + chrnx_image_rollback_failed=1 + continue + fi + fi + if ! mv -f -- "$chrnx_image_backed_up" "$chrnx_image_published"; then + echo "Responsive image rollback could not restore: $chrnx_image_published" >&2 + chrnx_image_rollback_failed=1 + fi + done + fi + return "$chrnx_image_rollback_failed" +} + +chrnx_cleanup_responsive_images() { + chrnx_image_status=$? + chrnx_image_rollback_failed=0 + trap - EXIT HUP INT TERM + set +e + if [ "$chrnx_image_promotion_active" -eq 1 ]; then + if ! chrnx_rollback_responsive_images; then + chrnx_image_rollback_failed=1 + if [ "$chrnx_image_status" -eq 0 ]; then + chrnx_image_status=1 + fi + fi + fi + if ! rm -rf -- "$chrnx_image_tmp"; then + echo "Responsive image temporary cleanup failed: $chrnx_image_tmp" >&2 + chrnx_image_status=1 + fi + if [ "$chrnx_image_rollback_failed" -eq 0 ]; then + if ! rm -rf -- "$chrnx_image_stage"; then + echo "Responsive image staging cleanup failed: $chrnx_image_stage" >&2 + chrnx_image_status=1 + fi + else + echo "Responsive image rollback backup preserved at: $chrnx_image_stage" >&2 + fi + exit "$chrnx_image_status" +} +trap 'chrnx_cleanup_responsive_images' EXIT +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM chrnx_build_responsive_image() { chrnx_image_basename=$1 @@ -68,8 +147,8 @@ chrnx_build_responsive_image() { for chrnx_image_width in 384 768; do chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_source_extension" sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null - cwebp -quiet -preset picture -q "$chrnx_image_webp_quality" -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.webp" - avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 58 --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null + cwebp -quiet -preset picture -q "$chrnx_image_webp_quality" -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp" + avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 58 --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null done } @@ -85,24 +164,87 @@ chrnx_build_responsive_background() { for chrnx_image_width in 384 768 1536; do chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.png" sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null - cwebp -quiet -preset picture -q 62 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.webp" - avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 52 --speed 6 "$chrnx_image_resized" "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null + cwebp -quiet -preset picture -q 62 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp" + avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 52 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null done } for chrnx_image_topic in code gaming life literature podcasts research; do chrnx_build_responsive_image "alter-ego-$chrnx_image_topic" "alter-ego-$chrnx_image_topic.png" - printf '{\n\t"source": "alter-ego-%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_topic" > "$chrnx_image_output/alter-ego-$chrnx_image_topic.json" + printf '{\n\t"source": "alter-ego-%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_topic" > "$chrnx_image_build_output/alter-ego-$chrnx_image_topic.json" done chrnx_build_responsive_image "alter-ego-gaming-half-body" "alter-ego-gaming-half-body.png" -printf '{\n\t"source": "alter-ego-gaming-half-body.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/alter-ego-gaming-half-body.json" +printf '{\n\t"source": "alter-ego-gaming-half-body.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/alter-ego-gaming-half-body.json" chrnx_build_responsive_image "the-no-names-musician-cutout" "the-no-names-musician-cutout.png" -printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/the-no-names-musician-cutout.json" +printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-musician-cutout.json" chrnx_build_responsive_image "the-no-names-collage" "the-no-names-collage.jpeg" 58 -printf '{\n\t"source": "the-no-names-collage.jpeg",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/the-no-names-collage.json" +printf '{\n\t"source": "the-no-names-collage.jpeg",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-collage.json" chrnx_build_responsive_background "podcast-frequency-grunge" "podcast-frequency-grunge.png" -printf '{\n\t"source": "podcast-frequency-grunge.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' > "$chrnx_image_output/podcast-frequency-grunge.json" +printf '{\n\t"source": "podcast-frequency-grunge.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' > "$chrnx_image_build_output/podcast-frequency-grunge.json" + +chrnx_validate_staged_family() { + chrnx_image_basename=$1 + shift + for chrnx_image_width in "$@"; do + for chrnx_image_extension in avif webp; do + chrnx_image_staged="$chrnx_image_stage/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_extension" + if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ] || [ ! -s "$chrnx_image_staged" ]; then + echo "Responsive image staging is incomplete: $chrnx_image_staged" >&2 + exit 1 + fi + chrnx_image_actual_width=$(sips -g pixelWidth "$chrnx_image_staged" 2>/dev/null | awk '/pixelWidth:/ { print $2 }') + if [ "$chrnx_image_actual_width" != "$chrnx_image_width" ]; then + echo "Responsive image staging width mismatch: $chrnx_image_staged" >&2 + exit 1 + fi + done + done + chrnx_image_manifest="$chrnx_image_stage/$chrnx_image_basename.json" + if [ ! -f "$chrnx_image_manifest" ] || [ -L "$chrnx_image_manifest" ] || [ ! -s "$chrnx_image_manifest" ]; then + echo "Responsive image staging is incomplete: $chrnx_image_manifest" >&2 + exit 1 + fi +} + +for chrnx_image_topic in code gaming life literature podcasts research; do + chrnx_validate_staged_family "alter-ego-$chrnx_image_topic" 384 768 +done +chrnx_validate_staged_family "alter-ego-gaming-half-body" 384 768 +chrnx_validate_staged_family "the-no-names-musician-cutout" 384 768 +chrnx_validate_staged_family "the-no-names-collage" 384 768 +chrnx_validate_staged_family "podcast-frequency-grunge" 384 768 1536 + +set -- "$chrnx_image_stage"/* +if [ "$#" -ne 52 ]; then + echo "Responsive image staging expected 52 files, got $#." >&2 + exit 1 +fi +for chrnx_image_staged in "$@"; do + if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ]; then + echo "Responsive image staging contains an unsafe entry: $chrnx_image_staged" >&2 + exit 1 + fi + chrnx_image_name=$(basename -- "$chrnx_image_staged") + chrnx_image_published="$chrnx_image_output/$chrnx_image_name" + if [ -d "$chrnx_image_published" ]; then + echo "Refusing directory-shaped responsive image output: $chrnx_image_published" >&2 + exit 1 + fi +done + +mkdir -p -- "$chrnx_image_backup" +chrnx_image_promotion_active=1 +for chrnx_image_staged in "$@"; do + chrnx_image_name=$(basename -- "$chrnx_image_staged") + chrnx_image_published="$chrnx_image_output/$chrnx_image_name" + if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then + mv -f -- "$chrnx_image_published" "$chrnx_image_backup/$chrnx_image_name" + fi + printf '%s\n' "$chrnx_image_name" >> "$chrnx_image_promoted_log" + mv -f -- "$chrnx_image_staged" "$chrnx_image_published" +done +chrnx_image_promotion_active=0 diff --git a/tests/smoke/visual-integration.test.mjs b/tests/smoke/visual-integration.test.mjs index 22c3394..f819695 100644 --- a/tests/smoke/visual-integration.test.mjs +++ b/tests/smoke/visual-integration.test.mjs @@ -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)/ );