#!/bin/sh -e

pkg=rachis-cli

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cd "${AUTOPKGTEST_TMP}"

export HOME=${HOME:-$PWD}

# Run the upstream test suite once against the installed package before
# exercising the CLI.  The rachis CLI is installed at this point, so the
# tests that invoke `rachis` as a subprocess run against the real
# executable (the same tests pass during the package build as well, where
# pybuild puts the generated scripts directory on PATH for dh_auto_test;
# the debian/patches guard only covers source-tree pytest runs without the
# CLI).  RACHISTEST limits the loaded plugins to the dummy plugins from the
# rachis core package, exactly like the package build.
RACHISTEST=1 python3 -m pytest -q -p no:cacheprovider \
	/usr/lib/python3/dist-packages/rachis_cli/tests

QZA_SAMPLE="/usr/lib/python3/dist-packages/q2_sample_classifier/tests/data/vaw.qza"
if [ ! -r "$QZA_SAMPLE" ]
then
	cat >&2 <<-ERR
	error: could not find sample data: $QZA_SAMPLE
	       Please install q2-sample-classifier
	ERR
	exit 1
fi

# TODO: same tests but with recommended dependencies!
#       This is important since there is a huge change of behavior, and
#       extensions may potentially break the rachis command as I am seeing with
#       python3-sklearn at the moment.
set -v
# 0. the basics
	rachis --version

# 1. help messages
	rachis --help
	rachis info --help
	rachis tools --help
	rachis dev --help
	rachis tools citations --help
	rachis tools export --help
	rachis tools extract --help
	rachis tools import --help
	rachis tools inspect-metadata --help
	rachis tools peek --help
	rachis tools validate --help
	rachis tools view --help
	rachis dev export-default-theme --help
	rachis dev import-theme --help
	rachis dev refresh-cache --help
	rachis dev reset-theme --help

# 2. info command
	rachis info

# 3. tools commands
	# click >= 8.3 raises NoArgsIsHelpError (exit 2) for a bare group
	# invocation, so ask for help explicitly instead of "rachis tools".
	rachis tools --help

	rachis tools citations "$QZA_SAMPLE" > citations.bib
	file citations.bib | grep -qi 'bibtex'

	rachis tools export --input-path "$QZA_SAMPLE" --output-path .
	file feature-table.biom | grep -qi 'Hierarchical Data Format'

	rachis tools extract --input-path "$QZA_SAMPLE"
	cd 861451cc-c250-43fc-818a-ddc2159dbe25
		md5sum --check checksums.md5
	cd -

	rachis tools inspect-metadata "$QZA_SAMPLE" --tsv > metadata.tsv
	rachis tools inspect-metadata "$QZA_SAMPLE" --no-tsv

	rachis tools peek "$QZA_SAMPLE"

	rachis tools import \
		--type 'FeatureTable[Frequency]' \
		--input-path '861451cc-c250-43fc-818a-ddc2159dbe25/data' \
		--output-path artifact.qza
	file artifact.qza | grep -qi 'zip archive data'

	rachis tools validate artifact.qza

	# TODO test rachis tools view, missing Vizualization data at the moment,
	# most likely available under the form of *.qvz files.

# 4. dev commands
	# see above: use --help instead of a bare group invocation
	rachis dev --help
	rachis dev export-default-theme --output-path file.theme
	diff file.theme - <<-END
	[option]
	fg = bright_blue

	[type]
	fg = green

	[default_arg]
	fg = magenta

	[command]
	fg = bright_blue

	[emphasis]
	underline = True

	[problem]
	fg = yellow

	[warning]
	fg = yellow
	bold = True

	[error]
	fg = red
	bold = True

	[required]
	underline = True

	[success]
	fg = green

	END
	rachis dev import-theme --theme file.theme
	rachis dev refresh-cache
	yes | rachis dev reset-theme

echo "\nRESULTS: OK\n"
