#!/usr/bin/env bash

export MISE_DEBUG=0 MISE_TRACE=0 MISE_LOG_LEVEL=info

mise install dummy@1.0.0
cat >mise.toml <<'TOML'
[tools]
dummy = { version = "1.0.0", postinstall = "mv mise.toml saved.toml; mkdir mise.toml" }
TOML

# The hook lets installation succeed but makes the subsequent config save fail.
# A directory at the destination fails even when the test runs as root.
if output=$(MISE_FORCE_PROGRESS=1 mise upgrade --bump 2>&1); then
  fail "upgrade should fail when its config cannot be saved: $output"
fi
printf '%s' "$output" | python3 -c 'import re, sys; print(re.sub(r"\x1b\[[0-9;?]*[a-zA-Z]", "", sys.stdin.read()))' >output.log
assert_contains 'cat output.log' 'Failed to save config'
assert_contains 'cat output.log' 'Upgraded 1 tool:'
assert_contains 'cat output.log' 'dummy 1.0.0 → 2.0.0'
assert_not_contains 'cat output.log' '✓ dummy@2.0.0'
assert "test -d '$MISE_DATA_DIR/installs/dummy/2.0.0'"
