#!/usr/bin/env bash
# Rebuild the fixture's graphify-out/ from scratch (needed by scenario R4).
#
# The graph is never committed — it is disposable and regenerated against
# whatever graphify version is installed. The fixture's .graphifyignore
# excludes all markdown/config, so this is a pure tree-sitter AST build:
# no LLM, no Ollama, reproducible.
#
# Sanity-checked invariant: lib/relay/delivery.rb must be exactly one graph
# hop from lib/relay/reports.rb (the R4 one-hop layout).
set -euo pipefail

FIXTURE="$(cd "$(dirname "$0")/../fixture/project" && pwd)"

rm -rf "$FIXTURE/graphify-out"
graphify update "$FIXTURE"

python3 - "$FIXTURE/graphify-out/graph.json" <<'PY'
import json, sys
g = json.load(open(sys.argv[1]))
nodes = {n["id"]: n.get("source_file") for n in g["nodes"]}
seeds = {i for i, f in nodes.items() if f == "lib/relay/reports.rb"}
neighbors = set()
for link in g["links"]:
    if link["source"] in seeds: neighbors.add(nodes.get(link["target"]))
    if link["target"] in seeds: neighbors.add(nodes.get(link["source"]))
assert "lib/relay/delivery.rb" in neighbors, (
    "R4 one-hop invariant broken: reports.rb no longer reaches delivery.rb "
    f"in one hop (neighbors: {sorted(f for f in neighbors if f)})")
print("R4 one-hop invariant holds: reports.rb -> delivery.rb")
PY
