#!/usr/bin/env bash

# Test that hook errors are handled gracefully (no panics, warnings shown)

# Test 1: Hooks with a failing script should warn but not crash
cat <<EOF >mise.toml
[tools]
dummy = 'latest'
[hooks]
preinstall = 'exit 1'
postinstall = 'echo POST'
EOF

# Preinstall fails but postinstall should still run and install should succeed
rm -rf ~/.local/share/mise/installs/dummy
output=$(mise i 2>&1)
assert_contains "echo '$output'" "hook in"
assert_contains "echo '$output'" "failed:"
# postinstall should still fire even though preinstall failed
assert_contains "echo '$output'" "POST"

# Test 2: Invalid hook script (nonexistent command) should warn
rm -rf ~/.local/share/mise/installs/dummy
cat <<EOF >mise.toml
[tools]
dummy = 'latest'
[hooks]
preinstall = 'nonexistent_command_12345'
EOF

# Should warn about hook error but not crash
output=$(mise i 2>&1)
assert_contains "echo '$output'" "hook in"
assert_contains "echo '$output'" "failed:"
