#!/usr/bin/env bash
# Rebuild the test drafts and run linkedin-post-check.py on them; writes commands.log and test-results.txt.
# Run inside a scratch folder that holds make-inputs.py, make-drafts.py and linkedin-post-check.py.
set -u
python3 make-inputs.py >/dev/null && python3 make-drafts.py
printf 'Caf\xe9 workshop in Montr\xe9al\n' > latin1.txt
: > empty.txt
printf '\n\n  \n' > blank.txt
rm -f missing.txt
cp draft-plain.txt my-post.txt
cp draft-comment.txt my-comment.txt
python3 -c "open(\"trail1.txt\",\"w\").write(\"a\"*3000+\"\\n\")"
python3 -c "open(\"trail2.txt\",\"w\").write(\"a\"*3000+\"\\n\\n\")"
LOG=commands.log
{
  echo "# commands.log: linkedin-post-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. Scripts and test files sit next to this file."
  echo
} > "$LOG"
run() { # run <published-or-edge line> <args...>
  echo "$1" >> "$LOG"; shift
  echo "ran: python3 linkedin-post-check.py $*" >> "$LOG"
  python3 linkedin-post-check.py "$@" > out.txt 2>&1
  echo "exit=$?" >> "$LOG"
  sed 's/^/  | /' out.txt >> "$LOG"
}
# one block per command printed on the page, each followed by its edge cases; blocks are separated by a blank line
run "published: python3 linkedin-post-check.py my-post.txt (reader command; my-post.txt here is a copy of the plain test draft)" my-post.txt
run "edge: an empty file" empty.txt
run "edge: a file of blank lines and spaces" blank.txt
run "edge: a missing file" missing.txt
run "edge: a file saved as Latin-1, not UTF-8" latin1.txt
run "edge: the letter a 3,000 times plus one final line break (ignored, as an editor adds it on save: fits, 0 left)" trail1.txt
run "edge: the letter a 3,000 times plus two final line breaks (the second counts: over by 1)" trail2.txt
echo >> "$LOG"
run "published: python3 linkedin-post-check.py draft-near-limit.txt (our test run, output printed on the page)" draft-near-limit.txt
run "edge: control, the letter a 3,000 times (known answer: fits, 0 left)" draft-control-3000.txt
run "edge: control, the letter a 3,001 times (known answer: over by 1)" draft-control-3001.txt
run "edge: the list post with blank lines (LF)" draft-line-breaks.txt
run "edge: the same list post saved with Windows CRLF line breaks; counted as LF, as a browser text box does" draft-crlf.txt
run "edge: emoji bullets (check mark 1 unit, pointing hand and rocket 2)" draft-emoji-bullets.txt
run "edge: skin-tone wave, a flag and a joined (ZWJ) emoji" draft-skin-flag-zwj.txt
run "edge: Unicode bold letters in the hook" draft-bold-unicode.txt
run "edge: a long link with tracking parameters" draft-long-url.txt
echo "edge: output to a console or file that is not UTF-8 (cp1252, as on Windows); the emoji prints as ?" >> "$LOG"
echo "ran: PYTHONIOENCODING=cp1252 python3 linkedin-post-check.py draft-near-limit.txt > out.txt" >> "$LOG"
PYTHONIOENCODING=cp1252 python3 linkedin-post-check.py draft-near-limit.txt > out.txt 2>&1
echo "exit=$?" >> "$LOG"
sed "s/^/  | /" out.txt >> "$LOG"
echo >> "$LOG"
run "published: python3 linkedin-post-check.py my-comment.txt --limit 1250 (reader command; my-comment.txt here is a copy of the comment test draft)" my-comment.txt --limit 1250
run "edge: the comment test draft itself, --limit 1250" draft-comment.txt --limit 1250
run "edge: --limit=1250 written with an equals sign (same as --limit 1250)" draft-comment.txt --limit=1250
run "edge: --limit with no number" draft-comment.txt --limit
run "edge: --limit 0 (rejected, a limit must be above 0)" draft-comment.txt --limit 0
run "edge: --limit -5 (rejected)" draft-comment.txt --limit -5
rm -f out.txt
grep -E '^(published|edge|exit)' "$LOG" > test-results.txt
cat test-results.txt
