#!/usr/bin/env bash
# Rebuild the test listings and run etsy-listing-check.py on each; writes commands.log and test-results.txt.
# Run inside a scratch folder that holds inputs.json, make-cases.py and etsy-listing-check.py.
set -u
python3 make-cases.py >/dev/null
printf 'Caf\xe9 Cr\xe8me Hat\nwool hat\n' > latin1.txt
printf 'Wool Hat\nwarm cap-\nwomen'"'"'s hat'"'"'\n' > trailing.txt
printf '\nWool Hat\nwool beanie\n' > leadblank.txt
printf 'Wool Hat\n\xd1\x88\xd0\xb0\xd0\xbf\xd0\xba\xd0\xb0\nna\xc3\xafve hat\n' > cyrillic.txt
LOG=commands.log
{
  echo "# commands.log: etsy-title-character-limit, November 30, 2026 (server: $(lsb_release -ds 2>/dev/null), $(python3 -V), Node $(node -v 2>/dev/null))"
  echo "# Each published command as run, its exit code and the edge inputs it was also tried on. Scripts and test files sit next to this file."
  echo
} > "$LOG"
run() { # run <edge-or-published line> <file>
  echo "$1" >> "$LOG"
  echo "ran: python3 etsy-listing-check.py $2" >> "$LOG"
  python3 etsy-listing-check.py "$2" > out.txt 2>&1
  echo "exit=$?" >> "$LOG"
  sed 's/^/  | /' out.txt >> "$LOG"
}
run "published: python3 etsy-listing-check.py listing.txt" listing.txt
run "edge: a 139-character plain title with 11 clean tags" listing-clean.txt
run "edge: decomposed accents (NFD) and Windows line breaks; counted as typed (72, as in the Word counter), the joined count (70) printed as a note" listing-crlf.txt
run "edge: a 141-character title and 14 tags" listing-over.txt
run "edge: a Cyrillic tag (not on Etsy's list) next to a Latin accented one (on it)" cyrillic.txt
run "edge: a hyphen and an apostrophe at the end of a tag; Etsy only rules out the first character, so both pass" trailing.txt
run "edge: line 1 left blank, tags below it" leadblank.txt
run "edge: an empty file" empty.txt
run "edge: a missing file" missing.txt
run "edge: a file saved as Latin-1, not UTF-8" latin1.txt
rm -f out.txt
grep -E '^(published|edge|exit)' "$LOG" > test-results.txt
cat test-results.txt
