#!/usr/bin/env bash

if [ -n "$EXCLUDE_FROM_CI" ]; then
  echo "This test is not supported in CI, because it requires a SSH key to be added to the GitHub account"
  exit 0
fi

#################################################################################
# Setup
#################################################################################

REMOTE_TASKS_DIR="${MISE_CACHE_DIR}/remote-git-tasks-cache"
MISE_V2025117_CACHE_DIR="${REMOTE_TASKS_DIR}/2fc21c39bc011033ee92059b37f2e91f844600fbbf78434b2782900949056f7e"

cargo init --name hello_cargo

#################################################################################
# Test remote tasks with no ref
#################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_ssh_latest]
file  = "git::ssh://git@github.com:jdx/mise.git//xtasks/lint/clippy"
EOF

assert_contains "mise tasks" "remote_lint_ssh_latest"
assert_succeed "mise run remote_lint_ssh_latest" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_ssh_latest" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_ssh_latest --no-cache" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_ssh_latest" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

# #################################################################################
# # Test remote tasks with with ref
# #################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_ssh_ref]
file  = "git::ssh://git@github.com:jdx/mise.git//xtasks/lint/clippy?ref=v2025.1.17"
EOF

assert_contains "mise tasks" "remote_lint_ssh_ref"
assert_succeed "mise run remote_lint_ssh_ref" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_V2025117_CACHE_DIR}"
assert_directory_not_empty "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_ssh_ref" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_ssh_ref --no-cache" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_ssh_ref" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_V2025117_CACHE_DIR}"
assert_directory_not_empty "${MISE_V2025117_CACHE_DIR}"
