#!/usr/bin/env bash
# Koldflux test run for "Make a favicon from your logo" (2026-11-26).
# Usage: bash test-run.sh <workdir> <font-dir>
#   <font-dir> holds DejaVuSans.ttf and DejaVuSans-Bold.ttf (open licence). The scripts sit next to this file.
# Needs: python3 with Pillow, node with the playwright package (set NODE_PATH to its node_modules), ffmpeg.
# Writes <workdir>/test-results.txt. Each step prints its own line; a failing step prints exit=<code> and the run goes on.
set -u
S="$(cd "$(dirname "$0")" && pwd)"; W="$1"; F="$2"
mkdir -p "$W" && cd "$W" || exit 2
R="$W/test-results.txt"; : > "$R"
say() { echo "$*" | tee -a "$R"; }
step() { say ""; say "== $1"; }
say "# test-results.txt: live Koldflux favicon generator (https://koldflux.com/tools/image/favicon-generator) and remove-background tool, run $(date +%F)"
say "# tools: $(ffmpeg -version | head -1 | cut -d' ' -f1-3), node $(node --version), Pillow $(python3 -c 'import PIL;print(PIL.__version__)'), Playwright headless Chromium"

step "1. five invented logos (Tidewell), each saved as JPG on white"
python3 "$S/make-logos.py" "$F" src 2>&1 | tee -a "$R"; echo "exit=$?" | tee -a "$R"
ls -l src | tail -n +2 | awk '{print $5, $9}' | tee -a "$R"

step "2. controls for ico-check.py (control-quarter.ico must report 64 of 256 at 16 px; the other three must report ERROR)"
python3 "$S/make-ico-control.py" ctl | tee -a "$R"
python3 "$S/ico-check.py" ctl/control-quarter.ico ctl/empty.ico ctl/notico.ico ctl/missing.ico 2>&1 | tee -a "$R"

step "3. favicon generator on each logo, square option crop and fit (only the option changes; the file is the same)"
node "$S/favicon-run.cjs" fav src/*.jpg 2>&1 | cut -c1-230 | tee -a "$R"
say "-- 16 px frame of favicon.ico:"
python3 "$S/ico-check.py" fav/*/favicon.ico 2>&1 | sed 's#/favicon.ico##' | tee -a "$R"

step "4. icon only: crop the symbol out of the logo with ffmpeg, then run the generator on the JPG and on the remove-background PNG"
mkdir -p ic
ffmpeg -v error -y -i src/logo-wide-lockup.jpg -vf "crop=300:300:50:50" -frames:v 1 ic/icon-only-wide.png; say "ffmpeg crop wide lockup exit=$?"
ffmpeg -v error -y -i src/logo-flat-badge.jpg -vf "crop=300:300:250:70" -frames:v 1 ic/icon-only-badge.png; say "ffmpeg crop flat badge exit=$?"
node "$S/favicon-run.cjs" fav-icon-jpg ic/*.png 2>&1 | cut -c1-120 | tee -a "$R"
node "$S/tool-run.cjs" ic-out ic/*.png 2>&1 | tee -a "$R"
node "$S/favicon-run.cjs" fav-icon-alpha ic-out/*.png 2>&1 | cut -c1-120 | tee -a "$R"
say "-- 16 px frame, icon cropped, then remove-background (alpha) and without it (opaque):"
python3 "$S/ico-check.py" fav-icon-jpg/*/favicon.ico fav-icon-alpha/*/favicon.ico 2>&1 | sed 's#/favicon.ico##' | tee -a "$R"

step "5. other files in the download (flat badge, crop): sizes and zip contents"
python3 "$S/check-sizes.py" fav/logo-flat-badge-crop/apple-touch-icon.png fav/logo-flat-badge-crop/icon-192.png fav/logo-flat-badge-crop/icon-512.png fav/logo-flat-badge-crop/favicons.zip fav/logo-flat-badge-crop/missing.png 2>&1 | tee -a "$R"

step "6. decode in Chromium: unzipped favicons next to a page with the tool's link lines; the second site links one file that does not exist (control, must say FAILED)"
rm -rf site badsite && mkdir -p site badsite
python3 -m zipfile -e fav/logo-flat-badge-crop/favicons.zip site
cat > site/index.html <<'H'
<!doctype html><html><head><meta charset="utf-8"><title>Tidewell</title>
<link rel="icon" href="/favicon.ico" sizes="48x48">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head><body>Tidewell</body></html>
H
cp site/favicon.ico badsite/; cat > badsite/index.html <<'H'
<!doctype html><html><head><meta charset="utf-8"><title>Tidewell</title>
<link rel="icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/not-there.png">
</head><body>Tidewell</body></html>
H
say "-- site:"; node "$S/favicon-browser.cjs" site 2>&1 | tee -a "$R"
say "-- badsite (control):"; node "$S/favicon-browser.cjs" badsite 2>&1 | tee -a "$R"
node "$S/favicon-browser.cjs" nodir 2>&1 | tee -a "$R"; say "favicon-browser nodir exit=${PIPESTATUS[0]}"

step "7. 16 px frame on a dark and a light tab, enlarged 12x"
python3 "$S/tab-sim.py" tabs.png fav/logo-flat-badge-crop/favicon.ico fav/logo-flat-badge-fit/favicon.ico fav/logo-wide-lockup-crop/favicon.ico fav/logo-wide-lockup-fit/favicon.ico fav-icon-jpg/icon-only-wide-crop/favicon.ico fav-icon-alpha/icon-only-wide-crop/favicon.ico 2>&1 | tee -a "$R"; say "tab-sim exit=${PIPESTATUS[0]}"
say ""; say "# done $(date +%T)"
