#!/usr/bin/env bash
# Rebuild the test files and run alt-length-check.py on each; writes commands.log and test-results.txt.
# Run inside a scratch folder that holds make-cases.py and alt-length-check.py.
set -u
python3 make-cases.py >/dev/null
printf "Caf\xe9 cr\xe8me mug on a shelf.\n" > latin1.txt
: > empty.txt
printf "Speckled blue mug with a thumb rest and a sand-colored foot on a pine table beside a stack of rust and cream mugs under a hand-lettered seconds sale sign\n" > runon.txt
rm -f missing.txt
python3 - <<'EOF'
def opener(n):
    t = ("Blue speckled mug on a pine table near a window " * 5)[: n - 1]
    if t.endswith(" "):
        t = t[:-1] + "s"
    return t + "."
w = lambda name, text: open(name, "w", encoding="utf-8").write(text)
w("blanks.txt", "Blue speckled mug on a pine table.\n\n\nThree mugs in a row on a shelf.\n")
w("exact120.txt", opener(119) + " Detail.\n" + opener(120) + " Detail.\n")
w("tricky.txt",
  "Dr. Lee holds a speckled blue mug by the studio window while morning light falls across the glaze, the handle and the bare clay foot. Short.\n"
  "The sign reads \"Seconds sale.\" Behind it, three more mugs in rust, cream and teal stand on a wooden shelf under a window with morning light.\n"
  "青い斑点のマグカップ。Behind it, three more mugs in rust, cream and teal stand on a wooden shelf under a window with morning light.\n"
  "Steam rises from the mug… Behind it, three more mugs in rust, cream and teal stand on a wooden shelf under a window with light.\n")
w("dotted.txt",
  "The U.S. flag hangs above a long pottery table where students glaze blue speckled mugs and bowls during an evening wheel class. Short.\n"
  "Open 9 a.m. to 5 p.m. daily at the studio, where visitors can watch potters trim, glaze and fire mugs in a wood kiln out back. Short.\n"
  "Mugs, bowls, etc. Behind them, three more mugs in rust, cream and teal stand on a wooden shelf under a window with morning light.\n"
  "Order at koldflux.com. Behind the sign, three more mugs in rust, cream and teal stand on a wooden shelf under a window with light.\n"
  "Shop on Main Blvd. with a long row of speckled blue mugs and bowls glazed in rust, cream and teal on pine shelves under morning light.\n")
w("fancy.txt", "\U0001d401\U0001d425\U0001d42e\U0001d41e mug on a pine table.\n")
EOF
LOG=commands.log
{
  echo "# commands.log: alt-text-character-limit, December 1, 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. The scripts sit next to this file; test-run.sh rebuilds every test file."
  echo
} > "$LOG"
run() { # run <published-or-edge line> [file]
  echo "$1" >> "$LOG"
  shift
  echo "ran: python3 alt-length-check.py $*" >> "$LOG"
  python3 alt-length-check.py "$@" > out.txt 2>&1
  echo "exit=$?" >> "$LOG"
  sed "s/^/  | /" out.txt >> "$LOG"
}
run "published: python3 alt-length-check.py alt.txt" alt.txt
run "edge: the length controls (500/501, 1000/1001, 2000/2001 plain, and 999 code points with 5 flags = 1009 UTF-16)" alt-over.txt
run "edge: Windows line breaks (CRLF) and a decomposed accent (NFD)" alt-crlf.txt
run "edge: one run-on sentence over 120 characters (fits every limit, but the main point is not up front)" runon.txt
run "edge: blank lines between alt texts (rows show file line numbers 1 and 4)" blanks.txt
run "edge: first sentence of exactly 119 and 120 characters (120 is not under 120, so it is flagged)" exact120.txt
run "edge: openers with Dr., a period inside quotes, a CJK full stop and an ellipsis" tricky.txt
run "edge: openers with U.S., a.m./p.m., etc. and a web address (a word with a dot inside does not end the sentence, so openers that end in etc. or koldflux.com. run on; Blvd., not in the list, ends the sentence early and a long opener passes)" dotted.txt
run "edge: styled fancy-text bold letters (each is 2 UTF-16 units, 1 code point)" fancy.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
run "edge: no file given (prints the usage)"
rm -f out.txt
grep -E "^(published|edge|exit)" "$LOG" > test-results.txt
cat test-results.txt
