#!/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
}

teardown(){
  _undeploy_app
}

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

  run $ABRA app ps DOESNTEXIST
  assert_failure
}

# bats test_tags=slow
@test "retrieve recipe if missing" {
  _deploy_app

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

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_success

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
}

# bats test_tags=slow
@test "bail if unstaged changes and no --chaos" {
  _deploy_app

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

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_failure
  assert_output --partial 'locally unstaged changes'

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_equal "$(_git_status)" "?? foo"

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

# bats test_tags=slow
@test "do not bail if unstaged changes and --chaos" {
  _deploy_app

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

  run $ABRA app ps --chaos "$TEST_APP_DOMAIN"
  assert_success

  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"
  assert_equal "$(_git_status)" "?? foo"

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

# bats test_tags=slow
@test "ensure recipe up to date if no --offline" {
  _deploy_app

  wantHash=$(_get_n_hash 3)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3
  assert_success

  assert_equal $(_get_current_hash) "$wantHash"

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_success

  assert_equal $(_get_head_hash) $(_get_current_hash)
}

@test "ensure recipe not up to date if --offline" {
  _deploy_app

  _ensure_env_version "0.1.0+1.20.0"
  latestRelease=$(_latest_release)

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease"
  assert_success

  run $ABRA app ps --offline "$TEST_APP_DOMAIN"
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l
  refute_output --partial "$latestRelease"
}

@test "error if not deployed" {
  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_failure
  assert_output --partial 'is not deployed'
}

# bats test_tags=slow
@test "show ps report" {
  _deploy_app

  _ensure_env_version "$(_latest_release)"

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_success
  assert_output --partial 'app'
  assert_output --partial 'healthy'
  assert_output --partial $(_latest_release)
  assert_output --partial 'false' # not a chaos deploy
}

# bats test_tags=slow
@test "show ps report with chaos deploy" {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --chaos
  assert_success

  headHash=$(_get_head_hash)

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_success
  assert_output --partial "$latestRelease"
  assert_output --partial "${headHash:0:8}" # is a chaos deploy
}

# bats test_tags=slow
@test "ensure live chaos commit is shown" {
  headHash=$(_get_head_hash)

  run $ABRA app deploy "$TEST_APP_DOMAIN" "0f5a0570" --no-input 
  assert_success

  run $ABRA app ps "$TEST_APP_DOMAIN"
  assert_success
  assert_output --partial "0f5a0570" # is not latest HEAD
  refute_output --partial "${headHash:0:8}"
}
