#!/bin/sh
set -eu

script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
scratch_dir=$(mktemp -d)
trap 'rm -rf "$scratch_dir"' EXIT HUP INT TERM

python3 "$script_dir/memory_cli.py" \
  --root "$script_dir/demo-store" \
  --as-of 2026-09-14 \
  validate > "$scratch_dir/actual-output.txt"

python3 "$script_dir/memory_cli.py" \
  --root "$script_dir/demo-store" \
  --as-of 2026-09-14 \
  retrieve customer-copy >> "$scratch_dir/actual-output.txt"

diff -u "$script_dir/expected-output.txt" "$scratch_dir/actual-output.txt"
echo "PASS expected output matches"

cp -R "$script_dir/demo-store" "$scratch_dir/source-tamper"
printf '\nTAMPER\n' >> "$scratch_dir/source-tamper/sources/customer-copy-policy-v2.md"
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/source-tamper" --as-of 2026-09-14 validate > "$scratch_dir/source-tamper.txt" 2>&1; then
  echo "FAIL source tamper was accepted" >&2
  exit 1
fi
grep -F "durable source hash mismatch" "$scratch_dir/source-tamper.txt" >/dev/null
echo "PASS source tamper rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/record-tamper"
printf '\n' >> "$scratch_dir/record-tamper/records/customer-copy-v2.json"
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/record-tamper" --as-of 2026-09-14 validate > "$scratch_dir/record-tamper.txt" 2>&1; then
  echo "FAIL record tamper was accepted" >&2
  exit 1
fi
grep -F "record hash mismatch" "$scratch_dir/record-tamper.txt" >/dev/null
echo "PASS record tamper rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/pointer-tamper"
python3 - "$scratch_dir/pointer-tamper/index.json" <<'PY'
import json
from pathlib import Path
import sys

path = Path(sys.argv[1])
data = json.loads(path.read_text(encoding="utf-8"))
data["entries"][0]["target"] = "../outside.json"
path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/pointer-tamper" --as-of 2026-09-14 validate > "$scratch_dir/pointer-tamper.txt" 2>&1; then
  echo "FAIL pointer tamper was accepted" >&2
  exit 1
fi
grep -F "path escapes the memory root" "$scratch_dir/pointer-tamper.txt" >/dev/null
echo "PASS pointer tamper rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/malformed-forward"
python3 - "$scratch_dir/malformed-forward/records/customer-copy-v1.json" <<'PY'
import json
from pathlib import Path
import sys

path = Path(sys.argv[1])
data = json.loads(path.read_text(encoding="utf-8"))
data["superseded_by"] = "customer-copy-v3"
path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/malformed-forward" --as-of 2026-09-14 validate > "$scratch_dir/malformed-forward.txt" 2>&1; then
  echo "FAIL malformed superseded_by link was accepted" >&2
  exit 1
fi
grep -F "superseded_by does not resolve" "$scratch_dir/malformed-forward.txt" >/dev/null
echo "PASS malformed superseded_by link rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/malformed-reverse"
python3 - "$scratch_dir/malformed-reverse" <<'PY'
from hashlib import sha256
import json
from pathlib import Path
import sys

root = Path(sys.argv[1])
record_path = root / "records/customer-copy-v2.json"
record = json.loads(record_path.read_text(encoding="utf-8"))
record.pop("supersedes")
record_path.write_text(json.dumps(record, indent=2) + "\n", encoding="utf-8")
index_path = root / "index.json"
index = json.loads(index_path.read_text(encoding="utf-8"))
index["entries"][0]["record_sha256"] = sha256(record_path.read_bytes()).hexdigest()
index_path.write_text(json.dumps(index, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/malformed-reverse" --as-of 2026-09-14 validate > "$scratch_dir/malformed-reverse.txt" 2>&1; then
  echo "FAIL malformed supersedes link was accepted" >&2
  exit 1
fi
grep -F "reverse supersedes link does not agree" "$scratch_dir/malformed-reverse.txt" >/dev/null
echo "PASS malformed supersedes link rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/self-link"
python3 - "$scratch_dir/self-link/records/customer-copy-v1.json" <<'PY'
import json
from pathlib import Path
import sys

path = Path(sys.argv[1])
data = json.loads(path.read_text(encoding="utf-8"))
data["superseded_by"] = data["id"]
path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/self-link" --as-of 2026-09-14 validate > "$scratch_dir/self-link.txt" 2>&1; then
  echo "FAIL supersession self-link was accepted" >&2
  exit 1
fi
grep -F "cannot be a self-link" "$scratch_dir/self-link.txt" >/dev/null
echo "PASS supersession self-link rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/cycle"
python3 - "$scratch_dir/cycle" <<'PY'
from hashlib import sha256
import json
from pathlib import Path
import sys

root = Path(sys.argv[1])
old_path = root / "records/customer-copy-v1.json"
new_path = root / "records/customer-copy-v2.json"
old = json.loads(old_path.read_text(encoding="utf-8"))
new = json.loads(new_path.read_text(encoding="utf-8"))
old["supersedes"] = new["id"]
new["status"] = "superseded"
new["superseded_by"] = old["id"]
old_path.write_text(json.dumps(old, indent=2) + "\n", encoding="utf-8")
new_path.write_text(json.dumps(new, indent=2) + "\n", encoding="utf-8")
index_path = root / "index.json"
index = json.loads(index_path.read_text(encoding="utf-8"))
index["entries"][0]["record_sha256"] = sha256(new_path.read_bytes()).hexdigest()
index_path.write_text(json.dumps(index, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/cycle" --as-of 2026-09-14 validate > "$scratch_dir/cycle.txt" 2>&1; then
  echo "FAIL supersession cycle was accepted" >&2
  exit 1
fi
grep -F "supersession cycle detected" "$scratch_dir/cycle.txt" >/dev/null
echo "PASS supersession cycle rejected"

cp -R "$script_dir/demo-store" "$scratch_dir/superseded-pointer"
python3 - "$scratch_dir/superseded-pointer" <<'PY'
from hashlib import sha256
import json
from pathlib import Path
import sys

root = Path(sys.argv[1])
record_path = root / "records/customer-copy-v1.json"
index_path = root / "index.json"
index = json.loads(index_path.read_text(encoding="utf-8"))
entry = index["entries"][0]
entry["record_id"] = "customer-copy-v1"
entry["target"] = "records/customer-copy-v1.json"
entry["record_sha256"] = sha256(record_path.read_bytes()).hexdigest()
index_path.write_text(json.dumps(index, indent=2) + "\n", encoding="utf-8")
PY
if python3 "$script_dir/memory_cli.py" --root "$scratch_dir/superseded-pointer" --as-of 2026-09-14 validate > "$scratch_dir/superseded-pointer.txt" 2>&1; then
  echo "FAIL pointer to superseded record was accepted" >&2
  exit 1
fi
grep -F "index points to a superseded record" "$scratch_dir/superseded-pointer.txt" >/dev/null
echo "PASS pointer to superseded record rejected"

if python3 "$script_dir/memory_cli.py" --root "$script_dir/demo-store" --as-of 2026-12-01 validate > "$scratch_dir/review-due.txt" 2>&1; then
  echo "FAIL due review was accepted" >&2
  exit 1
fi
grep -F "review due on 2026-12-01" "$scratch_dir/review-due.txt" >/dev/null
echo "PASS due review rejected"

if python3 "$script_dir/memory_cli.py" --root "$script_dir/demo-store" --as-of 2027-03-01 validate > "$scratch_dir/expired.txt" 2>&1; then
  echo "FAIL expired record was accepted" >&2
  exit 1
fi
grep -F "record expired on 2027-03-01" "$scratch_dir/expired.txt" >/dev/null
echo "PASS expired record rejected"
