#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
bootstrap="$repo_root/skills/using-lwc/scripts/bootstrap.sh"
test_root="$(mktemp -d "${TMPDIR:-/tmp}/using-lwc-bootstrap-test.XXXXXX")"
trap 'rm -rf "$test_root"' EXIT

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

home="$test_root/home"
outer="$home/work"
project="$outer/project"
nested="$project/src"
outside="$home/outside"
mock_bin="$test_root/bin"
runtime_tmp="$test_root/runtime-tmp"
mkdir -p "$outer/.lwc" "$nested" "$outside" "$mock_bin" "$runtime_tmp"
: > "$outer/.lwc/wiki.db"

cat > "$mock_bin/lwc" <<'MOCK_LWC'
#!/bin/sh
case "$*" in
  "--version")
    printf 'lwc %s\n' "${MOCK_LWC_VERSION:-0.6.0}"
    ;;
  "init --help")
    printf '%s\n' '--scope'
    ;;
  "--help")
    printf '%s\n' 'Set LWC_PROJECT_ROOT to bound project discovery.'
    [ "${MOCK_NO_CHANGESET_SELECTOR:-0}" = 0 ] && printf '%s\n' '--changeset'
    ;;
  "checkpoint --help"|"source add-manifest --help"|"source status --help"|"source diff --help")
    ;;
  "changeset --help")
    [ "${MOCK_NO_CHANGESET:-0}" = 0 ] || exit 1
    printf '%s\n' '--changeset'
    ;;
  "page put --help")
    printf '%s\n' '--provenance'
    ;;
  "--scope global init")
    mkdir -p "$HOME/.lwc"
    : > "$HOME/.lwc/wiki.db"
    ;;
  "--scope global purpose set "*|"--scope global schema set "*)
    ;;
  *)
    printf 'unexpected lwc call: %s\n' "$*" >&2
    exit 2
    ;;
esac
MOCK_LWC
chmod +x "$mock_bin/lwc"

project_root="$(cd "$project" && pwd -P)"
output="$(
  cd "$nested"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
)"

case "$output" in
  *"\"project_wiki\":\"\""*) ;;
  *) fail "bootstrap reused an ancestor Wiki outside LWC_PROJECT_ROOT: $output" ;;
esac
case "$output" in
  *"\"project_root\":\"$project_root\""*) ;;
  *) fail "bootstrap did not keep the configured project root: $output" ;;
esac

mkdir -p "$project/.lwc" "$nested/.lwc"
: > "$project/.lwc/wiki.db"
: > "$nested/.lwc/wiki.db"
conflict="$(
  cd "$nested"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
)"
case "$conflict" in
  *"\"project_wiki\":\"\""*"\"scope_conflict\":true"*) ;;
  *) fail "bootstrap selected one of multiple in-scope Wikis: $conflict" ;;
esac

if (
  cd "$outside"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
) >"$test_root/outside.out" 2>"$test_root/outside.err"; then
  fail "bootstrap accepted a working directory outside LWC_PROJECT_ROOT"
fi

if (
  cd "$home"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$home" \
    sh "$bootstrap"
) >"$test_root/home.out" 2>"$test_root/home.err"; then
  fail "bootstrap accepted the home directory as LWC_PROJECT_ROOT"
fi

if (
  cd "$project"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    MOCK_LWC_VERSION=0.5.0 \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
) >"$test_root/legacy.out" 2>"$test_root/legacy.err"; then
  fail "bootstrap accepted lwc v0.5.0 without bounded source diff support"
fi

if (
  cd "$project"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    MOCK_NO_CHANGESET=1 \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
) >"$test_root/no-changeset.out" 2>"$test_root/no-changeset.err"; then
  fail "bootstrap accepted an lwc without atomic changeset support"
fi

if (
  cd "$project"
  HOME="$home" \
    PATH="$mock_bin:$PATH" \
    TMPDIR="$runtime_tmp" \
    MOCK_NO_CHANGESET_SELECTOR=1 \
    LWC_AUTO_INSTALL=0 \
    LWC_PROJECT_ROOT="$project" \
    sh "$bootstrap"
) >"$test_root/no-selector.out" 2>"$test_root/no-selector.err"; then
  fail "bootstrap accepted an lwc without the global changeset selector"
fi

path_home="$test_root/path-home"
mkdir -p "$path_home/.local/bin"
cp "$mock_bin/lwc" "$path_home/.local/bin/lwc"
if (
  cd "$project"
  HOME="$path_home" \
    PATH="/usr/bin:/bin" \
    TMPDIR="$runtime_tmp" \
    LWC_AUTO_INSTALL=0 \
    sh "$bootstrap"
) >"$test_root/path.out" 2>"$test_root/path.err"; then
  fail "bootstrap accepted an lwc binary that was not globally callable on PATH"
fi
grep -Fq 'lwc is not on PATH' "$test_root/path.err" ||
  fail "bootstrap did not explain how to make lwc globally callable"

printf 'using-lwc bootstrap tests: 8 passed\n'
