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

source mapping/artifact/tools/test_utilities.sh

wrapper_tool=mapping/artifact/tools/revision

function setup() {
  make_test_case_environment "$wrapper_tool"
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    assert_failure \
      2 "usage: revision\.py create .*revision\.py create: error: the following arguments are required: map_path, revision_path" \
      "$wrapper_tool" create

    assert_failure \
      2 "usage: revision\.py create .*revision\.py create: error: the following arguments are required: revision_path" \
      "$wrapper_tool" create arg1

    [ "$("$wrapper_tool" create arg1 arg2)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2" ]

    assert_failure \
      2 "usage: revision\.py .*revision\.py: error: unrecognized arguments: arg3" \
      "$wrapper_tool" create arg1 arg2 arg3

    [ "$("$wrapper_tool" create arg1 arg2 --parents arg3)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2 -parents=arg3" ]

    [ "$("$wrapper_tool" create arg1 arg2 --parents arg3 arg4)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2 -parents=arg3,arg4" ]

    [ "$("$wrapper_tool" create arg1 arg2 --parents arg3 arg4 --author_name=arg5)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2 -parents=arg3,arg4 -author_name=arg5" ]

    [ "$("$wrapper_tool" create arg1 arg2 --parents arg3 arg4 --author_name=arg5 --author_email=arg6)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2 -parents=arg3,arg4 -author_name=arg5 -author_email=arg6" ]

    [ "$("$wrapper_tool" create arg1 arg2 --parents arg3 arg4 --author_name=arg5 --author_email=arg6 --timestamp_ns=arg7)" == \
      "$wrapped_tool -map_path=arg1 -revision_path=arg2 -parents=arg3,arg4 -author_name=arg5 -author_email=arg6 -timestamp_ns=arg7" ]
  )
}

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

main
