mindnet/tests/test_importer_detect_changes.sh
Lars 80d8f986bd
Some checks failed
Deploy mindnet to llm-node / deploy (push) Failing after 1s
tests/test_importer_detect_changes.sh hinzugefügt
2025-09-24 12:34:22 +02:00

106 lines
3.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# tests/test_importer_detect_changes.sh
# Prüft Änderungserkennung:
# - Body-Change -> wird erkannt bei hash-mode=body
# - Frontmatter-Change -> wird NICHT erkannt bei hash-mode=body
# - Frontmatter-Change -> wird erkannt bei hash-mode=frontmatter (bzw. MINDNET_HASH_COMPARE=Frontmatter)
#
# Aufruf:
# ./tests/test_importer_detect_changes.sh [--vault ./test_vault] [--prefix mindnet] [--note-id concept-alpha]
VAULT="./test_vault"
PREFIX="${COLLECTION_PREFIX:-mindnet}"
NOTE_ID="concept-alpha"
while [[ $# -gt 0 ]]; do
case "$1" in
--vault) VAULT="$2"; shift 2;;
--prefix) PREFIX="$2"; shift 2;;
--note-id) NOTE_ID="$2"; shift 2;;
*) echo "Unbekannte Option: $1" >&2; exit 2;;
esac
done
export COLLECTION_PREFIX="$PREFIX"
# Hilfsfunktion: lese Pfad zur Note aus Frontmatter id:
PYFIND=$(cat <<'PY'
import sys, os, glob, yaml
vault = sys.argv[1]
nid = sys.argv[2]
for p in glob.glob(os.path.join(vault, "**", "*.md"), recursive=True):
with open(p, "r", encoding="utf-8") as f:
s = f.read()
if s.startswith("---"):
try:
fm_txt, body = s.split("\n---\n", 1)
fm = yaml.safe_load(fm_txt.strip("- \n")) or {}
except Exception:
continue
if fm.get("id") == nid:
print(p)
break
PY
)
NOTE_PATH=$(python3 -c "$PYFIND" "$VAULT" "$NOTE_ID")
if [[ -z "${NOTE_PATH:-}" ]]; then
echo "Note mit id=$NOTE_ID nicht gefunden im Vault=$VAULT" >&2
exit 3
fi
echo "Note: $NOTE_PATH"
# Backup
ORIG="$(mktemp)"
cp "$NOTE_PATH" "$ORIG"
cleanup() {
cp "$ORIG" "$NOTE_PATH" || true
rm -f "$ORIG"
}
trap cleanup EXIT
# Ausgangszustand importieren (Baseline)
python3 -m scripts.import_markdown --vault "$VAULT" --apply --prefix "$PREFIX" >/dev/null
echo "-- Test A: Body-Änderung soll erkannt werden (hash-mode=body)"
# Body um eine Zeile erweitern
python3 - "$NOTE_PATH" <<'PY'
import sys
p=sys.argv[1]
with open(p,"r+",encoding="utf-8") as f:
s=f.read()
if s.startswith("---"):
fm, body = s.split("\n---\n",1)
body = body + "\n\nBodyTest: Änderung A"
f.seek(0); f.truncate(); f.write(fm+"\n---\n"+body)
PY
OUT=$(python3 -m scripts.import_markdown --vault "$VAULT" --prefix "$PREFIX" | grep "\"note_id\": \"$NOTE_ID\"" || true)
echo "$OUT"
echo "$OUT" | grep -q '"changed": true' || { echo "ERWARTET: changed=true (Body) — FEHLER"; exit 4; }
echo "-- Test B: Frontmatter-Änderung soll NICHT erkannt werden bei mode=body"
# Frontmatter-Feld hinzufügen
python3 - "$NOTE_PATH" <<'PY'
import sys, yaml, io
p=sys.argv[1]
with open(p,"r+",encoding="utf-8") as f:
s=f.read()
if s.startswith("---"):
fm_txt, body = s.split("\n---\n",1)
fm = yaml.safe_load(fm_txt.strip("- \n")) or {}
fm["test_flag"] = True
buf = io.StringIO()
yaml.safe_dump(fm, buf, allow_unicode=True, sort_keys=False)
f.seek(0); f.truncate(); f.write("---\n"+buf.getvalue()+"---\n"+body)
PY
OUT=$(python3 -m scripts.import_markdown --vault "$VAULT" --prefix "$PREFIX" | grep "\"note_id\": \"$NOTE_ID\"" || true)
echo "$OUT"
echo "$OUT" | grep -q '"changed": false' || { echo "ERWARTET: changed=false (FM bei mode=body) — FEHLER"; exit 5; }
echo "-- Test C: Frontmatter-Änderung soll erkannt werden bei mode=frontmatter"
OUT=$(MINDNET_HASH_COMPARE=Frontmatter python3 -m scripts.import_markdown --vault "$VAULT" --prefix "$PREFIX" | grep "\"note_id\": \"$NOTE_ID\"" || true)
echo "$OUT"
echo "$OUT" | grep -q '"changed": true' || { echo "ERWARTET: changed=true (FM bei mode=frontmatter) — FEHLER"; exit 6; }
echo "✔ Änderungserkennung: OK"