#!/usr/bin/env bash

# Test that "npm may be required" warning is NOT shown when node is configured
# in the same mise.toml alongside an npm package (even if node isn't installed yet).
# Regression test for https://github.com/jdx/mise/discussions/8919

# Create a PATH that has mise but not npm/node
MISE_BIN="$(command -v mise)"
MISE_DIR="$(dirname "$MISE_BIN")"
export PATH="$MISE_DIR:/usr/bin:/bin:/usr/sbin:/sbin"

# Verify npm is not on PATH — if it is, the restricted PATH isn't working
if command -v npm >/dev/null 2>&1; then
	echo "ERROR: npm is found in restricted PATH, test cannot verify behavior"
	exit 1
fi

# Create a mise.toml that has both node and an npm package
cat >mise.toml <<'EOF'
[tools]
node = "latest"
"npm:prettier" = "3"
EOF

# Run ls-remote for the npm package — should NOT show the warning since node is configured
output=$(MISE_LOG_LEVEL=warn mise ls-remote npm:prettier 2>&1 || true)

if [[ $output == *"npm may be required but was not found"* ]]; then
	echo "FAIL: warning was shown even though node is configured in mise.toml"
	echo "Output: $output"
	exit 1
fi

echo "PASS: no spurious npm warning when node is configured"
