Files
dotfiles/tests/fm-launcher.sh
T

171 lines
5.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Behavior check: a fresh interactive zsh runs the firstmate launchers and each
# one starts the tool its name promises, from the firstmate home directory.
#
# fm -> OpenCode 2 through ~/.local/bin/oc2
# fm-oc2 -> OpenCode 2 through ~/.local/bin/oc2, identical to fm
# fm-pi -> Pi
#
# The check reads the zshrc that the real home-manager configuration generates
# (the same file a rebuild installs), sources it in an interactive zsh, and runs
# each launcher with three harmless spies:
# - a `pi` spy first on PATH, which catches a Pi launch;
# - a spy named by OPENCODE2_BIN, which the real ~/.local/bin/oc2 wrapper
# execs instead of the OpenCode binary;
# - the spies record their working directory, so a launcher that forgets the
# `cd` into the firstmate home fails here.
# Nothing here launches a real firstmate primary, Pi, or OpenCode, and the spies
# exit at once, so no session lock is taken and the primary stays alive.
#
# Usage:
# tests/fm-launcher.sh # check the configuration source (default)
# tests/fm-launcher.sh --live # check the installed ~/.zshrc
# tests/fm-launcher.sh --zshrc F # check a specific generated zshrc
set -euo pipefail
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)
MODE=source
ZSHRC=''
LAUNCHERS='fm:oc2 fm-oc2:oc2 fm-pi:pi'
FM_HOME=${FM_HOME:-"$HOME/Documents/firstmate"}
while [ $# -gt 0 ]; do
case "$1" in
--live) MODE=live; shift ;;
--zshrc)
[ $# -ge 2 ] || { echo "fm-launcher: --zshrc needs a path" >&2; exit 2; }
MODE="file"; ZSHRC=$2; shift 2 ;;
-h|--help)
awk 'NR == 1 { next } /^#/ { sub(/^# ?/, ""); print; next } { exit }' "$0"
exit 0 ;;
*) echo "fm-launcher: unknown argument $1" >&2; exit 2 ;;
esac
done
TMP=$(mktemp -d "${TMPDIR:-/tmp}/fm-launcher.XXXXXX")
cleanup() { rm -rf "$TMP"; }
trap cleanup EXIT
mkdir -p "$TMP/bin"
cat > "$TMP/bin/pi" <<'SH'
#!/usr/bin/env bash
printf 'pi|%s' "$PWD" >> "$FM_TEST_SPY_LOG"
for arg in "$@"; do printf '|%s' "$arg" >> "$FM_TEST_SPY_LOG"; done
printf '\n' >> "$FM_TEST_SPY_LOG"
exit 0
SH
cat > "$TMP/bin/opencode2-spy" <<'SH'
#!/usr/bin/env bash
printf 'oc2|%s' "$PWD" >> "$FM_TEST_SPY_LOG"
for arg in "$@"; do printf '|%s' "$arg" >> "$FM_TEST_SPY_LOG"; done
printf '\n' >> "$FM_TEST_SPY_LOG"
exit 0
SH
chmod +x "$TMP/bin/pi" "$TMP/bin/opencode2-spy"
printf '{}\n' > "$TMP/opencode.json"
case "$MODE" in
live)
ZSHRC="$HOME/.zshrc"
LABEL="installed ~/.zshrc"
;;
file)
LABEL="$ZSHRC"
;;
source)
command -v nix >/dev/null 2>&1 \
|| { echo "fm-launcher: nix is required to build the configured zshrc" >&2; exit 2; }
ZSHRC="$TMP/zshrc"
nix eval --raw --impure --expr \
"(builtins.getFlake (toString $ROOT)).darwinConfigurations.\"lt-mbp\".config.home-manager.users.laptran.home.file.\"./.zshrc\".text" \
> "$ZSHRC"
LABEL="home-manager zshrc generated from $ROOT"
;;
esac
[ -r "$ZSHRC" ] || { echo "fm-launcher: cannot read $ZSHRC" >&2; exit 2; }
# Run one launcher in a fresh interactive zsh and print what the spies saw.
launch() {
local launcher=$1
: > "$TMP/spy.log"
: > "$TMP/stderr"
set +e
# shellcheck disable=SC2016 # the snippet expands inside the interactive zsh
FM_TEST_ZSHRC="$ZSHRC" \
FM_TEST_LAUNCHER="$launcher" \
FM_TEST_SPY_LOG="$TMP/spy.log" \
OPENCODE2_BIN="$TMP/bin/opencode2-spy" \
OPENCODE_CONFIG="$TMP/opencode.json" \
PATH="$TMP/bin:$PATH" \
timeout 60 zsh -f -i -c '
source "$FM_TEST_ZSHRC" || exit 4
if ! alias "$FM_TEST_LAUNCHER" >/dev/null 2>&1; then
echo "fm-launcher: no $FM_TEST_LAUNCHER alias in the sourced zshrc" >&2
exit 3
fi
eval "$FM_TEST_LAUNCHER"
' >/dev/null 2>"$TMP/stderr"
LAUNCH_STATUS=$?
set -e
}
for pair in $LAUNCHERS; do
launcher=${pair%%:*}
expected=${pair##*:}
launch "$launcher"
status=$LAUNCH_STATUS
if [ "$status" -eq 3 ]; then
echo "FAIL: $LABEL does not define a $launcher alias"
exit 1
fi
if [ "$status" -ne 0 ]; then
echo "FAIL: $launcher from $LABEL exited $status"
sed 's/^/ /' "$TMP/stderr"
exit 1
fi
if [ ! -s "$TMP/spy.log" ]; then
echo "FAIL: $launcher from $LABEL launched nothing the spies could see"
sed 's/^/ /' "$TMP/stderr"
exit 1
fi
record=$(head -n 1 "$TMP/spy.log")
tool=${record%%|*}
dir=$(printf '%s' "${record#*|}" | cut -d'|' -f1)
if [ "$tool" != "$expected" ]; then
echo "FAIL: $launcher from $LABEL launched $tool instead of $expected"
echo " recorded launch: $record"
exit 1
fi
if [ "$dir" != "$FM_HOME" ]; then
echo "FAIL: $launcher from $LABEL ran in $dir instead of the firstmate home $FM_HOME"
echo " recorded launch: $record"
exit 1
fi
if [ "$expected" = oc2 ] && grep -q '^pi' "$TMP/spy.log"; then
echo "FAIL: $launcher from $LABEL also launched Pi"
sed 's/^/ /' "$TMP/spy.log"
exit 1
fi
case "$launcher" in
fm) fm_record=$record ;;
fm-oc2) fm_oc2_record=$record ;;
esac
echo "PASS: $launcher from $LABEL launched $expected in $dir ($record)"
done
if [ "$fm_record" != "$fm_oc2_record" ]; then
echo "FAIL: fm and fm-oc2 are not identical launchers"
echo " fm: $fm_record"
echo " fm-oc2: $fm_oc2_record"
exit 1
fi
echo "PASS: fm and fm-oc2 are identical launchers"