#!/usr/bin/env bash

# Test that global postinstall hooks receive MISE_INSTALLED_TOOLS JSON array

cat <<EOF >mise.toml
[tools]
dummy = "latest"

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF

# Remove any existing dummy installation to force reinstall
rm -rf "$MISE_DATA_DIR/installs/dummy"

# Run install and capture output
output=$(mise install dummy 2>&1)

# Check that MISE_INSTALLED_TOOLS is set and contains JSON
if [[ $output == *'INSTALLED_TOOLS=[{"name":"dummy"'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS contains JSON with tool info"
else
  echo "✗ MISE_INSTALLED_TOOLS is NOT set correctly"
  echo "Output: $output"
  exit 1
fi

# Check that it includes the version field
if [[ $output == *'"version":'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS includes version field"
else
  echo "✗ MISE_INSTALLED_TOOLS does NOT include version field"
  echo "Output: $output"
  exit 1
fi

# Check that it reports the unresolved selector alongside the resolved version
# so the hook can tell a floating request from a pin (#13270)
if [[ $output == *'"requested_version":"latest"'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS includes requested_version for a latest request"
else
  echo "✗ MISE_INSTALLED_TOOLS does NOT include requested_version"
  echo "Output: $output"
  exit 1
fi
if [[ $output == *'"version":"latest"'* ]]; then
  echo "✗ version should be the resolved version, not the selector"
  echo "Output: $output"
  exit 1
fi

# A version prefix resolves to a concrete version but is reported verbatim
rm -rf "$MISE_DATA_DIR/installs/dummy"
output3=$(mise install dummy@1 2>&1)
if [[ $output3 == *'"version":"1.1.0","requested_version":"1"'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS reports a version prefix verbatim"
else
  echo "✗ MISE_INSTALLED_TOOLS did NOT report the version prefix"
  echo "Output: $output3"
  exit 1
fi

# An alias is reported by its alias name, not the version it points at
rm -rf "$MISE_DATA_DIR/installs/dummy"
cat <<EOF >mise.toml
[alias.dummy]
myalias = "1.0.0"

[tools]
dummy = "myalias"

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF
output4=$(mise install 2>&1)
if [[ $output4 == *'"version":"1.0.0","requested_version":"myalias"'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS reports an alias verbatim"
else
  echo "✗ MISE_INSTALLED_TOOLS did NOT report the alias"
  echo "Output: $output4"
  exit 1
fi

# Restore the original `latest` config for the no-op check below
rm -rf "$MISE_DATA_DIR/installs/dummy"
cat <<EOF >mise.toml
[tools]
dummy = "latest"

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF
mise install >/dev/null 2>&1

# A no-op `mise install` (everything already installed) still runs postinstall,
# now with an empty JSON array rather than an unset variable (#10574)
output2=$(mise install 2>&1)
if [[ $output2 == *'INSTALLED_TOOLS=[]'* ]]; then
  echo "✓ MISE_INSTALLED_TOOLS is [] on a no-op install"
else
  echo "✗ MISE_INSTALLED_TOOLS should be [] on a no-op install"
  echo "Output: $output2"
  exit 1
fi
