#!/usr/bin/env bash

setup_file(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _add_server
  _new_app
}

teardown_file(){
  _rm_app
  _rm_server
}

setup(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _ensure_catalogue
}

teardown(){
  _reset_recipe
  _reset_app
  _undeploy_app
  _reset_app # NOTE(d1): _undeploy_app writes version
}

@test "validate app argument" {
  run $ABRA app upgrade
  assert_failure

  run $ABRA app upgrade DOESNTEXIST
  assert_failure
}

@test "retrieve recipe if missing" {
  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE"
  assert_success
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE"

  run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_failure
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
}

# bats test_tags=slow
@test "error if specific version is not an upgrade" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input
  assert_failure
  assert_output --partial 'is not an upgrade'
}

# bats test_tags=slow
@test "no upgrade if on latest version" {
  _deploy_app

  run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "no available upgrades"
}

# bats test_tags=slow
@test "upgrade to latest catalogue published version" {
  appDomain="custom-html.$TEST_SERVER"

  run $ABRA app new custom-html \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "$appDomain"

  oneVersionBack=$(jq -r '."custom-html".versions[-2] | keys[0]' < "$ABRA_DIR/catalogue/recipes.json")
  refute [ -z "$oneVersionBack" ];

  run $ABRA app deploy "$appDomain" "$oneVersionBack" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$oneVersionsBack"

  latestVersion=$(jq -r '."custom-html".versions[-1] | keys[0]' < "$ABRA_DIR/catalogue/recipes.json")
  refute [ -z "$latestVersion" ];

  run $ABRA app upgrade "$appDomain" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$latestVersion"

  run $ABRA app undeploy "$appDomain" --no-input
  assert_success

  run $ABRA app volume remove "$appDomain" --no-input
  assert_success

  run $ABRA app remove "$appDomain" --no-input
  assert_success
  assert_not_exists "$ABRA_DIR/servers/$TEST_SERVER/$appDomain.env"
}

# bats test_tags=slow
@test "upgrade specific version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.2.0+1.21.0'
}

# bats test_tags=slow
@test "force upgrade specific version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.2.0+1.21.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input --no-converge-checks
  assert_failure
  assert_output --partial 'not an upgrade'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.2.0+1.21.0" \
    --no-input --no-converge-checks --force
  assert_success
  assert_output --partial '0.2.0+1.21.0'
}

@test "force upgrade with no available upgrades" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_success

  latestRelease=$(_latest_release)

  run $ABRA app upgrade "$TEST_APP_DOMAIN" \
    --force --no-input --no-converge-checks
  assert_success
  assert_output --partial "$latestRelease"
}

# bats test_tags=slow
@test "upgrade to latest" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$(_latest_release)"
}

# bats test_tags=slow
@test "show single release note" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.1+1.20.2'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$(_latest_release)"
  assert_output --partial 'release notes baz' # 0.2.0+1.21.0
  refute_output --partial 'release notes bar' # 0.1.1+1.20.2
}

# bats test_tags=slow
@test "show single release note for specific version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.1+1.20.2'
  assert_output --partial 'release notes bar' # 0.1.1+1.20.2
  refute_output --partial 'release notes baz' # 0.2.0+1.21.0
}

# bats test_tags=slow
@test "show multiple release notes" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks
  assert_success
  assert_output --partial "$(_latest_release)"
  assert_output --partial 'release notes bar' # 0.1.1+1.20.2
  assert_output --partial 'release notes baz' # 0.2.0+1.21.0
}

# bats test_tags=slow
@test "show multiple release notes for specific version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.2.0+1.21.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.2.0+1.21.0'
  assert_output --partial 'release notes bar' # 0.1.1+1.20.2
  assert_output --partial 'release notes baz' # 0.2.0+1.21.0
}

# bats test_tags=slow
@test "upgrade commit deployment not possible" {
  tagHash=$(_get_tag_hash "0.1.0+1.20.0")
  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$tagHash"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks --chaos
  assert_success
  assert_output --partial "${tagHash:0:8}"

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "0.1.1+1.20.2" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "not a known version"
}

@test "chaos commit upgrade not possible" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks
  assert_success
  assert_output --partial '0.1.0+1.20.0'

  tagHash=$(_get_tag_hash "0.2.0+1.21.0")

  run $ABRA app upgrade "$TEST_APP_DOMAIN" "$tagHash" --no-input --no-converge-checks
  assert_failure
  assert_output --partial "not a known version"
}

# bats test_tags=slow
@test "no chaos version label if no chaos" {
  _deploy_app

  run $ABRA app upgrade "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks
  assert_success

  run $ABRA app labels "$TEST_APP_DOMAIN" --no-input
  assert_success
  refute_output --regexp "coop-cloud.abra-test-recipe.$TEST_SERVER.chaos-version"
}
