#!/usr/bin/env bash
# Fixture assertions: wal_without_shm
set -euo pipefail
target_dir="${1:?usage: assert.sh <target_dir> <stage>}"
stage="${2:?usage: assert.sh <target_dir> <stage>}"
tool_bin="${TOOL_BIN:-br}"
cd "$target_dir"

case "$stage" in
  detect)
    out=$("$tool_bin" doctor --json 2>/dev/null) || true
    # db.sidecars must be warn (NOT error) since frankensqlite expects this.
    echo "$out" | jq -e '
      .checks[] | select(.name == "db.sidecars") | select(.status == "warn")
      | select(.message | test("WAL sidecar"; "i"))
    ' >/dev/null || {
      echo "ASSERT FAIL[$stage]: db.sidecars not warn for WAL-without-SHM" >&2
      echo "$out" | jq '.checks[] | select(.name == "db.sidecars")' >&2
      exit 1
    }
    ;;
  post_repair)
    # Repair either no-ops or checkpoints the WAL into the DB. Either is
    # acceptable as long as data is preserved: beads.db must remain a real
    # SQLite file and be queryable. (Doctor must NOT delete the WAL without
    # first checkpointing — but a fresh-init workspace has no uncommitted
    # data so checkpoint-then-remove is benign.)
    [ -f .beads/beads.db ] || {
      echo "ASSERT FAIL[$stage]: beads.db missing after --repair" >&2
      exit 1
    }
    size=$(stat -c%s .beads/beads.db 2>/dev/null || stat -f%z .beads/beads.db)
    if [ "$size" -lt 1024 ]; then
      echo "ASSERT FAIL[$stage]: beads.db suspiciously small after --repair ($size bytes)" >&2
      exit 1
    fi
    # Doctor should still report ok schema after repair.
    out=$("$tool_bin" doctor --json 2>/dev/null) || true
    n_err=$(echo "$out" | jq '[.checks[] | select(.status == "error")] | length')
    if [ "$n_err" -ne 0 ]; then
      echo "ASSERT FAIL[$stage]: --repair introduced error checks" >&2
      echo "$out" | jq '.checks[] | select(.status == "error")' >&2
      exit 1
    fi
    ;;
  post_undo)
    [ -f .beads/beads.db ] || { echo "ASSERT FAIL[$stage]: beads.db gone" >&2; exit 1; }
    ;;
  *)
    echo "unknown stage: $stage" >&2
    exit 2
    ;;
esac
