#!/usr/bin/env bash

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

teardown_file(){
  _rm_app
  _rm_server
  _reset_recipe
}

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

teardown(){
  _reset_recipe
  _reset_app
  _undeploy_app
  _reset_tags

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
}

@test "first deploy with empty env version" {
  latestRelease=$(_latest_release)

  _wipe_env_version

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

  # current deployment
  assert_output --regexp 'VERSION.*N/A'
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.* ' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*N/A'
  assert_output --regexp 'NEW VERSION.*' + "${latestRelease}"

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "first deploy to latest version" {
  latestRelease=$(_latest_release)

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

  # current deployment
  assert_output --regexp 'VERSION.*N/A'
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.* ' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${latestRelease}"
  assert_output --regexp 'NEW VERSION.*' + "${latestRelease}"

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

# bats test_tags=slow
@test "deploy, re-deploy, choose env version" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" \
    --no-input --no-converge-checks
  assert_success

  _undeploy_app

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

  # current deployment
  assert_output --regexp 'VERSION.*N/A'
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.*' + "0.1.1+1.20.2"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "0.1.1+1.20.2"
  assert_output --regexp 'NEW VERSION.*' + "0.1.1+1.20.2"

  run grep -q "TYPE=$TEST_RECIPE:0.1.1+1.20.2" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "deploy, re-deploy, ignore env version" {
  latestRelease=$(_latest_release)

  run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.1+1.20.2" \
    --no-input --no-converge-checks
  assert_success

  _undeploy_app

  run $ABRA app deploy "$TEST_APP_DOMAIN" \
    --no-input --no-converge-checks --ignore-env-version
  assert_success

  # current deployment
  assert_output --regexp 'VERSION.*N/A'
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "0.1.1+1.20.2"
  assert_output --regexp 'NEW VERSION.*' + "${latestRelease}"

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "deploy then chaos deploy" {
  headHash=$(_get_head_hash)
  latestRelease=$(_latest_release)

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

  run bash -c 'echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo"'
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

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

  # current deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}+U"

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${latestRelease}"
  assert_output --regexp 'NEW VERSION.*' + "${headHash:0:8}+U"

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run grep -q "TYPE=$TEST_RECIPE:${headHash:0:8}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "chaos deploy then force deploy" {
  headHash=$(_get_head_hash)
  latestRelease=$(_latest_release)

  run bash -c 'echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo"'
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

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

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

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

  # current deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}+U"

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${headHash:0:8}+U"
  assert_output --regexp 'NEW VERSION.*' + "${latestRelease}"

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "deploy then force chaos commit deploy" {
  headHash=$(_get_head_hash)
  latestRelease=$(_latest_release)

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

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run $ABRA app deploy "$TEST_APP_DOMAIN" "${headHash:0:8}" \
    --no-input --no-converge-checks --force
  assert_success

  # current deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}"

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${latestRelease}"
  assert_output --regexp 'NEW VERSION.*' + "${headHash:0:8}"

  run grep -q "TYPE=$TEST_RECIPE:${headHash:0:8}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}

@test "chaos deploy then chaos deploy with unstaged" {
  headHash=$(_get_head_hash)
  latestRelease=$(_latest_release)

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

  # current deployment
  assert_output --regexp 'VERSION.*N/A'
  assert_output --regexp 'CHAOS.*false'

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}"

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${latestRelease}"
  assert_output --regexp 'NEW VERSION.*' + "${headHash:0:8}"

  run bash -c 'echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo"'
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

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

  # current deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}"

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}+U"

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${latestRelease}"
  assert_output --regexp 'NEW VERSION.*' + "${headHash:0:8}+U"

  run grep -q "TYPE=$TEST_RECIPE:${headHash:0:8}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success

  run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
}

@test "hash deploy then force deploy" {
  headHash=$(_get_head_hash)
  latestRelease=$(_latest_release)

  run $ABRA app deploy "$TEST_APP_DOMAIN" "${headHash:0:8}" \
    --no-input --no-converge-checks
  assert_success

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

  # current deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*' + "${headHash:0:8}"

  # new deployment
  assert_output --regexp 'VERSION.*' + "${latestRelease}"
  assert_output --regexp 'CHAOS.*false'

  # env version
  assert_output --regexp 'CURRENT VERSION.*' + "${headHash:0:8}"
  assert_output --regexp 'NEW VERSION.*' + "${latestRelease}"

  run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
}
