#!/bin/bash
# Runs every published command on the test file and on an edge file and writes commands.log.
cd "$(dirname "$0")"
python3 mkcaptions.py
{
run() { # $1 published text, $2 command as run, $3 edge note
  echo "published: $1"; echo "run: $2"; out=$(eval "$2" 2>&1); code=$?; echo "$out" | sed 's/^/  /'; echo "exit=$code"; [ -n "$3" ] && echo "edge: $3"; echo; }
run "node caption-check.mjs caption.txt" "node caption-check.mjs caption.txt" ""
run "node caption-check.mjs caption.txt" "node caption-check.mjs caption-edge.txt" "caption-edge.txt has CRLF line breaks, a family emoji, a decomposed e + accent, 7 hashtags, 2 mentions and 2,350 UTF-16 units, so every limit flag is OVER"
run "node caption-check.mjs caption.txt 80" "node caption-check.mjs caption.txt 80" "optional second argument shortens the preview to 80 graphemes"
run "node caption-check.mjs caption.txt" "node caption-check.mjs missing.txt" "file that does not exist: prints a message and exits 2"
echo "cross-check of caption-edge.txt in Python (independent count):"
python3 - <<'PY'
t = open("caption-edge.txt", encoding="utf-8", newline="").read().replace("\r\n", "\n")
print("  utf16 units", len(t.encode("utf-16-le")) // 2, "code points", len(t), "utf8 bytes", len(t.encode("utf-8")))
PY
} > commands.log 2>&1
