#!/usr/bin/env bash

_new_app() {
  run $ABRA app new "$TEST_RECIPE" \
    --no-input \
    --server "$TEST_SERVER" \
    --domain "$TEST_APP_DOMAIN" \
    --secrets
  assert_success
  assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
}

_deploy_app() {
  run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run $ABRA app ls --server "$TEST_SERVER" --status
  assert_success
  assert_output --partial "$TEST_APP_DOMAIN"
  assert_output --partial 'deployed'
}

_undeploy_app() {
  run $ABRA app undeploy "$TEST_APP_DOMAIN" --no-input
  assert_success

  run $ABRA app ls --server "$TEST_SERVER" --status
  assert_success
  assert_output --partial "$TEST_APP_DOMAIN"
  assert_output --partial 'unknown'
}

_rm_app() {
  # NOTE(d1): not asserting outcomes on teardown here since some might fail
  # depending on what the test created. all commands run through anyway
  if [[ -f "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" ]]; then
    run $ABRA app undeploy "$TEST_APP_DOMAIN" --no-input
    run $ABRA app secret remove "$TEST_APP_DOMAIN" --all --no-input
    run $ABRA app volume remove "$TEST_APP_DOMAIN" --no-input
    run $ABRA app remove "$TEST_APP_DOMAIN" --no-input
  fi
}

_reset_app(){
  run rm -rf "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"
  assert_success
  assert_not_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"

  _new_app
}
