#!/usr/bin/env bash

export MISE_LOCKFILE=1

echo "=== Testing mise lock skips global config by default ==="
# Set up a tool in the global config
cat >"$MISE_CONFIG_DIR/config.toml" <<EOF
[tools]
"aqua:jqlang/jq" = "1.7.1"
EOF

# Set up a tool in the local project config
cat >mise.toml <<EOF
[tools]
"aqua:mikefarah/yq" = "4.44.6"
EOF

rm -f mise.lock "$MISE_CONFIG_DIR/mise.lock"

# Running mise lock should only create the project lockfile, not the global one
output=$(mise lock --platform linux-x64 2>&1)
assert_contains "echo '$output'" "Processing 1 tool(s)"
assert_contains "echo '$output'" "mikefarah/yq"
assert "test -f mise.lock"
assert "test ! -f $MISE_CONFIG_DIR/mise.lock"

echo "=== Testing mise lock --global includes global config ==="
rm -f mise.lock "$MISE_CONFIG_DIR/mise.lock"

output=$(mise lock --global --platform linux-x64 2>&1)
# Should process tools from both project and global configs
assert "test -f mise.lock"
assert "test -f $MISE_CONFIG_DIR/mise.lock"
assert_contains "cat $MISE_CONFIG_DIR/mise.lock" "jqlang/jq"
assert_contains "cat mise.lock" "mikefarah/yq"

echo "=== Cleanup ==="
rm -f mise.toml mise.lock "$MISE_CONFIG_DIR/config.toml" "$MISE_CONFIG_DIR/mise.lock"

echo "mise lock global tests passed!"
