#!/bin/sh

set -e

# Test the numbered menu fallback of pkgos-choose (used when there is no
# fzf, or when the standard input is not a terminal, like in test runs).

ME=$(basename $0)
SCRIPT=$(cd "$(dirname "$0")/../../../misc" && pwd)/pkgos-choose

OUT=$( { printf '2\n' | "${SCRIPT}" "Pick one" "first" "second" "third" ; } 2>/dev/null)
if [ "${OUT}" != "second" ] ; then
	echo "${ME}: expected 'second', got: ${OUT}"
	exit 1
fi

OUT=$( { printf 'blah\n9\n0\n3\n' | "${SCRIPT}" "Pick one" "first" "second" "third" ; } 2>/dev/null)
if [ "${OUT}" != "third" ] ; then
	echo "${ME}: expected 'third' after invalid input, got: ${OUT}"
	exit 1
fi

OUT=$("${SCRIPT}" "Pick one" "only one" <<EOF
1
EOF
)
if [ "${OUT}" != "only one" ] ; then
	echo "${ME}: expected 'only one', got: ${OUT}"
	exit 1
fi

exit 0
