#!/usr/bin/env bash

# Direct Homebrew metadata tests need a macOS CI runner with network. Keep this
# out of local runs so it never removes a developer's installed applications.
if [[ "$(uname)" != "Darwin" ]] || [[ ${CI:-} != "true" ]]; then
  exit 0
fi

cask_app="/Applications/Hidden Bar.app"
caskroom="/opt/homebrew/Caskroom/hiddenbar"

if [[ -e $cask_app ]] || [[ -e $caskroom ]]; then
  exit 0
fi

cleanup() {
  shopt -s nullglob
  local app_work_paths=(
    /Applications/Hidden\ Bar.mise-old-*
    /Applications/Hidden\ Bar.mise-tmp-*
  )
  chmod -RN "$cask_app" "${app_work_paths[@]}" 2>/dev/null || true
  rm -rf "$cask_app" "$caskroom" "${app_work_paths[@]}"
}
trap cleanup EXIT

assert_contains "mise bootstrap packages brew tap --dry-run jdx/tap" "~/.config/mise/config.toml"
mise bootstrap packages brew tap jdx/tap
assert_contains "cat ~/.config/mise/config.toml" '[bootstrap.brew.taps]'
assert_contains "cat ~/.config/mise/config.toml" '"jdx/tap" = "https://github.com/jdx/homebrew-tap.git"'

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

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

assert_directory_exists "$cask_app"
assert_directory_exists "$caskroom"
assert_contains "mise bootstrap packages status" "installed"

# Force a reinstall while the existing app contains a delete-protected
# directory. This exercises the complete app swap and permission-recovery path
# without downloading the much larger docker-desktop cask.
chmod +a "everyone deny delete_child" "$cask_app/Contents"
rm -rf "$caskroom"
assert_contains "mise bootstrap packages status" "missing"
mise bootstrap packages apply --yes

assert_directory_exists "$cask_app"
assert_directory_exists "$caskroom"
assert_contains "mise bootstrap packages status" "installed"
assert_fail "find /Applications -maxdepth 1 -name 'Hidden Bar.mise-old-*' -print -quit | grep -q ."

# aube's install docs reference jdx/tap/aube. It currently lives in
# homebrew/core and the public jdx tap does not publish aube API metadata, so
# this assertion starts validating the tapped install path as soon as that tap
# metadata is available without falling back to the brew CLI.
if curl --retry 3 --retry-delay 1 --max-time 10 -fsSL https://raw.githubusercontent.com/jdx/homebrew-tap/HEAD/api/formula/aube.json >/dev/null 2>&1; then
  cat <<EOF >mise.toml
[bootstrap.packages]
"brew:jdx/tap/aube" = "latest"
EOF
  mise bootstrap packages apply --yes
  assert_contains "/opt/homebrew/bin/aube --version" "aube"
fi
