#!/usr/bin/env bash
# Etsy listing photo size: check four CC0 product photos (3872 to 4608 px on the long side) before and after the
# Koldflux target-size tool, against the Etsy Help Center figures read on 2026-11-30
# (shortest side 2000 px or more; images larger than 1MB may not finish uploading).
# Inputs (Wikimedia Commons, all CC0 1.0):
#   photos/candle-wax.jpg        File:Candle_Wax.jpg (SarKaLay)
#   photos/decorative-candle.jpg File:Decorative_candle_and_flowers.jpg (Sindugab), EXIF orientation 8
#   photos/duck-mug.jpg          File:Entenbecher.jpg (Tetzemann)
#   photos/toy-train.jpg         File:Wooden_toy_train_•_tracks_and_bridge.jpg (Thane Keller)
# The tool outputs come from tool-run.cjs (headless Chromium):
#   out/t950  custom target 950 KB (972,800 bytes)   out/t1mb  the 1 MB preset (1,048,576 bytes)
#   out/edge  a transparent PNG made in step 5
# Usage: bash test-run.sh <workdir>
set -euo pipefail
W="${1:?usage: test-run.sh <workdir>}"
cd "$W"
for f in photos/candle-wax.jpg photos/decorative-candle.jpg photos/duck-mug.jpg photos/toy-train.jpg; do
  [ -s "$f" ] || { echo "missing $f"; exit 1; }
done
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHECK="$HERE/etsy-photo-check.py"
echo "# etsy-listing-photo-size test run $(date -u +%FT%TZ)"
ffmpeg -version | head -1
python3 --version

echo "## 1. inputs (sha1)"
sha1sum photos/*.jpg

echo "## 2. control with a known answer: a 640x480 JPEG made by ffmpeg must read 640x480, orientation none (ffmpeg writes no EXIF)"
mkdir -p edge
ffmpeg -v error -y -f lavfi -i testsrc=s=640x480 -frames:v 1 edge/control-640x480.jpg
python3 "$CHECK" edge/control-640x480.jpg || true

echo "## 3. originals, then tool outputs (checker exits 1 when any file fails a check)"
python3 "$CHECK" photos/*.jpg || echo "checker exit=$?"
python3 "$CHECK" out/t950/*.jpg && echo "checker exit=0"
python3 "$CHECK" out/t1mb/*.jpg || echo "checker exit=$?"

echo "## 4. cross-check pixel size with ffprobe (stored frame, before EXIF rotation)"
for f in photos/*.jpg out/t950/*.jpg out/t1mb/*.jpg; do
  echo "$f $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$f") bytes=$(stat -c %s "$f")"
done

echo "## 5. transparency: 2400x2400 PNG, red square in the middle, the rest red at alpha 0"
ffmpeg -v error -y -f lavfi -i color=c=red:s=2400x2400 -frames:v 1 \
  -vf "format=rgba,geq=r=200:g=30:b=30:a='if(lt(abs(X-1200)\,600)*lt(abs(Y-1200)\,600)\,255\,0)'" edge/cutout-transparent.png
px() { ffmpeg -v error -i "$1" -vf "format=rgba,crop=1:1:$2:$3" -f rawvideo - | od -An -tu1 | tr -s ' '; }
for f in edge/cutout-transparent.png out/edge/cutout-transparent-950kb.jpg; do
  [ -s "$f" ] || { echo "$f missing (run tool-run.cjs on edge/cutout-transparent.png)"; continue; }
  echo "$f corner_rgba=$(px "$f" 10 10) centre_rgba=$(px "$f" 1200 1200)"
done

echo "## 6. checker edge inputs: missing file, empty file, PNG, @list with Windows line breaks and a non-ASCII name"
: > edge/empty.jpg
cp photos/toy-train.jpg "edge/train • été.jpg"
printf 'out/t950/toy-train-950kb.jpg\r\nedge/train \xe2\x80\xa2 \xc3\xa9t\xc3\xa9.jpg\r\n' > edge/list-crlf.txt
python3 "$CHECK" edge/nope.jpg edge/empty.jpg edge/cutout-transparent.png @edge/list-crlf.txt @edge/nolist.txt || echo "checker exit=$?"
echo "## 6b. a folder with camera-style names (IMG_0001.JPG, b.jpeg): the folder argument finds both, the pattern *.jpg finds neither"
mkdir -p edge/case; cp out/t950/toy-train-950kb.jpg edge/case/IMG_0001.JPG; cp out/t950/duck-mug-950kb.jpg edge/case/b.jpeg
python3 "$CHECK" edge/case && echo "checker exit=0"
(cd edge/case && python3 "$CHECK" *.jpg) || echo "checker exit=$?"

echo "## 7. colour profile: ICC description embedded in each 950 KB output (Etsy converts to sRGB itself and suggests sRGB if colours look wrong)"
for f in out/t950/*.jpg; do
  python3 - "$f" <<'PY'
import struct, sys
d = open(sys.argv[1], "rb").read()
i = d.find(b"ICC_PROFILE\x00")
if i < 0:
    print(sys.argv[1], "icc=none"); sys.exit()
p = d[i + 14:]
n = struct.unpack(">I", p[128:132])[0]
for k in range(n):
    sig, off, ln = struct.unpack(">4sII", p[132 + 12 * k:144 + 12 * k])
    if sig == b"desc":
        t = p[off:off + ln]
        txt = t[28:].decode("utf-16-be", "ignore") if t[:4] == b"mluc" else t[12:].split(b"\x00")[0].decode("latin-1")
        print(sys.argv[1], "icc_desc=" + txt.strip("\x00"))
PY
done
