#!/usr/bin/env bash
# A `feedcache` that speaks loop-check's stub protocol AND the subject's data contract.
#
# loop-check's own --stub writes a generic fake that prints a fixed line of prose. That
# is right for a subject whose tool output is prose and wrong for this one: every value
# has to parse and type-check on the way in, so under the generic stub even the baseline
# run fails validation and there is no successful run to contrast anything against.
# This shim keeps every knob loop-check turns -- exit code, empty output, duration --
# and makes the successful case return the shape the subject's contract requires.
#
# Installed by --pre into the same bin directory loop-check put its stub in.
sleep "${LOOPCHECK_STUB_SLEEP:-0}"
echo "feedcache $*" >> "${LOOPCHECK_CALLS:-/dev/null}" 2>/dev/null || true
if [ "${1:-}" = "alert" ]; then
  exit "${LOOPCHECK_STUB_EXIT:-0}"
fi
if [ -n "${LOOPCHECK_STUB_OUT:-}" ]; then
  # Well formed, correctly typed, and carrying a timestamp nothing downstream reads.
  # This is exactly what the production cache hands the subject on every broken day.
  printf '{"feed":"%s","observed_at":"2026-08-28T20:00:00","count":2,"rows":[{"id":"a"},{"id":"b"}]}\n' "${2:-unknown}"
fi
exit "${LOOPCHECK_STUB_EXIT:-0}"
