#!/usr/bin/env bash

test_description='cli'

. lib/test-lib.sh

################################################################

test_expect_code 5 'dump non-existant db' \
    'impass dump'

test_expect_success 'add first entry' \
    'impass add foo@bar'

test_expect_success 'add second entry' \
    "impass add 'baz asdf Dokw okb 32438uoijdf'"

test_expect_code 2 'add existing context' \
    'impass add foo@bar'

test_begin_subtest "dump all entries"
impass dump 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "baz asdf Dokw okb 32438uoijdf": {
    FOO
  },
  "foo@bar": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest "dump search 0"
impass dump foo 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "foo@bar": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest "dump search 1"
impass dump asdf 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "baz asdf Dokw okb 32438uoijdf": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest "dump search 2"
impass dump ba 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "baz asdf Dokw okb 32438uoijdf": {
    FOO
  },
  "foo@bar": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_code 2 'add existing context' 'impass add foo@bar'

test_expect_code 2 'replace non-existing context' \
    'impass replace aaaa'
# FIXME: add replacement test

test_expect_code 2 'update non-existing context' \
    'impass update aaaa'
test_begin_subtest "update entry"
impass update foo@bar foo@example
impass dump foo 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "foo@example": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_code 2 'remove non-existant entry' 'impass remove aaaa'
test_begin_subtest "remove entry"
echo yes | impass remove foo@example
impass dump 2>&1 | sed 's/"date": ".*"/FOO/g' >OUTPUT
cat <<EOF >EXPECTED
{
  "baz asdf Dokw okb 32438uoijdf": {
    FOO
  }
}
EOF
test_expect_equal_file OUTPUT EXPECTED

################################################################

test_done
