#!/usr/bin/env bash

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

teardown_file(){
  _undeploy_app
  _rm_app
  _rm_server
}

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

teardown(){
  _undeploy_app
}

@test "list without status" {
  run $ABRA app ls
  assert_success
  assert_output --partial "$TEST_SERVER"
  assert_output --partial "$TEST_APP_DOMAIN"
}

# bats test_tags=slow
@test "list with status" {
  run $ABRA app ls --status
  assert_success
  assert_output --partial "$TEST_SERVER"
  assert_output --partial "$TEST_APP_DOMAIN"
  assert_output --partial "unknown"

  _deploy_app

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

@test "filter by server" {
  run mkdir -p "$ABRA_DIR/servers/foo.com"
  assert_success
  assert_exists "$ABRA_DIR/servers/foo.com"

  run cp \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" \
    "$ABRA_DIR/servers/foo.com/app.foo.com.env"
  assert_exists "$ABRA_DIR/servers/foo.com/app.foo.com.env"

  run $ABRA app ls
  assert_success
  assert_output --partial "$TEST_SERVER"
  assert_output --partial "foo.com"

  run rm -rf "$ABRA_DIR/servers/foo.com"
  assert_success
  assert_not_exists "$ABRA_DIR/servers/foo.com"
}

@test "filter by recipe" {
  run mkdir -p "$ABRA_DIR/servers/foo.com"
  assert_success
  assert_exists "$ABRA_DIR/servers/foo.com"

  run cp \
    "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" \
    "$ABRA_DIR/servers/foo.com/app.foo.com.env"
  assert_exists "$ABRA_DIR/servers/foo.com/app.foo.com.env"

  run sed -i "s/TYPE=$TEST_RECIPE/TYPE=foo-recipe/g" "$ABRA_DIR/servers/foo.com/app.foo.com.env"
  assert grep -q "TYPE=foo-recipe" "$ABRA_DIR/servers/foo.com/app.foo.com.env"

  run $ABRA app ls
  assert_success
  assert_output --partial "$TEST_RECIPE"
  assert_output --partial "foo-recipe"

  run $ABRA app ls --recipe foo-recipe
  assert_success
  refute_output --partial "$TEST_RECIPE"
  assert_output --partial "foo-recipe"

  run rm -rf "$ABRA_DIR/servers/foo.com"
  assert_success
  assert_not_exists "$ABRA_DIR/servers/foo.com"
}

@test "output is machine readable" {
  run $ABRA app ls --machine

  expectedOutput='{"'
  expectedOutput+="$TEST_SERVER"
  expectedOutput+='":{"apps":'

  assert_output --partial "$expectedOutput"
}

# bats test_tags=slow
@test "list with status fetches recipe" {
  _deploy_app

  run $ABRA app ls --status
  assert_success

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

  run $ABRA app ls --status
  assert_success
}

# bats test_tags=slow
@test "list with chaos version" {
  run bash -c "echo foo >> $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 $ABRA app ls --status
  assert_success
  assert_output --partial "+U"
}
