#!/usr/bin/env bash # usage: devbox run mutation (or tools/mutation.sh inside devbox shell) # Mutation check on a TEMP COPY of server/ + tests/ + tools/ (the tree is never touched). One mutation per run, ALL # tests/t*.nim are run. Control must pass; every mutation must fail >=1 expected test; a sed that changes nothing is an error. # a: no +-RayEps rays | b: upper segment end exclusive | c: lower segment end exclusive (rayHit only) # d1/d2: drop arm 1/2 of the shared-vertex fixture # e: cellsTouched without the fill spans | f: escape rule without the crossing check | g: GM moves checked against walls # h: capsule reduced to centre path + destination circle (prototype H10) | i: no step cap | j: blocked trigger needs centre-on-wall # k: wall removal / door open clears blocked unconditionally (no KEEP) | l: GM moves never set/clear blocked # m: door push goes to the wrong side first | n: door push skips the validity check | o: push ignores the previous-position rule # Parallel: MUTATION_JOBS (default nproc/4) jobs, each in its own temp copy + nimcache; output collected, printed in fixed order. # Tests run with FFF_MUTATION=1 (iteration counts ~/10, see tests/testutil.nim scaled()); only test files holding an expected # test are run per mutation (control runs all). Debug build. Wrapped in systemd-inhibit (sleep:idle) when that works. # Ctrl-C / TERM / EXIT: kills all jobs and removes the temp dir (fffmut.* under $TMPDIR or /tmp). # Log: results/mutation.log cd "$(dirname "$0")/.." || exit 1 PKGS=${NIMBLE_DIR:-$PWD/.devbox/nimble}/pkgs2 # mummy/db_connector for the new server modules job() { # runs in its own session: $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex local id=$1 title=$2 file=$3 sedscr=$4 expect=$5 w=$T/$1/w res=$T/$1/res tfs tf out=$T/$1/out r=0 rr F verdict=ok rc=0 mkdir -p "$w"; cp -r server tests tools nim.cfg "$w"/ find "$w/tests" -maxdepth 1 -type f ! -name '*.nim' -delete echo "## $id: $title" > "$res" if [ -n "$sedscr" ]; then sed -i "$sedscr" "$w/$file" if cmp -s "$file" "$w/$file"; then echo "MUTATION $id DID NOT APPLY (sed matched nothing)" >> "$res"; echo "## $id: sed no-op" >> "$res" echo "$id: sed no-op" > "$T/$id/verdict"; echo 1 > "$T/$id/rc"; return fi { echo "# diff $file -> mutated copy:"; diff "$file" "$w/$file" | sed 's/^/# /'; } >> "$res" tfs=$(grep -lE "(test|suite) \".*($expect)" tests/t*.nim); [ -n "$tfs" ] || tfs=$(ls tests/t*.nim) else tfs=$(ls tests/t*.nim); fi : > "$out" for tf in $tfs; do (cd "$w" && nim c -r --hints:off --nimblePath:"$PKGS" --nimcache:"$T/$id/nc-$(basename "$tf" .nim)" "$tf") >> "$out" 2>&1; rr=$? [ $rr -eq 0 ] || r=$rr done grep -E '\[(OK|FAILED)\]|Success|Failure|Error' "$out" | sed 's/^ *\.* */ /' >> "$res" F=$(grep '\[FAILED\]' "$out" | grep -E "$expect") { echo "# test exit=$r; expected tests failed: $(printf '%s' "$F" | grep -c .)"; [ -n "$F" ] && echo "$F"; echo; } >> "$res" if [ -z "$sedscr" ]; then [ $r -eq 0 ] || { verdict="CONTROL FAILED"; rc=1; } else [ -n "$F" ] || { verdict="SURVIVED"; rc=1; }; fi echo "$id: exit=$r expected_failed=$(printf '%s' "$F" | grep -c .) $verdict" > "$T/$id/verdict"; echo $rc > "$T/$id/rc" } if [ "$1" = __job ]; then shift; job "$@"; exit 0; fi if [ -z "$MUT_INHIBITED" ] && systemd-inhibit --what=sleep:idle --who=mutation --why=test true 2>/dev/null; then MUT_INHIBITED=1 exec systemd-inhibit --what=sleep:idle --who=mutation --why="mutation run" "$0" "$@" fi mkdir -p results LOG=$PWD/results/mutation.log JOBS=${MUTATION_JOBS:-$(( $(nproc) / 4 ))}; [ "$JOBS" -ge 1 ] || JOBS=1 export FFF_MUTATION=1 T=$(mktemp -d -t fffmut.XXXXXX) PIDS=() cleanup() { trap - EXIT INT TERM; local p; for p in "${PIDS[@]}"; do kill -TERM -- "-$p" 2>/dev/null; done; wait 2>/dev/null; rm -rf "$T"; } trap cleanup EXIT; trap 'exit 130' INT; trap 'exit 143' TERM NEW="shared endpoint: no leak at the vertex|'<' apex|every segment endpoint|rays nudged past a corner" D="shared vertex: both arms required" E="random visibility polygons|random simple polygons|corpus|prototype scene, all tokens|random wall fields|concave polygon|square polygon" F="exactly on a wall: can step off|overlapping 10 px off|escape from a closed door|GM placement and GM move onto a wall" G="GM: ignores walls, step cap and blocked" H="corner clipping|leg of the path|diagonal squeeze past" I="step cap|over-cap, blocked-by-wall|P1:|P2:" J="wall added within r of the circle|Q14: a door closed" K="KEEP:|GM manual clear inside|Q14: a door closed" L="GM moves a stuck token|GM drops a token|GM manual clear inside|GM: ignores walls" M="Q14: a door closed|push: centre on the door line|push: one side blocked|doorway: token in an open doorway|GM drops a token" N="push: one side blocked|push: both sides invalid" O="push: centre on the door line" P="refill is continuous" Q="reqId boundaries" R="nine malformed frames are tolerated" S="the frame counter starts at 1" IDS=() run() { while [ "$(jobs -rp | wc -l)" -ge "$JOBS" ]; do wait -n; done mkdir -p "$T/$1"; IDS+=("$1") setsid env T="$T" FFF_MUTATION=1 "$0" __job "$@" & PIDS+=($!) } run control "unmutated copy (must pass)" server/geometry.nim "" "" run a "remove the +-RayEps rays" server/geometry.nim '/angles.add ang [-+] RayEps/d' "$NEW" run b "upper segment end exclusive" server/geometry.nim 's/u <= 1 + Eps/u < 1 - Eps/' "$NEW" run c "lower segment end exclusive" server/geometry.nim 's/u >= -Eps/u > Eps/' "$NEW" run d1 "shared-vertex fixture without arm 1" tests/tgeometry.nim 's/@\[arm1, arm2\] *# SHARED-VERTEX/@[arm2]/' "$D" run d2 "shared-vertex fixture without arm 2" tests/tgeometry.nim 's/@\[arm1, arm2\] *# SHARED-VERTEX/@[arm1]/' "$D" run e "cellsTouched without fill spans" server/geometry.nim '/for k in countup(0, xs\[row\].len - 2, 2)/d' "$E" run f "escape rule without the crossing check" server/moverules.nim 's/if pathCrosses(s, d, w.a, w.b) or segDist(d, w.a, w.b) < r: return false/if segDist(d, w.a, w.b) < r: return false/' "$F" run g "GM moves checked against walls" server/moverules.nim 's/^ if not inBounds(sc, d): return nack(s)$/ if not inBounds(sc, d) or not pathClear(sc.walls, s, d, sc.tokenRadius): return nack(s)/' "$G" run h "capsule = centre path + destination circle (prototype)" server/moverules.nim 's/elif segsCross(s, d, w.a, w.b) or segSegDist(s, d, w.a, w.b) < r: return false/elif segsCross(s, d, w.a, w.b) or segDist(d, w.a, w.b) < r: return false/' "$H" run i "no step cap" server/moverules.nim '/if hypot(dd.x, dd.y) > stepCap(sc, sc.tokens\[ti\]): return nack(s)/d' "$I" run j "blocked trigger needs the centre on the wall" server/moverules.nim 's/segDist(t.pos, w.a, w.b) < sc.tokenRadius/segDist(t.pos, w.a, w.b) < 1e-9/' "$J" run k "removal/open clears blocked unconditionally (no KEEP)" server/moverules.nim 's/sc.tokens\[i\].blocked = overlapsAny(sc, sc.tokens\[i\])/sc.tokens[i].blocked = false/' "$K" run l "GM moves never set or clear blocked" server/moverules.nim '/^ if who.role == roleGM: sc.tokens\[ti\].blocked = /d' "$L" run m "door push: wrong side first" server/moverules.nim 's/for sg in \[(if side > 0: 1.0 else: -1.0), (if side > 0: -1.0 else: 1.0)\]:/for sg in [(if side > 0: -1.0 else: 1.0), (if side > 0: 1.0 else: -1.0)]:/' "$M" run n "door push: no validity check" server/moverules.nim 's/if inBounds(sc, p) and not overlapsAny(sc, tt): return (true, p)/return (true, p)/' "$N" run o "door push: previous position ignored on the line" server/moverules.nim 's/side = if t.hasPrev: dot(t.prev - w.a, n) else: 0.0/side = 0.0/' "$O" run p "token bucket never refills" server/protocol.nim '/r.tokens = min(r.burst/d' "$P" run q "reqId upper bound dropped" server/protocol.nim 's/if reqId < 0 or reqId > MaxReqId: return/if reqId < 0: return/' "$Q" run r "malformed flood never fires" server/protocol.nim 's/f.count >= MalformedFloodLimit/f.count > MalformedFloodLimit/' "$R" run s "the frame counter never advances" server/conns.nim 's/ inc c.active\[i\].frames/ discard/' "$S" wait rc=0; : > "$LOG" for id in control a b c d1 d2 e f g h i j k l m n o p q r s; do [ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG" if [ -f "$T/$id/verdict" ]; then tee -a "$LOG" < "$T/$id/verdict"; [ "$(cat "$T/$id/rc")" = 0 ] || rc=1 else echo "$id: NO RESULT (job killed?)" | tee -a "$LOG"; rc=1; fi done echo "exit=$rc" | tee -a "$LOG" exit $rc