#!/usr/bin/env bash
# Acceptance checks 1 to 11 of CARD.md, run on histories written by real shells driven in tmux.
# Needs Linux with tmux, zsh, strace, script and uv. Run from anywhere: ./e2e.sh
# Writes out/e2e.log, the pages it read in out/*.txt, and every captured history in out/capture/.
set -u
cd "$(dirname "$0")"
mkdir -p out/capture
LOG=out/e2e.log
: > "$LOG"
T=(tmux -L corrigenda -f /dev/null)
WORK=$(mktemp -d)  # the shells run here; git finds no repository in it and changes nothing
PY=$(uv run python -c 'import sys; print(sys.executable)')
failed=0

say() { printf '%s\n' "$*" | tee -a "$LOG"; }
check() { if eval "$2"; then say "PASS $1"; else say "FAIL $1"; failed=$((failed + 1)); fi; }
joined() { tr '\n' ' ' < "$1"; }
cor() { uv run corrigenda.py "$@"; }

shell() {  # shell NAME HISTFILE bash|zsh [VAR=value ...]: a real interactive shell in a tmux pane
  local name=$1 file=$2 kind=$3
  shift 3
  local cmd=(bash --norc --noprofile -i)
  [ "$kind" = zsh ] && cmd=(zsh -f -i)
  "${T[@]}" new-session -d -s "$name" -x 100 -y 50 -c "$WORK" \
    env -i HOME="$HOME" PATH="$PATH" TERM=xterm-256color LANG=C.UTF-8 PS1='$ ' HISTFILE="$file" "$@" "${cmd[@]}"
  sleep 1
}
key() {  # key NAME TEXT [SECONDS]: type one line and press Enter
  "${T[@]}" send-keys -t "$1" -l "$2"
  "${T[@]}" send-keys -t "$1" Enter
  sleep "${3:-0.6}"
}
leave() {  # leave NAME: type exit and wait until the shell has written its history and gone
  key "$1" exit 0.2
  while "${T[@]}" has-session -t "$1" 2>/dev/null; do sleep 0.2; done
}

say "== corrigenda e2e, $(date -u +%FT%TZ), $(uname -sm), $("$PY" --version), $(tmux -V), zsh $(zsh --version | cut -d' ' -f2)"

# Captures. The timestamped bash session waits 301 s, so it runs beside the others.
timestamped() {
  local f
  f=$(mktemp)
  shell ts "$f" bash 'HISTTIMEFORMAT=%s '
  key ts 'gti status' 5
  key ts 'git status'
  key ts 'ls'
  key ts 'gti status' 5
  key ts 'git status'
  key ts 'pwd'
  key ts 'gti status' 301
  key ts 'git status'
  leave ts
  cp "$f" out/capture/bash-timestamped.hist
  cor "$f" > out/c5-timestamped.txt
}
timestamped &
ts_job=$!

# Check 2's session, then check 11's second session on the same file.
f2=$(mktemp)
shell plain "$f2" bash
for between in ls pwd ''; do
  key plain 'gti status'
  key plain 'git status'
  [ -n "$between" ] && key plain "$between"
done
leave plain
cp "$f2" out/capture/bash-plain.hist
cor "$f2" > out/c2-page.txt
cor --aliases "$f2" > out/c2-aliases.txt
shell again "$f2" bash
key again "alias gti='git'"
for i in 1 2 3 4; do key again 'gti status'; done
leave again
cp "$f2" out/capture/bash-plain-retired.hist
cor "$f2" > out/c11-page.txt
echo $? > out/c11.rc

# zsh with EXTENDED_HISTORY, including a multi-line entry.
fz=$(mktemp)
shell z "$fz" zsh
key z 'setopt EXTENDED_HISTORY RCS; SAVEHIST=1000'  # zsh -f also turns off RCS, and with it the save at exit
key z 'gti status' 2
key z 'git status'
key z 'for i in 1 2; do'
key z 'echo $i'
key z 'done'
key z 'echo 2×3'  # × is c3 97 in UTF-8; zsh metafies 0x97 to 83 b7, so the file is no longer UTF-8
key z 'gti status'
key z 'git status'
key z 'sl'
key z 'ls'
key z 'uname'
key z 'sl'
key z 'ls'
leave z
cp "$fz" out/capture/zsh-extended.hist
SHELL=/bin/bash cor out/capture/zsh-extended.hist > out/c5-zsh.txt

# A bash history that gained times halfway: a plain session, then one with HISTTIMEFORMAT.
fm=$(mktemp)
shell m1 "$fm" bash
key m1 'gti status'
key m1 'git status'
key m1 'gti status'
key m1 'git status'
leave m1
shell m2 "$fm" bash 'HISTTIMEFORMAT=%s '
key m2 'gti log'
key m2 'git log'
leave m2
cp "$fm" out/capture/bash-mixed.hist
cor "$fm" > out/c5-mixed.txt

# Check 4: a secret in the arguments.
fs=$(mktemp)
shell s "$fs" bash
for i in 1 2 3; do
  key s 'gti push https://user:SECRET123@example.invalid/x.git'
  key s 'git push https://user:SECRET123@example.invalid/x.git'
done
leave s
cp "$fs" out/capture/bash-secret.hist
cor "$fs" > out/c4-page.txt
cor --aliases "$fs" > out/c4-aliases.txt

# An alias in daily use, and a slip with two corrections.
fa=$(mktemp)
shell a "$fa" bash
key a "alias ll='ls -l'"
for i in $(seq 38); do key a ll 0.15; done
for i in 1 2; do key a ll 0.3; key a ls 0.3; done
for i in 1 2 3; do
  key a 'gerp -c bash /etc/shells'
  key a 'grep -c bash /etc/shells'
done
key a 'gerp -c bash /etc/shells'
key a 'egrep -c bash /etc/shells'
leave a
cp "$fa" out/capture/bash-alias.hist
cor "$fa" > out/c1-alias.txt

wait "$ts_job"

say "== 1. unit tests"
cp out/capture/*.hist fixtures/  # the fixtures are always the histories these shells just wrote
say "fixtures: $(cd fixtures && ls | tr '\n' ' ')"
check "1 unittest passes" 'uv run python -m unittest -v > out/unittest.log 2>&1'
grep -E '^(Ran|OK|FAILED)' out/unittest.log | tee -a "$LOG"
# The tests must fail when a rule breaks: each line breaks one rule in a copy and runs them again.
M=$(mktemp -d)
while IFS='|' read -r label expr; do
  rm -rf "${M:?}"/*
  cp -r corrigenda.py test_corrigenda.py fixtures "$M"/
  sed -i "$expr" "$M/corrigenda.py"
  if cmp -s "$M/corrigenda.py" corrigenda.py; then say "FAIL 1 the mutation did not apply: $label"; failed=$((failed + 1)); continue; fi
  verdict=$(cd "$M" && PYTHONDONTWRITEBYTECODE=1 "$PY" -m unittest 2>&1 >/dev/null | grep -E '^(OK|FAILED)')
  check "1 tests catch it: $label => $verdict" '[ "${verdict%% *}" = FAILED ]'
done <<'LIST'
a swap costs 2 instead of 1|s/d\[i - 2\]\[j - 2\] + 1)/d[i - 2][j - 2] + 2)/
the rate guard removed|s/2 \* n >= firsts\[wa\]/True/
arguments no longer compared|s/wa == wb or ra != rb or/wa == wb or/
the 60 second window removed|s/> WINDOW/> 10 ** 9/
the binary file test removed|s/if b"\\0" in data\[:8000\]:/if False:/
LIST
rm -rf "$M"

say "== 2. end to end, bash without times"
row=$(grep -E '^line [0-9]+: for gti read git, +3 times\.$' out/c2-page.txt)
say "row: $row"
n=$(printf '%s' "$row" | sed -E 's/^line ([0-9]+):.*/\1/')
say "sed -n ${n}p: $(sed -n "${n}p" out/capture/bash-plain.hist)"
check "2 one row, gti read git, 3 times" '[ "$(grep -c "^line " out/c2-page.txt)" = 1 ] && [ -n "$row" ]'
check "2 sed finds the first gti status" '[ "$(sed -n "${n}p" out/capture/bash-plain.hist)" = "gti status" ] && [ "$(grep -n "^gti " out/capture/bash-plain.hist | head -1 | cut -d: -f1)" = "$n" ]'
check "2 alias block is exactly alias gti=git" '[ "$(cat out/c2-aliases.txt)" = "alias gti='"'"'git'"'"'" ] && [ "$(tail -1 out/c2-page.txt)" = "alias gti='"'"'git'"'"'" ]'
check "2 no-times statement and thinning" 'joined out/c2-page.txt | grep -qF "This history keeps no times. Corrections were taken from adjacent lines." && joined out/c2-page.txt | grep -qF "Counts are what this file still holds. A shell set to drop duplicates or old entries holds fewer."'

say "== 3. the build box's own default history"
uv run corrigenda.py > out/c3-page.txt 2> out/c3-stderr.txt
rc3=$?
say "exit $rc3; head: $(grep -m1 "to the shell history" out/c3-page.txt | sed "s/^ *//")"
check "3 exits 0 with no traceback" '[ $rc3 = 0 ] && ! grep -q Traceback out/c3-page.txt out/c3-stderr.txt'
F3=$(grep -m1 -oE 'history at ~[^,]+' out/c3-page.txt | sed "s#^history at ~#$HOME#")
top=$(grep -m1 -E '^line ' out/c3-page.txt)
if [ -n "$top" ]; then
  A=$(printf '%s' "$top" | sed -E 's/.*: for ([^ ]+) read ([^ ]+),.*/\1/')
  B=$(printf '%s' "$top" | sed -E 's/.*: for ([^ ]+) read ([^ ]+),.*/\2/')
  C=$(printf '%s' "$top" | sed -E 's/.* ([0-9]+) times\.$/\1/')
  H=$(sed -E 's/^: [0-9]+:[0-9]+;//' "$F3" | grep -Ev '^#[0-9]+$' | awk -v A="$A" -v B="$B" 'prev ~ ("^" A "( |$)") && $0 ~ ("^" B "( |$)") {n++} {prev=$0} END{print n+0}')
  say "top row: $top; independent awk count $H"
  check "3 independent count is at least the printed count" '[ "$H" -ge "$C" ]'
else
  say "no row printed: $(grep -F 'stands as printed' out/c3-page.txt)"
  # Independent look: every adjacent pair with a different first word and the same rest of the line, first words only.
  say "adjacent pairs with identical arguments in $(printf '%s' "$F3" | sed "s#^$HOME#~#"), count and first words:"
  sed -E 's/^: [0-9]+:[0-9]+;//' "$F3" | grep -Ev '^#[0-9]+$' \
    | awk '{w = $1; r = $0; sub(/^[^ ]* ?/, "", r); if (NR > 1 && w != pw && r == pr) print pw, w} {pw = $1; pr = r}' \
    | sort | uniq -c | sort -rn | tee -a "$LOG"
  say "NOT RUN 3 top row comparison: the page printed no row"
fi

say "== 4. privacy"
check "4 no SECRET123, push or example.invalid in page or aliases" '! grep -qE "SECRET123|push|example\.invalid" out/c4-page.txt out/c4-aliases.txt'
check "4 the slip was found all the same" 'grep -qE "for gti read git, +3 times\." out/c4-page.txt'
say "history holds the secret $(grep -c SECRET123 out/capture/bash-secret.hist) times; page row: $(grep '^line ' out/c4-page.txt)"

say "== 5. time honesty"
check "5 bash plain: no-times statement" 'joined out/c2-page.txt | grep -qF "This history keeps no times."'
check "5 bash timestamped: every-entry statement" 'joined out/c5-timestamped.txt | grep -qF "Every entry carries a time. A correction counts if it follows within 60 seconds."'
check "5 zsh extended: every-entry statement" 'joined out/c5-zsh.txt | grep -qF "Every entry carries a time. A correction counts if it follows within 60 seconds."'
K=$(grep -cE '^#[0-9]+$' out/capture/bash-mixed.hist)
N=$(grep -vE '^#[0-9]+$' out/capture/bash-mixed.hist | grep -c .)
check "5 bash mixed: $K of $N statement" 'joined out/c5-mixed.txt | grep -qF "$K of $N entries carry times. Where both had one, the correction had to follow within 60 seconds; elsewhere adjacent lines were taken."'
for p in c2-page c5-timestamped c5-zsh c5-mixed; do
  check "5 thinning statement in $p" 'joined out/$p.txt | grep -qF "Counts are what this file still holds. A shell set to drop duplicates or old entries holds fewer."'
done
TS=$(awk '/^#[0-9]+$/ {ts = substr($0, 2)} /^gti / {print ts; exit}' out/capture/bash-timestamped.hist)
GAP=$(awk '/^#[0-9]+$/ {ts = substr($0, 2)} /^gti / {g = ts} /^git / {print ts - g}' out/capture/bash-timestamped.hist | tr '\n' ' ')
say "timestamped gaps gti to git, seconds: $GAP; row: $(grep '^line ' out/c5-timestamped.txt)"
check "5 the 301 s pair is not counted, the 5 s pairs are" 'grep -qE "for gti read git, +2 times\.$" out/c5-timestamped.txt'
check "5 the row is dated $(date -d @"$TS" +%F) (date -d @$TS)" 'grep -qE "^line [0-9]+, $(date -d @"$TS" +%F): for gti read git," out/c5-timestamped.txt'
ZTS=$(sed -nE 's/^: ([0-9]+):[0-9]+;gti .*/\1/p' out/capture/zsh-extended.hist | head -1)
check "5 zsh row is dated $(date -d @"$ZTS" +%F)" 'grep -qE "^line [0-9]+, $(date -d @"$ZTS" +%F): for gti read git," out/c5-zsh.txt'

say "== 6. the alias block is pure"
for f in out/capture/bash-plain.hist out/capture/zsh-extended.hist out/capture/bash-alias.hist; do
  cor --aliases "$f" > out/a.txt
  check "6 every --aliases line matches the pattern ($f)" '[ -s out/a.txt ] && ! grep -vqE "^alias [A-Za-z][A-Za-z0-9_-]*='"'"'[A-Za-z][A-Za-z0-9_-]*'"'"'$" out/a.txt'
  script -qec "uv run corrigenda.py --aliases $f" /dev/null > out/a-pty.txt
  check "6 no ESC on a terminal ($f)" '[ -s out/a-pty.txt ] && ! grep -q $'"'"'\x1b'"'"' out/a-pty.txt'
  cor "$f" > out/p.txt
  check "6 the same block ends the page ($f)" '[ "$(tail -n "$(wc -l < out/a.txt)" out/p.txt)" = "$(cat out/a.txt)" ]'
done
say "type: $(bash -c 'shopt -s expand_aliases; eval "$(uv run corrigenda.py --aliases out/capture/bash-plain.hist)"; type gti')"
check "6 bash takes the block" '[ "$(bash -c '"'"'shopt -s expand_aliases; eval "$(uv run corrigenda.py --aliases out/capture/bash-plain.hist)"; type gti'"'"')" = "gti is aliased to \`git'"'"'" ]'
check "6 heading names ~/.zshrc for the zsh fixture" 'grep -qx "Errata slip, to be tipped in at ~/.zshrc" out/c5-zsh.txt'
check "6 heading names ~/.bashrc for a bash fixture with SHELL=/bin/bash" '[ "$(SHELL=/bin/bash cor out/capture/bash-plain.hist | grep "^Errata")" = "Errata slip, to be tipped in at ~/.bashrc" ]'

say "== 7. emphasis and layout"
script -qec "uv run corrigenda.py out/capture/bash-alias.hist" /dev/null > out/c7-pty.txt
script -qec "uv run corrigenda.py out/capture/zsh-extended.hist" /dev/null >> out/c7-pty.txt
bolds=$(grep -oaE $'\x1b\\[1m[^\x1b]*\x1b\\[0m' out/c7-pty.txt | tr -d '\033' | tr '\n' ' ')
say "bold runs on a pty: $bolds"
check "7 on a pty only the counts are bold" '[ "$(grep -oa $'"'"'\x1b'"'"' out/c7-pty.txt | wc -l)" = 6 ] && [ "$bolds" = "[1m3[0m [1m2[0m [1m2[0m " ]'
NO_COLOR=1 script -qec "uv run corrigenda.py out/capture/zsh-extended.hist" /dev/null > out/c7-nocolor.txt
check "7 NO_COLOR=1 on a pty: no ESC" '[ -s out/c7-nocolor.txt ] && ! grep -q $'"'"'\x1b'"'"' out/c7-nocolor.txt'
check "7 piped through cat -v: no ^[" '! cor out/capture/zsh-extended.hist | cat -v | grep -qF "^["'
"${T[@]}" new-session -d -s view -x 80 -y 60 -c "$PWD" env -i HOME="$HOME" PATH="$PATH" TERM=xterm-256color LANG=C.UTF-8 PS1='$ ' HISTFILE= bash --norc --noprofile -i
sleep 1
key view 'clear; uv run corrigenda.py out/capture/zsh-extended.hist' 2
"${T[@]}" capture-pane -p -t view > out/c7-pane.txt
"${T[@]}" capture-pane -p -e -t view > out/c7-pane.ans
"${T[@]}" kill-session -t view
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' out/c7-pane.txt | sed '$d' > out/c7-page.txt  # drop trailing blanks and the next prompt
check "7 pane: no line over 79 columns (widest $(awk '{ if (length > m) m = length } END { print m }' out/c7-page.txt))" '[ "$(awk "length > 79" out/c7-page.txt | wc -l)" = 0 ]'
check "7 pane: title centred on the 72 measure" 'grep -qxE " {31}CORRIGENDA" out/c7-page.txt'
check "7 pane: colophon ends with the typeface line, then normal is dead" 'grep -A1 -x "Set in the typeface your terminal chose." out/c7-page.txt | tail -1 | grep -qx "normal is dead"'
cor --aliases out/capture/zsh-extended.hist > out/a.txt
check "7 pane: the alias block is last, after the colophon" '[ "$(tail -n 2 out/c7-page.txt)" = "$(cat out/a.txt)" ] && [ "$(grep -n "^normal is dead$" out/c7-page.txt | cut -d: -f1)" -lt "$(grep -n "^alias " out/c7-page.txt | head -1 | cut -d: -f1)" ]'
check "7 pane: bold only on counts (tmux capture -e)" '[ "$(grep -c $'"'"'\x1b'"'"' out/c7-pane.ans)" = 2 ]'
check "7 widest row unit test ran" 'grep -q "test_widest_row .* ok" out/unittest.log'

say "== 8. edges"
check "8 no recurring slip: stands as printed, exit 0, thinning" '[ "$(cat out/c11.rc)" = 0 ] && grep -qxF "No correction recurs in this history. The edition stands as printed." out/c11-page.txt && joined out/c11-page.txt | grep -qF "Counts are what this file still holds. A shell set to drop duplicates or old entries holds fewer."'
cor /nonexistent/history > out/c8-out.txt 2> out/c8-err.txt
rc8=$?
say "nonexistent path: exit $rc8, stderr: $(cat out/c8-err.txt)"
check "8 nonexistent path: one sentence, exit 2, no traceback" '[ $rc8 = 2 ] && [ "$(wc -l < out/c8-err.txt)" = 1 ] && [ ! -s out/c8-out.txt ] && ! grep -q Traceback out/c8-err.txt'
EMPTY=$(mktemp -d)
env -u HISTFILE HOME="$EMPTY" SHELL=/bin/bash PYTHONDONTWRITEBYTECODE=1 "$PY" corrigenda.py > /dev/null 2> out/c8-err.txt
rc8=$?
say "no history anywhere: exit $rc8, stderr: $(cat out/c8-err.txt)"
check "8 no history found: the sentence, exit 2" '[ $rc8 = 2 ] && [ "$(cat out/c8-err.txt)" = "No history file was found. Looked in: ~/.bash_history, ~/.zsh_history." ]'
cor "$WORK" > /dev/null 2> out/c8-err.txt
rc8=$?
say "a directory: exit $rc8, stderr: $(sed "s#$WORK#WORK#" out/c8-err.txt)"
check "8 a directory: one sentence, exit 2" '[ $rc8 = 2 ] && [ "$(wc -l < out/c8-err.txt)" = 1 ] && ! grep -q Traceback out/c8-err.txt'
head -c 65536 /dev/urandom > "$WORK/noise"
for bin in /usr/bin/ls "$WORK/noise"; do
  cor "$bin" > out/c8-out.txt 2> out/c8-err.txt
  rc8=$?
  say "binary $(basename "$bin"): exit $rc8, stderr: $(sed "s#$WORK#WORK#" out/c8-err.txt)"
  check "8 binary input: one sentence, exit 2, no traceback ($(basename "$bin"))" '[ $rc8 = 2 ] && [ "$(wc -l < out/c8-err.txt)" = 1 ] && [ ! -s out/c8-out.txt ] && ! grep -q Traceback out/c8-err.txt'
done
head -c 65536 /dev/urandom | tr -d '\000' > "$WORK/latin"
iconv -f UTF-8 -t UTF-8 "$WORK/latin" > /dev/null 2>&1 && say "unexpected: the noise decodes as UTF-8"
say "zsh capture, the echo 2 line in hex: $(LC_ALL=C grep -a 'echo 2' out/capture/zsh-extended.hist | od -An -tx1 | tr -s ' \n' ' ')"
check "8 the zsh capture holds zsh's metafy byte 0x83 and is not UTF-8" 'LC_ALL=C grep -qa $'"'"'\x83'"'"' out/capture/zsh-extended.hist && ! iconv -f UTF-8 -t UTF-8 out/capture/zsh-extended.hist > /dev/null 2>&1'
for f in "$WORK/latin" out/capture/zsh-extended.hist; do
  cor "$f" > out/c8-out.txt 2>&1
  rc8=$?
  check "8 not UTF-8 and no NUL: a page, exit 0, no traceback ($(basename "$f"), exit $rc8)" '[ $rc8 = 0 ] && ! grep -q Traceback out/c8-out.txt && grep -qx "normal is dead" out/c8-out.txt'
done
for p in out/c2-page.txt out/c4-page.txt out/c5-*.txt out/c11-page.txt out/c3-page.txt out/c1-alias.txt; do
  check "8 thinning statement on $p" 'joined $p | grep -qF "Counts are what this file still holds. A shell set to drop duplicates or old entries holds fewer."'
done

say "== 9. cleanliness"
SRC=(corrigenda.py test_corrigenda.py e2e.sh shots.py README.md BUILD.md)
check "9 no U+2014 in sources, README or output" '! grep -l $'"'"'\xe2\x80\x94'"'"' "${SRC[@]}" out/*.txt 2>/dev/null | grep -q .'
D=-  # spelled this way so the pattern does not match itself
DASH="(^|[^-])$D$D([^-[:alpha:]]|\$)|[[:alnum:]]$D$D[[:alnum:]]"
check "9 no doubled hyphen used as a dash" '! grep -nE "$DASH" "${SRC[@]}" out/*.txt 2>/dev/null | grep -q .'
grep -E '^(import|from)' corrigenda.py | tee -a "$LOG"
check "9 only the listed stdlib imports" '[ -z "$(grep -E "^(import|from)" corrigenda.py | grep -vxE "import (argparse|os|re|shutil|sys)|from (collections|datetime) import .*")" ]'
say "interpreter: $PY"
PYTHONDONTWRITEBYTECODE=1 strace -f -e trace=socket,connect,openat -o out/strace.txt "$PY" corrigenda.py out/capture/zsh-extended.hist > /dev/null
sed -i "s#$HOME#~#g" out/strace.txt  # the kept trace names no home directory
say "strace: $(grep -c openat out/strace.txt) openat calls, $(grep -cE 'connect\(|socket\(' out/strace.txt) socket or connect calls"
check "9 no network, no file opened for writing" '! grep -qE "connect\(|socket\(AF_INET|O_WRONLY|O_RDWR|O_CREAT" out/strace.txt'

say "== 10. README"
check "10 no home path or username in README" '! grep -qE "/home/|/Users/" README.md && ! grep -qw "$USER" README.md'
check "10 no output pasted in README" '! grep -qE "read [A-Za-z0-9_-]+, +[0-9]+ times|^ *CORRIGENDA$" README.md'

say "== 11. retirement"
G=$(grep -c '^gti ' out/capture/bash-plain-retired.hist)
F=$(awk 'prev ~ /^gti / && /^git / {n++} {prev = $0} END {print n + 0}' out/capture/bash-plain-retired.hist)
say "the file holds $G gti entries, $F followed by git"
check "11 after the alias is adopted the leaf stands as printed" '[ "$G" = 7 ] && [ "$F" = 3 ] && grep -qxF "No correction recurs in this history. The edition stands as printed." out/c11-page.txt && ! grep -q "^alias" out/c11-page.txt'

"${T[@]}" kill-server 2>/dev/null
rm -rf "$WORK" "$EMPTY" out/a.txt out/a-pty.txt out/p.txt
say "== $failed failed"
[ "$failed" = 0 ]
