#!/usr/bin/env bash

test_description='basic command line usage.'

. ./test-lib.sh

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

# FIXME: update with source already in db
# FIXME: add with prompting

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

test_expect_code 1 'fail search without database' \
    'xapers search tag:foo'

test_expect_code 1 'fail to add without file or source' \
    'xapers add --tags=new'

test_expect_success 'add file without source' \
    'xapers add \
    --file=$DOC_DIR/1.pdf \
    --tags=new,foo'

test_expect_success 'new docdir exists' \
    'test -d $XAPERS_ROOT/0000000001'

test_begin_subtest 'tag file exists'
cat <<EOF >EXPECTED
foo
new
EOF
test_expect_equal_file "$XAPERS_ROOT"/0000000001/tags EXPECTED

test_expect_code 1 'fail to add non-bibtex file as source' \
    'xapers add \
     --source=$DOC_DIR/1.pdf'

test_expect_success 'add bib without file' \
    'xapers add \
    --source=$DOC_DIR/2.bib \
    --tags=new,bar'

test_begin_subtest 'bib file exists and is correct'
cat <<EOF >EXPECTED
@article{
    Good_Bad_Up_Down_Left_Right_et_al._2012,
    author = "Good, Bob and Bad, Sam and Up, Steve and Down, Joseph and Left, Aidan and Right, Kate and et al.",
    publisher = "Optical Society of America",
    doi = "10.9999/FOO.1",
    title = "Multicolor cavity sadness",
    url = "http://dx.doi.org/10.9999/FOO.1",
    journal = "Journal of the Color Feelings",
    number = "10",
    month = "Sep",
    volume = "29",
    year = "2012",
    pages = "2092"
}

EOF
test_expect_equal_file "$XAPERS_ROOT"/0000000002/bibtex EXPECTED

test_expect_success 'add with file and bib' \
    'xapers add \
    --file=$DOC_DIR/3.pdf \
    --source=$DOC_DIR/3.bib \
    --tags=qux'

test_expect_code 1 'fail to add non-existant file' \
    'xapers add --file=foo.pdf'

test_expect_code 1 'fail to add non-existant source' \
    'xapers add --source=foo.bib'

test_expect_code 1 'fail to add non-bibtex file as source' \
    'xapers add --source=$DOC_DIR/3.pdf'

test_expect_code 1 'fail to add source doc already associated with different doc' \
    'xapers add --source=doi:10.9999/FOO.1 id:1'

test_begin_subtest 'update doc with bib'
xapers add --source=$DOC_DIR/1.bib id:1
xapers search id:1 >OUTPUT
cat <<EOF >EXPECTED
id:1 [arxiv:1234] {arxiv:1234} (foo new) "Creation of the Universe"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'update with different bib overwrites previous'
xapers add --source=$DOC_DIR/1a.bib id:1
xapers search id:1 >OUTPUT
cat <<EOF >EXPECTED
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'previous source no longer in db'
xapers search arxiv:1234 >OUTPUT
cat <<EOF >EXPECTED
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'update doc with file'
xapers add --file=$DOC_DIR/2\ file.pdf doi:10.9999/FOO.1
xapers search pellentesque >OUTPUT
cat <<EOF >EXPECTED
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_success 'add bib without file' \
    'xapers add \
    --source=$DOC_DIR/4.bib \
    --tags=new'

test_expect_success 'add file without source' \
    'xapers add \
    --file=$DOC_DIR/5.pdf \
    --tags=new'

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

test_begin_subtest 'count all'
output=`xapers count`
test_expect_equal "$output" 5

test_begin_subtest 'count all (*)'
output=`xapers count '*'`
test_expect_equal "$output" 5

test_begin_subtest 'count search'
output=`xapers count tag:new`
test_expect_equal "$output" 4

test_expect_code 1 'fail search without query' \
    'xapers search'

test_begin_subtest 'search all'
xapers search '*' >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search all pipe'
xapers search '*' | cat >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search all --limit'
xapers search --limit=3 '*' >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search text'
xapers search --output=summary lorem >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix title:'
xapers search title:cavity >OUTPUT
cat <<EOF >EXPECTED
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix author:'
xapers search author:cruise >OUTPUT
cat <<EOF >EXPECTED
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix id:'
xapers search id:3 >OUTPUT
cat <<EOF >EXPECTED
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix <source>:'
xapers search doi:10.9999/FOO.1 >OUTPUT
cat <<EOF >EXPECTED
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix bib:'
test_subtest_known_broken
xapers search key:Good_Bad_Up_Down_Left_Right_et_al._2012 >OUTPUT
cat <<EOF >EXPECTED
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search different prefix bib:'
xapers search key:fake:1234 >OUTPUT
cat <<EOF >EXPECTED
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search prefix tag:'
xapers search tag:new >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=tags'
xapers search --output=tags tag:foo | sort >OUTPUT
cat <<EOF >EXPECTED
foo
new
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=tags all'
xapers search --output=tags '*' | sort >OUTPUT
cat <<EOF >EXPECTED
bar
foo
new
qux
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=sources'
xapers search --output=sources tag:bar >OUTPUT
cat <<EOF >EXPECTED
doi:10.9999/FOO.1
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=sources all'
xapers search --output=sources '*' >OUTPUT
cat <<EOF >EXPECTED
arxiv:1235
doi:10.9999/FOO.1
doi:10.9999/FOO.2
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=keys'
xapers search --output=keys tag:bar >OUTPUT
cat <<EOF >EXPECTED
Good_Bad_Up_Down_Left_Right_et_al._2012
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=keys all'
xapers search --output=keys '*' >OUTPUT
cat <<EOF >EXPECTED
30929234
Good_Bad_Up_Down_Left_Right_et_al._2012
arxiv:1235
fake:1234
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=files'
xapers search --output=files '*' | sed "s|$XAPERS_ROOT|XAPERS_ROOT|" >OUTPUT
cat <<EOF >EXPECTED
XAPERS_ROOT/0000000005/5.pdf
XAPERS_ROOT/0000000003/3.pdf
XAPERS_ROOT/0000000002/2 file.pdf
XAPERS_ROOT/0000000001/1.pdf
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'search --output=bibtex single'
xapers search --output=bibtex tag:foo | sed "s|$XAPERS_ROOT|XAPERS_ROOT|" >OUTPUT
cat <<EOF >EXPECTED
@article{
    arxiv:1235,
    author = "Dole, Bob and Cruise, Tim",
    title = "Creation of the γ-verses",
    eprint = "1235",
    file = ":XAPERS_ROOT/0000000001/1.pdf:pdf",
    year = "2012"
}

EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'bibtex multiple'
xapers bibtex tag:new | sed "s|$XAPERS_ROOT|XAPERS_ROOT|" >OUTPUT
cat <<EOF >EXPECTED
@article{
    30929234,
    author = "Me and You and We Know, Everyone",
    url = "http://dx.doi.org/10.9999/FOO.2",
    title = "The Circle and the Square: Forbidden Love",
    journal = "Shaply Letters",
    doi = "10.9999/FOO.2",
    year = "1869"
}

@article{
    Good_Bad_Up_Down_Left_Right_et_al._2012,
    author = "Good, Bob and Bad, Sam and Up, Steve and Down, Joseph and Left, Aidan and Right, Kate and et al.",
    publisher = "Optical Society of America",
    doi = "10.9999/FOO.1",
    title = "Multicolor cavity sadness",
    url = "http://dx.doi.org/10.9999/FOO.1",
    journal = "Journal of the Color Feelings",
    number = "10",
    month = "Sep",
    volume = "29",
    file = ":XAPERS_ROOT/0000000002/2 file.pdf:pdf",
    year = "2012",
    pages = "2092"
}

@article{
    arxiv:1235,
    author = "Dole, Bob and Cruise, Tim",
    title = "Creation of the γ-verses",
    eprint = "1235",
    file = ":XAPERS_ROOT/0000000001/1.pdf:pdf",
    year = "2012"
}

EOF
test_expect_equal_file OUTPUT EXPECTED

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

test_expect_code 1 'fail tag without operation' \
    'xapers tag tag:foo'

test_expect_code 1 'fail tag without search' \
    'xapers tag +baz'

test_begin_subtest 'add tag'
xapers tag +baz -- tag:foo
xapers search tag:baz >OUTPUT
cat <<EOF >EXPECTED
id:1 [arxiv:1235] {arxiv:1235} (baz foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'check tags added to tag file'
cat <<EOF >EXPECTED
baz
foo
new
EOF
test_expect_equal_file "$XAPERS_ROOT"/0000000001/tags EXPECTED

test_begin_subtest 'remove tag'
xapers tag -baz -- tag:baz
xapers search tag:baz >OUTPUT
cat <<EOF >EXPECTED
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'check tags removed from tag file'
cat <<EOF >EXPECTED
foo
new
EOF
test_expect_equal_file "$XAPERS_ROOT"/0000000001/tags EXPECTED

test_begin_subtest 'add and remove tags'
xapers tag -foo +zzz -- tag:foo and tag:zzz
xapers search tag:foo and tag:zzz >OUTPUT
cat <<EOF >EXPECTED
EOF
test_expect_equal_file OUTPUT EXPECTED

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

rm -rf "$TMP_DIRECTORY"/export

test_expect_code 1 'fail export no query' \
    'xapers export $TMP_DIRECTORY/export'

test_begin_subtest 'export all'
xapers export "$TMP_DIRECTORY"/export '*'
find "$TMP_DIRECTORY"/export -mindepth 1 | sed "s|$TMP_DIRECTORY|TMP_DIRECTORY|" | sort >OUTPUT
cat <<EOF | sort >EXPECTED
TMP_DIRECTORY/export/5.pdf
TMP_DIRECTORY/export/When_the_liver_meats_the_pavement.pdf
TMP_DIRECTORY/export/Multicolor_cavity_sadness.pdf
TMP_DIRECTORY/export/Creation_of_the_γ-verses.pdf
EOF
test_expect_equal_file OUTPUT EXPECTED

rm -rf "$TMP_DIRECTORY"/export

test_begin_subtest 'export query'
xapers export "$TMP_DIRECTORY"/export lorem
find "$TMP_DIRECTORY"/export -mindepth 1 | sed "s|$TMP_DIRECTORY|TMP_DIRECTORY|" | sort >OUTPUT
cat <<EOF | sort >EXPECTED
TMP_DIRECTORY/export/5.pdf
TMP_DIRECTORY/export/Creation_of_the_γ-verses.pdf
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_success 'restore to existing db' \
    "xapers restore"

test_begin_subtest 'database intact after restore'
xapers search '*' >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_code 1 'fail delete with no query' \
    "xapers delete"

# purge the db from the root
rm -rf $XAPERS_ROOT/.xapers

test_expect_success 'restore purged db' \
    "xapers restore"

test_begin_subtest 'database intact after restore'
xapers search '*' >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
id:2 [doi:10.9999/FOO.1] {Good_Bad_Up_Down_Left_Right_et_al._2012} (bar new) "Multicolor cavity sadness"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'delete single document noprompt'
xapers delete --noprompt id:2
xapers search '*' >OUTPUT
cat <<EOF >EXPECTED
id:5 [] {} (new) ""
id:4 [doi:10.9999/FOO.2] {30929234} (new) "The Circle and the Square: Forbidden Love"
id:3 [] {fake:1234} (qux) "When the liver meats the pavement"
id:1 [arxiv:1235] {arxiv:1235} (foo new) "Creation of the γ-verses"
EOF
test_expect_equal_file OUTPUT EXPECTED

test_begin_subtest 'delete document search w/ prompt'
echo 'yes' | xapers delete lorem
xapers search lorem >OUTPUT
cat <<EOF >EXPECTED
EOF
test_expect_equal_file OUTPUT EXPECTED

test_expect_code 1 'check for deleted docdirs' "
test -d $XAPERS_ROOT/0000000001 \
|| test -d $XAPERS_ROOT/0000000002 \
|| test -d $XAPERS_ROOT/0000000005
"

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

test_done
