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

mkdir -p system-config
cat >system-config/config.toml <<EOF
[tools]
"aqua:cli/cli" = "2.65.0"
EOF
export MISE_SYSTEM_CONFIG_FILE="$PWD/system-config/config.toml"

# 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" system-config/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"
assert "test ! -f system-config/mise.lock"

echo "=== Testing mise lock --global targets only global config ==="
rm -f mise.lock "$MISE_CONFIG_DIR/mise.lock" system-config/mise.lock

output=$(mise lock --global --platform linux-x64 2>&1)
# Should process only tools from global configs
assert "test ! -f mise.lock"
assert "test -f $MISE_CONFIG_DIR/mise.lock"
assert "test -f system-config/mise.lock"
assert_contains "cat $MISE_CONFIG_DIR/mise.lock" "jqlang/jq"
assert_contains "cat system-config/mise.lock" "cli/cli"

echo "=== Testing mise lock points at --global when only global config has tools ==="
rm -rf mise.toml mise.lock "$MISE_CONFIG_DIR/mise.lock" system-config

# jq is still declared in the global config, so `mise lock` has nothing in project
# scope but `mise lock --global` does. Say so instead of "No tools configured".
output=$(mise lock 2>&1)
assert_contains "echo '$output'" "mise lock --global"
assert "test ! -f mise.lock"

echo "=== Testing the dotfiles layout that symlinks the global config to a project config ==="
# A config reached through both the global path and the project path is merged into
# a single global entry, so plain `mise lock` finds no project-scoped tools.
mkdir -p dotfiles
cat >dotfiles/mise.toml <<EOF
[tools]
"aqua:mikefarah/yq" = "4.44.6"
EOF
ln -sf "$PWD/dotfiles/mise.toml" "$MISE_CONFIG_DIR/config.toml"

( # subshell to avoid cd'ing back (SC2103)
  cd dotfiles

  output=$(mise lock 2>&1)
  assert_contains "echo '$output'" "mise lock --global"
  assert "test ! -f mise.lock"

  # --global locks it, writing the lockfile beside the symlink target
  assert "mise lock --global --platform linux-x64"
  assert_contains "cat mise.lock" "mikefarah/yq"
)

echo "=== Testing the hint honors tool selectors ==="
# Drop the symlink before its target, so the global config path is a real file again
rm -f "$MISE_CONFIG_DIR/config.toml"
rm -rf dotfiles
# Global declares jq; a project config with no tools keeps project scope empty.
cat >"$MISE_CONFIG_DIR/config.toml" <<EOF
[tools]
"aqua:jqlang/jq" = "1.7.1"
EOF
cat >mise.toml <<EOF
[env]
FOO = "bar"
EOF

# A selector naming a tool that is not in global config must not point at --global
output=$(mise lock "aqua:mikefarah/yq" 2>&1)
assert_contains "echo '$output'" "No tools configured to lock"
assert_not_contains "echo '$output'" "mise lock --global"

# A selector naming a tool that *is* in global config should
output=$(mise lock "aqua:jqlang/jq" 2>&1)
assert_contains "echo '$output'" "mise lock --global"

echo "=== Testing the hint stays out of --local runs ==="
# --local asks for mise.local.lock; there is no global equivalent to point at
output=$(mise lock --local 2>&1)
assert_contains "echo '$output'" "No tools configured to lock"
assert_not_contains "echo '$output'" "mise lock --global"

echo "=== Testing mise lock with nothing configured anywhere ==="
rm -f "$MISE_CONFIG_DIR/config.toml" mise.toml
rm -rf dotfiles

output=$(mise lock 2>&1)
assert_contains "echo '$output'" "No tools configured to lock"
assert_not_contains "echo '$output'" "mise lock --global"

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

echo "mise lock global tests passed!"
