#!/usr/bin/env bash
set -xeuo pipefail

source mapping/artifact/tools/test_utilities.sh

wrapper_tool=mapping/artifact/artifact

function setup() {
  make_test_case_environment "$wrapper_tool"
}

function test_no_args() {
  (
    builtin cd "$(setup)"

    assert_failure 1 "Usage: artifact .*" "$wrapper_tool"
  )
}

function test_unknown_verb() {
  (
    builtin cd "$(setup)"

    assert_failure \
      1 ".*/unknown_verb: No such file or directory" \
      "$wrapper_tool" unknown_verb
  )
}

function test_help() {
  (
    builtin cd "$(setup)"

    [[ $("$wrapper_tool" -h) == "Usage: artifact"* ]]
    [[ $("$wrapper_tool" -help) == "Usage: artifact"* ]]
    [[ $("$wrapper_tool" --help) == "Usage: artifact"* ]]
  )
}

function test_layer_verb() {
  local wrapped_tool=mapping/artifact/tools/layer
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    [ "$("$wrapper_tool" layer)" == "$wrapped_tool" ]

    [ "$("$wrapper_tool" layer arg)" == "$wrapped_tool arg" ]

    [ "$("$wrapper_tool" layer arg1 arg2 arg3)" == \
      "$wrapped_tool arg1 arg2 arg3" ]
  )
}

function test_map_verb() {
  local wrapped_tool=mapping/artifact/tools/map
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    [ "$("$wrapper_tool" map)" == "$wrapped_tool" ]

    [ "$("$wrapper_tool" map arg)" == "$wrapped_tool arg" ]

    [ "$("$wrapper_tool" map arg1 arg2 arg3)" == \
      "$wrapped_tool arg1 arg2 arg3" ]
  )
}

function test_revision_verb() {
  local wrapped_tool=mapping/artifact/tools/revision
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    [ "$("$wrapper_tool" revision)" == "$wrapped_tool" ]

    [ "$("$wrapper_tool" revision arg)" == "$wrapped_tool arg" ]

    [ "$("$wrapper_tool" revision arg1 arg2 arg3)" == \
      "$wrapped_tool arg1 arg2 arg3" ]
  )
}

function main() {
  test_no_args
  test_unknown_verb
  test_help
  test_layer_verb
  test_map_verb
  test_revision_verb
}

main
