#!/usr/bin/env bash # Behavior check: a fresh interactive zsh runs `fm` and the permanent launcher # starts OpenCode 2 through ~/.local/bin/oc2, never 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 # `fm` with two 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. # Nothing here launches a real firstmate primary, Pi, or OpenCode, so no # session lock is taken. # # 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='' 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' >> "$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' >> "$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; } : > "$TMP/spy.log" : > "$TMP/stderr" set +e # shellcheck disable=SC2016 # the snippet expands inside the interactive zsh FM_TEST_ZSHRC="$ZSHRC" \ 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 >/dev/null 2>&1; then echo "fm-launcher: no fm alias in the sourced zshrc" >&2 exit 3 fi eval fm ' >/dev/null 2>"$TMP/stderr" status=$? set -e launched='' [ -s "$TMP/spy.log" ] && launched=$(head -n 1 "$TMP/spy.log") if [ "$status" -eq 3 ]; then echo "FAIL: $LABEL does not define an fm alias" exit 1 fi if [ "$status" -ne 0 ]; then echo "FAIL: fm launch from $LABEL exited $status" sed 's/^/ /' "$TMP/stderr" exit 1 fi if [ -z "$launched" ]; then echo "FAIL: fm from $LABEL launched nothing the spies could see" sed 's/^/ /' "$TMP/stderr" exit 1 fi if [ "${launched%%|*}" != oc2 ]; then echo "FAIL: fm from $LABEL launched ${launched%%|*} instead of OpenCode 2 (oc2)" echo " recorded launch: $launched" exit 1 fi if grep -q '^pi' "$TMP/spy.log"; then echo "FAIL: fm from $LABEL also launched Pi" sed 's/^/ /' "$TMP/spy.log" exit 1 fi echo "PASS: fm from $LABEL launched OpenCode 2 via oc2 ($launched)"