#!/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

# Run install and capture output
output=$(mise install dummy 2>&1)
installed_tools=$(printf '%s\n' "$output" | sed -n 's/^.*INSTALLED_TOOLS=//p' | tail -n 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

# The backend is the canonical identifier without options, and install_path is
# the concrete version directory rather than a floating selector.
assert "printf '%s' '$installed_tools' | jq -r '.[0].backend'" "asdf:dummy"
installed_version=$(printf '%s' "$installed_tools" | jq -r '.[0].version')
assert "printf '%s' '$installed_tools' | jq -r '.[0].install_path'" "$MISE_DATA_DIR/installs/dummy/$installed_version"

# A version prefix resolves to a concrete version but is reported verbatim
output3=$(MISE_INSTALLS_DIR="$MISE_DATA_DIR/installs-prefix" 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
cat <<EOF >mise.toml
[alias.dummy]
myalias = "1.0.0"

[tools]
dummy = "myalias"

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF
output4=$(MISE_INSTALLS_DIR="$MISE_DATA_DIR/installs-alias" 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
cat <<EOF >mise.toml
[tools]
dummy = "latest"

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF
# 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

# A batch hook still receives successful installations when another requested
# version fails, while the overall install remains unsuccessful.
cat <<EOF >mise.toml
[tools]
dummy = ["1.0.0", "other-dummy"]

[hooks]
postinstall = "echo INSTALLED_TOOLS=\$MISE_INSTALLED_TOOLS"
EOF
set +e
output5=$(MISE_INSTALLS_DIR="$MISE_DATA_DIR/installs-partial" mise install 2>&1)
status5=$?
set -e
if [[ $status5 == 0 ]]; then
  echo "✗ partial install should retain the failed command status"
  echo "Output: $output5"
  exit 1
fi
installed_tools5=$(printf '%s\n' "$output5" | sed -n 's/^.*INSTALLED_TOOLS=//p' | tail -n 1)
assert "printf '%s' '$installed_tools5' | jq -r 'length'" "1"
assert "printf '%s' '$installed_tools5' | jq -r '.[0].version'" "1.0.0"
assert "printf '%s' '$installed_tools5' | jq -r '.[0].backend'" "asdf:dummy"
assert "printf '%s' '$installed_tools5' | jq -r '.[0].install_path'" "$MISE_DATA_DIR/installs-partial/dummy/1.0.0"
