#!/usr/bin/env bash
# SPDX-License-Identifier: LicenseRef-Starship-1.0
# Copyright (c) 2023-2026 Robert A. James
# test_check_no_patent.sh -- the tests of scripts/check_no_patent.sh (#191: "the SSRN paper but not the
# patent"): a directory of documents is refused if any file is the patent -- named for it, or a PDF whose
# title says it is -- and passed otherwise. PDFs made here with pdflatex, from a line of TeX each.
#
# Prints PASS:/FAIL: per check and RESULT:; exits 0 only if every check passes.
set -uo pipefail
CHECK="$(cd "$(dirname "$0")" && pwd)/check_no_patent.sh"
T=$(mktemp -d)
trap 'rm -rf "$T"' EXIT
ok=1
pdf() { # pdf
mkdir -p "$1"
printf '\\documentclass{article}\\title{%s}\\usepackage{hyperref}\\hypersetup{pdftitle={%s}}\\begin{document}x\\end{document}\n' "$3" "$3" > "$T/t.tex"
(cd "$T" && pdflatex -interaction=nonstopmode -halt-on-error t.tex > /dev/null 2>&1) && mv "$T/t.pdf" "$1/$2"
}
expect() { # expect "what"
local out rc
out=$(bash "$CHECK" "$2" 2>&1); rc=$?
if { [ "$1" = pass ] && [ $rc -eq 0 ]; } || { [ "$1" = refuse ] && [ $rc -eq 1 ] && grep -q "refused" <<<"$out"; }; then
echo "PASS: $3"; else echo "FAIL: $3 (exit $rc: $out)"; ok=0; fi
}
pdf "$T/good" book.pdf "LithosAnanke"
pdf "$T/good" ssrn.pdf "Steady-State Machines: a paper"
expect pass "$T/good" "the book and the SSRN paper: passed"
pdf "$T/named" patent.pdf "Something"
expect refuse "$T/named" "a file named for the patent: refused"
pdf "$T/titled" vol9.pdf "Steady-State Machine --- Patent Application"
expect refuse "$T/titled" "a PDF titled as the patent, under another name: refused"
mkdir -p "$T/empty"
expect pass "$T/empty" "an empty directory: passed"
expect refuse "$T/missing" "a directory that is not there: refused, not passed by default"
echo "RESULT: $([ $ok = 1 ] && echo PASS || echo FAIL)"
[ $ok = 1 ]