#!/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}/27d097acd7d7d15682b9d5f22bfa7c363b1951a50bbbd2aff3923eb949be0ef0"

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/ripgrep"
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/ripgrep?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}"

#################################################################################
# Test usage and info commands with git remote tasks (SSH)
# Verifies fix for https://github.com/jdx/mise/discussions/7578
#################################################################################

# Usage should work and show task in completion output
assert_contains "mise tasks ls --usage" 'cmd remote_lint_ssh_ref'

# Info should show local cached path, not git URL
assert_contains "mise tasks info remote_lint_ssh_ref" "remote-git-tasks-cache"
assert_not_contains "mise tasks info remote_lint_ssh_ref" "git::ssh"

# JSON output should also work
assert_contains "mise tasks info remote_lint_ssh_ref --json" '"file":'
assert_contains "mise tasks info remote_lint_ssh_ref --json" "remote-git-tasks-cache"

# Validate should work with remote tasks
assert_succeed "mise tasks validate remote_lint_ssh_ref"
