#!/usr/bin/env bash

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

teardown_file(){
  _rm_server
}

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

teardown(){
  if [[ -d "$ABRA_DIR/servers/nowhere.com" ]]; then
    run rm -rf "$ABRA_DIR/servers/nowhere.com"
    assert_success

    run docker context rm nowhere.com
    assert_success
  fi

  if [[ -d "$ABRA_DIR/servers/default" ]]; then
    _rm_default_server
  fi
}

@test "list server" {
  run "$ABRA" server ls
  assert_success
  assert_output --partial "$TEST_SERVER"
}

@test "show 'local' when --local server created" {
  run $ABRA server add --local
  assert_success
  assert_exists "$ABRA_DIR/servers/default"

  run "$ABRA" server ls
  assert_success
  assert_output --partial 'default'
  assert_output --partial 'local'
}

@test "list correct details" {
  if [ "$TEST_SERVER" = "default" ]; then
    skip "cannot add 'default' server (local)"
  fi

  run "$ABRA" server ls
  assert_success
  assert_output --partial "$TEST_SERVER"
  assert_output --partial "NAME"
  assert_output --partial "HOST"
  refute_output --partial "USER"
  refute_output --partial "PORT"
}

@test "machine readable output" {
  if [ ! "$TEST_SERVER" = "default" ]; then
    skip "can only diff output against 'default' server (local)"
  fi

  output=$("$ABRA" server ls --machine)
  run diff \
    <(jq -S "." <(echo "$output")) \
    <(jq -S "." <(echo '[{"host":"local","name":"default"}]'))
  assert_success
}
