#!/usr/bin/env bash
# Runs every published command on the test clip and on edge inputs; writes commands.log and test-results.txt.
# Usage (in a folder holding the published scripts, wcag22.html, understanding-1.2.5.html, talk.mp4, silent.mp4, reference_raw.txt and out/ from the tool runs): bash test-run.sh
set -uo pipefail
cd "${W:-$PWD}"; mkdir -p edge; LOG=commands.log; RES=test-results.txt; : > "$LOG"; : > "$RES"
python3 --version >> "$RES"; ffprobe -version | head -1 >> "$RES"
T=out/talk-accurate.txt
# edge inputs
: > edge/empty.txt; : > edge/empty.html; : > edge/empty.json
sed 's/$/\r/' $T > edge/crlf.txt
sed 's/Narrator/Zoë/; s/old man/vieux 北京/' $T > edge/nonascii.txt
python3 - <<'PY'
import json
q = json.load(open("wcag-quotes.json", encoding="utf-8"))
t = json.loads(json.dumps(q)); t["criteria"]["1.2.2"]["text"] = t["criteria"]["1.2.2"]["text"].replace("Captions are provided", "Captions may be provided")
json.dump(t, open("edge/tampered.json", "w", encoding="utf-8"))
t2 = json.loads(json.dumps(q)); t2["criteria"]["1.2.2"]["text"] = t2["criteria"]["1.2.2"]["text"].split(", except")[0] + "."
json.dump(t2, open("edge/no-exception.json", "w", encoding="utf-8"))
c = json.loads(json.dumps(q)); c["criteria"]["1.2.1"]["text"] += " Zoë 北京"
open("edge/nonascii.json", "w", encoding="utf-8", newline="\r\n").write(json.dumps(c, ensure_ascii=False, indent=1))
PY
run() { # run <published> <edge or test> <command...>
  local pub="$1" label="$2"; shift 2
  [ -s "$LOG" ] && echo >> "$LOG"; echo "published: $pub" >> "$LOG"; printf "ran:" >> "$LOG"; printf " %q" "$@" >> "$LOG"; echo >> "$LOG"
  "$@" > /tmp/cmd.out 2>&1; local c=$?; echo "exit=$c" >> "$LOG"
  { echo "[$label] $*"; head -${HEADN:-8} /tmp/cmd.out | cut -c1-240; echo "exit=$c"; } >> "$RES"
}
edgerun() { local edge="$1"; shift; echo "edge: $edge" >> "$LOG"; printf "ran:" >> "$LOG"; printf " %q" "$@" >> "$LOG"; echo >> "$LOG"; "$@" > /tmp/cmd.out 2>&1; local c=$?; echo "exit=$c" >> "$LOG"; { echo "[edge: $edge] $*"; head -${HEADN:-8} /tmp/cmd.out | cut -c1-240; echo "exit=$c"; } >> "$RES"; }

P0='python3 extract-wcag.py --selftest'
run "$P0" test python3 extract-wcag.py --selftest
edgerun "unexpected extra argument" python3 extract-wcag.py --selftest nope
PC1='curl -O https://koldflux.com/hub-media/captions-or-transcript-wcag-video/extract-wcag.py -O https://koldflux.com/hub-media/captions-or-transcript-wcag-video/matrix.py'
rm -rf edge/dl; mkdir -p edge/dl; U=file://$PWD
run "$PC1" test sh -c "cd edge/dl && curl -sS -O $U/extract-wcag.py -O $U/matrix.py && cmp extract-wcag.py ../../extract-wcag.py && cmp matrix.py ../../matrix.py && echo downloaded copies identical"
echo "note: the logged run fetches the same files from the local copy (file://) because the page is not live yet; the live URLs were not tested" >> "$LOG"
edgerun "file not found at that address" sh -c "cd edge/dl && curl -sS -O $U/nope.py"
edgerun "host does not exist" curl -sS -O https://nope.invalid/extract-wcag.py
P1='python3 extract-wcag.py wcag22.html understanding-1.2.5.html > wcag-quotes.json'
run "$P1" test sh -c 'python3 extract-wcag.py wcag22.html understanding-1.2.5.html > edge/quotes-rerun.json && cmp edge/quotes-rerun.json wcag-quotes.json && echo identical to the saved wcag-quotes.json'
echo "note: the logged run writes to edge/quotes-rerun.json and compares it with wcag-quotes.json, so the saved file is not overwritten; the command is otherwise the published one" >> "$LOG"
edgerun "missing file" python3 extract-wcag.py nope.html understanding-1.2.5.html
edgerun "empty file" python3 extract-wcag.py edge/empty.html understanding-1.2.5.html

P2='python3 matrix.py wcag-quotes.json'
HEADN=14 run "$P2" test python3 matrix.py wcag-quotes.json
edgerun "missing file" python3 matrix.py nope.json
edgerun "empty file" python3 matrix.py edge/empty.json
edgerun "quote changed on purpose (control: must fail)" python3 matrix.py edge/tampered.json
edgerun "1.2.2 exception clause removed on purpose (control: must fail)" python3 matrix.py edge/no-exception.json
edgerun "Windows line breaks and non-ASCII text in the quotes" python3 matrix.py edge/nonascii.json

P3='ffprobe -v error -show_entries stream=codec_type -of csv=p=0 your-video.mp4'
run "$P3" test ffprobe -v error -show_entries stream=codec_type -of csv=p=0 talk.mp4
python3 -c "import subprocess as s; d=float(s.check_output(['ffprobe','-v','error','-show_entries','format=duration','-of','csv=p=0','talk.mp4'])); print('talk.mp4 duration %.2f s = %d min %d s when rounded to the second' % (d, round(d)//60, round(d)%60))" >> "$RES"
edgerun "video with no audio track" ffprobe -v error -show_entries stream=codec_type -of csv=p=0 silent.mp4
edgerun "missing file" ffprobe -v error -show_entries stream=codec_type -of csv=p=0 nope.mp4

P4='python3 make-transcript.py your-file.txt "Title of your video" "Speaker name" transcript.txt'
cp $T talk-accurate.txt; rm -f transcript.txt
printf "[0:00] Hello there. How are you?\n[0:03] Fine, thanks.\n" > edge/ts.txt
printf "WEBVTT\n\n00:00:01.000 --> 00:00:02.000\nHello there.\n" > edge/sub.vtt
cp out/talk-accurate.srt edge/sub.srt
PC4='curl -O https://koldflux.com/hub-media/captions-or-transcript-wcag-video/make-transcript.py'
run "$PC4" test sh -c "cd edge/dl && curl -sS -O $U/make-transcript.py && cmp make-transcript.py ../../make-transcript.py && echo downloaded copy identical"
edgerun "file not found at that address" sh -c "cd edge/dl && curl -sS -O $U/nope.py"
edgerun "host does not exist" curl -sS -O https://nope.invalid/make-transcript.py
run "$P4" test python3 make-transcript.py talk-accurate.txt "A staircase and a cry" "Narrator" transcript.txt
echo "note: your-file.txt, the title and the speaker name were replaced by the test clip's values, as the page says" >> "$LOG"
{ echo "first lines of transcript.txt:"; head -c 330 transcript.txt; echo; } >> "$RES"
cp transcript.txt transcript-example.txt
echo "keep me" > edge/keep.txt
edgerun "missing file, existing output must stay untouched" python3 make-transcript.py nope.txt "Title" "Narrator" edge/keep.txt
echo "edge: after the missing-file run, edge/keep.txt holds: $(cat edge/keep.txt)" | tee -a "$LOG" >> "$RES"
edgerun "empty file" python3 make-transcript.py edge/empty.txt "Title" "Narrator" edge/out-empty.txt
echo "edge: after the empty-file run, edge/out-empty.txt exists: $([ -e edge/out-empty.txt ] && echo yes || echo no)" | tee -a "$LOG" >> "$RES"
edgerun "empty speaker name" python3 make-transcript.py talk-accurate.txt "Title" "" edge/out-nospeaker.txt
edgerun ".srt file (must be refused)" python3 make-transcript.py edge/sub.srt "Title" "Narrator" edge/out-srt.txt
edgerun ".vtt file (must be refused)" python3 make-transcript.py edge/sub.vtt "Title" "Narrator" edge/out-vtt.txt
edgerun ".txt with timestamps (must be refused)" python3 make-transcript.py edge/ts.txt "Title" "Narrator" edge/out-ts.txt
edgerun "Windows line breaks" python3 make-transcript.py edge/crlf.txt "Title" "Narrator" edge/out-crlf.txt
edgerun "non-ASCII speaker and text" python3 make-transcript.py edge/nonascii.txt "Titre é" "Zoë" edge/out-na.txt
edgerun "no output path (prints the transcript to the screen)" sh -c 'python3 make-transcript.py edge/ts.txt T S | head -2; python3 make-transcript.py talk-accurate.txt T S | head -c 80'

P5='python3 wer.py reference_raw.txt talk-accurate.txt'
run "$P5" test sh -c 'python3 wer.py reference_raw.txt out/talk-accurate.txt | head -1; python3 wer.py reference_raw.txt out/talk-fast.txt | head -1'
edgerun "missing file" python3 wer.py reference_raw.txt nope.txt
edgerun "empty file" python3 wer.py reference_raw.txt edge/empty.txt
edgerun "Windows line breaks" python3 wer.py reference_raw.txt edge/crlf.txt
edgerun "non-ASCII text" python3 wer.py reference_raw.txt edge/nonascii.txt

P6='node tool-run.cjs silent.mp4 out-prefix accurate'
export PATH=$HOME/.nvm/versions/node/v24.21.0/bin:$PATH
HEADN=3 run "$P6" test env WAIT_S=60 node tool-run.cjs silent.mp4 out/silent-rerun accurate
edgerun "missing file" env WAIT_S=20 node tool-run.cjs nope.mp4 out/nope accurate

P7='bash make-clips.sh episode.wav clips'
HEADN=4 run "$P7" test bash make-clips.sh episode.wav out/clips
edgerun "missing file" bash make-clips.sh nope.wav out/clips
edgerun "empty file" bash make-clips.sh edge/empty.txt out/clips

P8='python3 names-wrong.py reference_raw.txt talk-accurate.txt "d'"'"'avrigny,villefort,morrel,noirtier,abbe"'
HEADN=8 run "$P8" test python3 names-wrong.py reference_raw.txt out/talk-accurate.txt "d'avrigny,villefort,morrel,noirtier,abbe"
HEADN=8 run "$P8" test python3 names-wrong.py reference_raw.txt out/talk-fast.txt "d'avrigny,villefort,morrel,noirtier,abbe"
edgerun "missing file" python3 names-wrong.py reference_raw.txt nope.txt "morrel"
edgerun "empty file" python3 names-wrong.py reference_raw.txt edge/empty.txt "morrel"
edgerun "Windows line breaks" python3 names-wrong.py reference_raw.txt edge/crlf.txt "morrel,noirtier"
edgerun "non-ASCII text" python3 names-wrong.py reference_raw.txt edge/nonascii.txt "morrel"
