Files
FFF_vtt_frozen/tools/mutation.sh
T
SirStone cae20d7c89 tools: WS probing and the token wire tests (#20)
tools/probing.test.mjs re-runs the M2 T8 probing test over the wire with a hostile
client: over-cap probes always get the one uniform nack (old position, no code), two
servers whose walls differ only beyond cap + token radius answer 90 identical
probes identically, and a wall inside that bound does change the replies, so the
harness is shown able to fail. It stays inside the 6.4 burst, so no probe is a rate
limit.

tools/tokens.test.mjs pins the wire behaviour of the slice: the 6.1 acks and codes,
the controller-only and GM-only rules, the connect resync order, the unnamed-player
gate, the door push and a restart.

tools/mutation.sh gains aa-af: the egress ignoring the view rule, gm_only and the
owner arm of the rule, a missing token_gone, a GM-only field in a player frame, and
explored forgetting cells.
2026-11-15 09:35:29 +01:00

146 lines
12 KiB
Bash
Executable File

#!/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
# p: token bucket never refills | q: reqId upper bound dropped | r: malformed flood never fires | s: frame counter frozen
# t: wall removal renumbers next_wall_id (the dense-id design, #3) | u: the id counter is not loaded from the DB
# v: the id counter is never written to the DB | w: a wall may stay open | x: zero-length walls accepted
# y: wall endpoints unchecked against the world bounds | z: door.set on a plain wall accepted | z2: persisted `open` loaded inverted
# aa: the egress sends every token (LOS and gm_only ignored) | ab: gm_only ignored in the view rule
# ac: a controlled token is not always visible | ad: no token_gone when a token leaves view
# ae: a player token frame carries the GM-only `kind` | af: explored forgets the cells outside the current polygon
# 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)
PAA="the set of tokens follows the visibility rule"
AB="a gm_only token inside the LOS is not visible"
AC="a controlled token is always visible"
AD="leaving view sends"
AE="no GM-only field"
AF="moving adds cells and never forgets"
IDS=()
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"
AA="the set of tokens follows the visibility rule"
AB="a gm_only token inside the LOS is not visible"
AC="a controlled token is always visible"
AD="leaving view sends"
AE="no GM-only field"
AF="moving adds cells and never forgets"
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"
# slice 4 (#19): the wall id counter (never reused, #3), the "a wall is never open" invariant, the
# 6.1 validation (a != b, in bounds) and the persisted door state.
TW="never reuses|id counter survives"
run t "wall removal renumbers next_wall_id" server/world.nim 's/^ sc\.walls\.delete idx\.get$/ sc.walls.delete idx.get; sc.nextWallId = sc.walls.len + 1/' "$TW"
run u "the id counter is not loaded from the DB" server/world.nim 's/nextWallId: parseInt(row\[10\])/nextWallId: 1/' "$TW"
run v "the id counter is never written to the DB" server/world.nim '/db.exec(sql"UPDATE scene SET next_wall_id/d' "$TW"
run w "a wall may stay open" server/world.nim 's/^ if result\.kind == wkWall: result\.open = false$/ discard/' "a wall is never open"
run x "zero-length walls accepted" server/world.nim 's/ (rec\.a\.x != rec\.b\.x or rec\.a\.y != rec\.b\.y) and inBounds/ inBounds/' "zero-length wall"
run y "wall endpoints unchecked against the world bounds" server/world.nim 's/p\.x >= 0 and p\.y >= 0 and p\.x <= sc\.w and p\.y <= sc\.h/p.x >= 0 and p.y >= 0/' "both endpoints must be inside"
run z "door.set accepted on a plain wall" server/world.nim '/if sc.walls\[idx.get\].kind != wkDoor: return waNotDoor/d' "wrong kinds are their own"
run z2 "persisted open state loaded inverted" server/world.nim 's/open: row\[6\] == "1",/open: row[6] == "0",/' "round-trip through a reload"
# slice 5 (#20): the view rule, the egress diff and the explored set
run aa "the egress ignores LOS, gm_only and ownership" server/views.nim 's/^ if not pv\.visibleTo(t, playerId): continue$/ if false: continue/' "$AA"
run ab "gm_only ignored by the view rule" server/vision.nim '/^ if t\.gmOnly: return false$/d' "$AB"
run ac "a controlled token is not always visible" server/vision.nim '/^ if t\.owner == playerId: return true$/d' "$AC"
run ad "no token_gone when a token leaves the view" server/views.nim 's/^ result\.add tokenGoneFrame(id)$/ discard/' "$AD"
run ae "a player token frame carries the GM-only kind" server/views.nim 's/ "statuses": \[\], "props": props/ "statuses": [], "props": props, "kind": "npc"/' "$AE"
run af "explored forgets cells outside the current polygon" server/vision.nim 's/^ for idx in added: pv\.explored\.incl idx$/ pv.explored = added/' "$AF"
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 t u v w x y z z2 aa ab ac ad ae af; 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