#!/bin/sh

set -eu

ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
SCRIPT_UNDER_TEST="$ROOT_DIR/bin/restart_java_backend.sh"

fail() {
    printf 'FAIL: %s\n' "$1" >&2
    exit 1
}

assert_grep() {
    pattern="$1"
    target="$2"
    if ! grep -Eq -- "$pattern" "$target"; then
        fail "expected pattern [$pattern] in $target"
    fi
}

assert_not_grep() {
    pattern="$1"
    target="$2"
    if grep -Eq -- "$pattern" "$target"; then
        fail "did not expect pattern [$pattern] in $target"
    fi
}

test_script_contract() {
    assert_grep 'ps -ef' "$SCRIPT_UNDER_TEST"
    assert_not_grep 'pgrep' "$SCRIPT_UNDER_TEST"
    assert_grep 'APP_MARKER=' "$SCRIPT_UNDER_TEST"
    assert_grep 'status_backend\(\)' "$SCRIPT_UNDER_TEST"
}

main() {
    [ -f "$SCRIPT_UNDER_TEST" ] || fail "script under test not found: $SCRIPT_UNDER_TEST"
    test_script_contract
    printf 'PASS: restart_java_backend tests\n'
}

main "$@"
