packages feed

keiro-dsl-0.15.0.0: test/source-anchor-mutation-test.sh

#!/usr/bin/env bash
# Restoring falsification evidence for exact behavior-source anchors.
set -euo pipefail

SPEC="keiro-dsl/test/fixtures/behavior-complete.keiro"
OUT="keiro-dsl/test/conformance-behavior-complete"
WITNESSES="$OUT/BehaviorComplete/Journey/BehaviorHoles.hs"
SOURCE_MAP="$OUT/Generated/BehaviorComplete/BehaviorSourceMap.hs"
BEHAVIOR_CONTRACT="$OUT/Generated/BehaviorComplete/Journey/BehaviorContract.hs"
LEDGER="$OUT/keiro-dsl-ledger.context.behavior-complete.txt"

BACKUP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/keiro-source-anchor-mutation.XXXXXX")"
case "$BACKUP_DIR" in
  *keiro-source-anchor-mutation.*) ;;
  *) echo "FAIL: unexpected backup path: $BACKUP_DIR"; exit 1 ;;
esac

cp "$SPEC" "$BACKUP_DIR/behavior-complete.keiro"
cp "$WITNESSES" "$BACKUP_DIR/BehaviorHoles.hs"
cp "$SOURCE_MAP" "$BACKUP_DIR/BehaviorSourceMap.hs"
cp "$BEHAVIOR_CONTRACT" "$BACKUP_DIR/BehaviorContract.hs"
cp "$LEDGER" "$BACKUP_DIR/ledger.txt"

restore_all() {
  cp "$BACKUP_DIR/behavior-complete.keiro" "$SPEC"
  cp "$BACKUP_DIR/BehaviorHoles.hs" "$WITNESSES"
  cp "$BACKUP_DIR/BehaviorSourceMap.hs" "$SOURCE_MAP"
  cp "$BACKUP_DIR/BehaviorContract.hs" "$BEHAVIOR_CONTRACT"
  cp "$BACKUP_DIR/ledger.txt" "$LEDGER"
}

cleanup() {
  restore_all
  rm -f "$BACKUP_DIR/behavior-complete.keiro" "$BACKUP_DIR/BehaviorHoles.hs" "$BACKUP_DIR/BehaviorSourceMap.hs" "$BACKUP_DIR/BehaviorContract.hs" "$BACKUP_DIR/ledger.txt" "$BACKUP_DIR/scaffold.log" "$BACKUP_DIR/report.log"
  rmdir "$BACKUP_DIR"
}
trap cleanup EXIT

run_report() {
  cabal run -v0 keiro-dsl-behavior-complete-report -- --format=json
}

echo "== baseline: source-anchor planner failures are stable and write nothing =="
cabal test -v0 keiro-dsl-test --test-options='--match "refuses line-only, missing, and duplicate behavior source anchors before writes"'
run_report >/dev/null
echo "ok: planner and runtime baselines are green"

echo "== move every behavior source anchor by two lines =="
perl -0pi -e 's/\A/# source movement qualification\n\n/' "$SPEC"
cabal run -v0 keiro-dsl -- scaffold "$SPEC" --out "$OUT" >"$BACKUP_DIR/scaffold.log" 2>&1

if cmp -s "$BACKUP_DIR/BehaviorSourceMap.hs" "$SOURCE_MAP"; then
  echo "FAIL: source movement did not update BehaviorSourceMap"
  exit 1
fi
if cmp -s "$BACKUP_DIR/ledger.txt" "$LEDGER"; then
  echo "FAIL: source movement did not update source-bearing ledger provenance"
  exit 1
fi
cmp "$BACKUP_DIR/BehaviorContract.hs" "$BEHAVIOR_CONTRACT"
cmp "$BACKUP_DIR/BehaviorHoles.hs" "$WITNESSES"
grep -Fq '"behavior-v1-43b8fc7fa48595dd" -> Just (BehaviorSourceLocation "keiro-dsl/test/fixtures/behavior-complete.keiro" 23 16)' "$SOURCE_MAP"
echo "ok: only positional artifacts moved; contracts and witnesses stayed byte-identical"

echo "== falsify a moved witness and require its new exact position =="
sed -i.sed-bak '/behavior-v1-43b8fc7fa48595dd/ s/(startCommand 0)/(decideCommand 0)/' "$WITNESSES"
rm -f "$WITNESSES.sed-bak"
set +e
run_report >"$BACKUP_DIR/report.log" 2>&1
STATUS=$?
set -e
if [[ "$STATUS" -eq 0 ]]; then
  sed -n '1,240p' "$BACKUP_DIR/report.log"
  echo "FAIL: moved command-mismatch witness stayed green"
  exit 1
fi
grep -Fq 'command-mismatch' "$BACKUP_DIR/report.log"
grep -Fq 'keiro-dsl/test/fixtures/behavior-complete.keiro:23:16' "$BACKUP_DIR/report.log"
echo "ok: failing witness reported the moved file:line:column"

echo "== restore exact source and generated bytes =="
restore_all
cmp "$BACKUP_DIR/behavior-complete.keiro" "$SPEC"
cmp "$BACKUP_DIR/BehaviorHoles.hs" "$WITNESSES"
cmp "$BACKUP_DIR/BehaviorSourceMap.hs" "$SOURCE_MAP"
cmp "$BACKUP_DIR/BehaviorContract.hs" "$BEHAVIOR_CONTRACT"
cmp "$BACKUP_DIR/ledger.txt" "$LEDGER"
run_report >/dev/null
git diff --check
echo "PASS: source-anchor mutations were caught and every touched file was restored"