#!/bin/sh
# pic projects: the projects held in ~/.pic/pic.sqlite (picu_project).
set -eu
PIC=${PIC:?set PIC to the pic binary}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-projects.XXXXXX")
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
mkdir "$T/home"
export HOME="$T/home"
out=$("$PIC" projects) || fail "projects: exit status with none"
[ -z "$out" ] || fail "projects: not empty at first: $out"
printf 'PASS: no projects lists nothing\n'
sqlite3 "$HOME/.pic/pic.sqlite" "INSERT INTO picu_project(name, prefix, remote, cred, kind, state, created_ms)
VALUES('zeta', 'fslrepo_zeta_', 'https://example.org/zeta', 'ZETA_PW', 'fossil', 'ready', 2),
('alpha', 'fslrepo_alpha_', 'http://127.0.0.1:8080/alpha', NULL, 'fossil', 'cloning', 1)"
printf 'alpha\tfossil\tcloning\thttp://127.0.0.1:8080/alpha\nzeta\tfossil\tready\thttps://example.org/zeta\n' > "$T/want"
"$PIC" projects > "$T/got" || fail "projects: exit status"
cmp -s "$T/want" "$T/got" || fail "projects: listing: $(cat "$T/got")"
"$PIC" projects extra 2> /dev/null && fail "projects: an extra argument was accepted"
printf 'PASS: projects are listed by name with kind, state and remote\n'