#!/usr/bin/env bash

# Source-build pipeline test: builds GNU hello from source with mise's
# formula shim (mise-managed ruby, no Homebrew installed). Uses a throwaway
# prefix, so no root is needed. Requires network and a C toolchain.
if [[ "$(uname)" != "Linux" ]] || ! command -v cc >/dev/null 2>&1; then
  exit 0
fi
case "$(uname -m)" in
  x86_64 | aarch64) ;;
  *) exit 0 ;;
esac

export MISE_SYSTEM_BREW_PREFIX="$HOME/brew-source-prefix"
# hello has bottles everywhere; force it down the source-build path
export MISE_SYSTEM_BREW_FORCE_SOURCE=hello
# GNU's load-balancing mirror intermittently returns 502s. Use the canonical
# archive host so this test exercises mise's source builder instead of mirror
# availability; the Homebrew-provided sha256 still verifies the download.
export MISE_URL_REPLACEMENTS='{"https://ftpmirror.gnu.org/gnu":"https://ftp.gnu.org/gnu"}'

cat <<EOF >mise.toml
[bootstrap.packages]
"brew:hello" = "latest"
EOF

assert_contains "mise bootstrap packages status" "missing"
assert_contains "mise bootstrap packages apply --dry-run" "build hello/"

mise bootstrap packages apply --yes
assert_contains "mise bootstrap packages status" "installed"

# the built binary runs from the prefix and the keg carries a receipt
assert_contains "$MISE_SYSTEM_BREW_PREFIX/bin/hello --greeting=mise" "mise"
assert_contains "cat $MISE_SYSTEM_BREW_PREFIX/Cellar/hello/*/INSTALL_RECEIPT.json" '"poured_from_bottle":false'
assert_succeed "test -L $MISE_SYSTEM_BREW_PREFIX/var/homebrew/linked/hello && test -e $MISE_SYSTEM_BREW_PREFIX/var/homebrew/linked/hello"

# idempotent
assert_contains "mise bootstrap packages apply --yes 2>&1" "already"
