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

source mapping/artifact/tools/test_utilities.sh

wrapper_tool=mapping/artifact/tools/map

function setup() {
  make_test_case_environment "$wrapper_tool"
}

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

    assert_failure \
      2 "usage: map\.py .*map\.py: error: the following arguments are required: subcommand" \
      "$wrapper_tool"
  )
}

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

    assert_failure \
      2 "usage: map\.py .*map\.py: error: argument subcommand: invalid choice: 'unknown_verb'" \
      "$wrapper_tool" unknown_verb
  )
}

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

    [[ $("$wrapper_tool" -h) == "usage: map.py"* ]]
    [[ $("$wrapper_tool" --help) == "usage: map.py"* ]]
  )
}

function test_hash_verb() {
  local wrapped_tool=mapping/artifact/tools/map_tools/hash
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: map\.py hash .*map\.py hash: error: the following arguments are required: path" \
      "$wrapper_tool" hash

    [ "$("$wrapper_tool" hash arg1)" == "$wrapped_tool -path=arg1" ]

    assert_failure \
      2 "usage: map\.py .*map\.py: error: unrecognized arguments: arg2" \
      "$wrapper_tool" hash arg1 arg2
  )
}

function test_info_verb() {
  local wrapped_tool=mapping/artifact/tools/map_tools/info
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: map\.py info .*map\.py info: error: the following arguments are required: path" \
      "$wrapper_tool" info

    [ "$("$wrapper_tool" info arg1)" == "$wrapped_tool -path=arg1" ]

    assert_failure \
      2 "usage: map\.py .*map\.py: error: unrecognized arguments: arg2" \
      "$wrapper_tool" info arg1 arg2
  )
}

function test_new_verb() {
  local wrapped_tool=mapping/artifact/tools/map_tools/new
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: map\.py new .*map\.py new: error: the following arguments are required: name, path" \
      "$wrapper_tool" new

    assert_failure \
      2 "usage: map\.py new .*map\.py new: error: the following arguments are required: path" \
      "$wrapper_tool" new arg1

    [ "$("$wrapper_tool" new arg1 arg2)" == \
      "$wrapped_tool -name=arg1 -path=arg2" ]

    assert_failure \
      2 "usage: map\.py .*map\.py: error: unrecognized arguments: arg3" \
      "$wrapper_tool" new arg1 arg2 arg3
  )
}

function test_create_verb() {
  local wrapped_tool=mapping/artifact/tools/map_tools/create
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: map\.py create .*map\.py create: error: the following arguments are required: input_path, output_path, layer_name, layer_path, layer_data_parameter" \
      "$wrapper_tool" create

    assert_failure \
      2 "usage: map\.py create .*map\.py create: error: the following arguments are required: output_path, layer_name, layer_path, layer_data_parameter" \
      "$wrapper_tool" create arg1

    assert_failure \
      2 "usage: map\.py create .*map\.py create: error: the following arguments are required: layer_name, layer_path, layer_data_parameter" \
      "$wrapper_tool" create arg1 arg2

    assert_failure \
      2 "usage: map\.py create .*map\.py create: error: the following arguments are required: layer_path, layer_data_parameter" \
      "$wrapper_tool" create arg1 arg2 arg3

    [ "$("$wrapper_tool" create arg1 arg2 arg3 arg4)" == \
      "$wrapped_tool -input_path=arg1 -output_path=arg2 -layer_name=arg3 -layer_path=arg4" ]

    [ "$("$wrapper_tool" create arg1 arg2 arg3 arg4 arg5)" == \
      "$wrapped_tool -input_path=arg1 -output_path=arg2 -layer_name=arg3 -layer_path=arg4 -layer_data_parameter=arg5" ]

    [ "$("$wrapper_tool" create arg1 arg2 arg3 arg4 arg5 arg6)" == \
      "$wrapped_tool -input_path=arg1 -output_path=arg2 -layer_name=arg3 -layer_path=arg4 -layer_data_parameter=arg5,arg6" ]

    [ "$("$wrapper_tool" create arg1 arg2 arg3 arg4 arg5 arg6 --layer_hash=arg7)" == \
      "$wrapped_tool -input_path=arg1 -output_path=arg2 -layer_name=arg3 -layer_path=arg4 -layer_hash=arg7 -layer_data_parameter=arg5,arg6" ]
  )
}

function test_verify_verb() {
  local wrapped_tool=mapping/artifact/tools/map_tools/verify
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: map\.py verify .*map\.py verify: error: the following arguments are required: path, layers" \
      "$wrapper_tool" verify

    [ "$("$wrapper_tool" verify arg1)" == \
      "$wrapped_tool -path=arg1 -layers=" ]

    [ "$("$wrapper_tool" verify arg1 arg2)" == \
      "$wrapped_tool -path=arg1 -layers=arg2" ]

    [ "$("$wrapper_tool" verify arg1 arg2 arg3)" == \
      "$wrapped_tool -path=arg1 -layers=arg2,arg3" ]
  )
}

function main() {
  test_no_args
  test_unknown_verb
  test_help
  test_hash_verb
  test_info_verb
  test_new_verb
  test_create_verb
  test_verify_verb
}

main
