#!/usr/bin/env bash

# tests that "test.sh" gets mapped to "test" except when an existing task is there named "test"
assert "mise task add --file test.sh --description sh"
assert "mise task ls" "test  sh"
assert_json_partial_array "mise task ls --json" "name,description,source" "[
  {
    \"name\": \"test\",
    \"description\": \"sh\",
    \"source\": \"$PWD/mise-tasks/test.sh\"
  }
]"
assert "mise task ls --usage" 'cmd test help=sh'

assert "mise task add --file test --description no-sh"
assert "mise task ls" "test     no-sh
test.sh  sh"
assert_json_partial_array "mise task ls --json" "name,description,source" "[
  {
    \"name\": \"test\",
    \"description\": \"no-sh\",
    \"source\": \"$PWD/mise-tasks/test\"
  },
  {
    \"name\": \"test.sh\",
    \"description\": \"sh\",
    \"source\": \"$PWD/mise-tasks/test.sh\"
  }
]"

assert_json_partial_array "mise task ls --json" "name,description,source" "$(
  cat <<EOF
[
  {
    "name": "test",
    "description": "no-sh",
    "source": "$PWD/mise-tasks/test"
  },
  {
    "name": "test.sh",
    "description": "sh",
    "source": "$PWD/mise-tasks/test.sh"
  }
]
EOF
)"

assert "mise task ls --usage" 'cmd test help=no-sh
cmd test.sh help=sh'

touch script.sh
chmod +x script.sh
cat <<EOF >mise.toml
[tasks.mytask]
run = [
  'echo abc',
  './script.sh {{flag(name="test", default="false")}}',
]
EOF

assert "mise run mytask" "abc"
