Elffuss BraidHub

utopia-ia/elffuss-code public

main · 5.3 KiB · executableRawPermalink
1#!/usr/bin/env bash
2# Runs Code's suites in both modes, and level 2's own suites once.
3#
4# tests/run-modes.sh every suite in tests/suites.txt
5# tests/run-modes.sh --only codeedit,e2e just these (from any section)
6# tests/run-modes.sh --mode default only one mode (default | ext | both)
7# tests/run-modes.sh --no-once leave out level 2's own suites
8#
9# One loopback server (tests/lib/x2-server.mjs: Code on 127.0.0.1, the sandbox
10# on localhost, both on one random port) serves every suite. A suite in [both]
11# runs as "node S.mjs" (default mode) and as "node --import ./lib/x2-mode.mjs
12# S.mjs" (extension mode: two code extensions installed and running in the
13# sandbox while the suite works, and three more checks at the end). A suite
14# passes when it exits 0, prints at least one ✅ (or ✓) and no ❌ (or ✗). The script fails if
15# any suite fails in either mode. Logs go to a temporary folder; no timings.
16set -uo pipefail
17set -m # each suite in its own process group
18cd "$(dirname "$0")"
19
20MODE=both ONLY="" ONCE=1 TIMEOUT="${SUITE_TIMEOUT:-900}"
21while [ $# -gt 0 ]; do
22 case "$1" in
23 --mode) MODE="$2"; shift 2 ;;
24 --only) ONLY=",$2,"; shift 2 ;;
25 --no-once) ONCE=0; shift ;;
26 *) echo "unknown option: $1"; exit 2 ;;
27 esac
28done
29case "$MODE" in default|ext|both) ;; *) echo "--mode is default, ext or both"; exit 2 ;; esac
30
31# suites.txt: the names in one section
32section() { awk -v s="[$1]" '/^\[/{on=($0==s); next} on && NF && $1 !~ /^#/ {print $1}' suites.txt; }
33pick() { if [ -z "$ONLY" ]; then cat; else while read -r n; do case "$ONLY" in *",$n,"*) echo "$n" ;; esac; done; fi; }
34
35BOTH=$(section both | pick)
36ONCE_LIST=""; [ "$ONCE" = 1 ] && ONCE_LIST=$(section once | pick)
37if [ -n "$ONLY" ]; then
38 for n in $(echo "$ONLY" | tr ',' ' '); do
39 if ! { echo "$BOTH"; echo "$ONCE_LIST"; } | grep -qx "$n"; then
40 if section not-run | grep -qx "$n:\?"; then echo "$n is in [not-run]"; else echo "$n is not in tests/suites.txt"; fi
41 exit 2
42 fi
43 done
44fi
45for n in $BOTH $ONCE_LIST; do [ -f "$n.mjs" ] || { echo "missing suite: $n.mjs"; exit 2; }; done
46
47TMPROOT="${TMPDIR:-/tmp}"
48LOGS=$(mktemp -d "${TMPROOT%/}/elffuss-run-modes.XXXXXX")
49export SCRATCH="$LOGS/scratch"
50mkdir -p "$SCRATCH"
51
52node lib/x2-server.mjs > "$LOGS/server.log" 2>&1 &
53SERVER=$!
54cleanup() { kill -TERM -- -"$SERVER" 2>/dev/null; wait "$SERVER" 2>/dev/null; }
55trap cleanup EXIT
56trap 'exit 130' INT TERM
57BASE=""
58for _ in $(seq 1 100); do
59 BASE=$(sed -n 's#^Code *\(http://127\.0\.0\.1:[0-9]*\)/$#\1#p' "$LOGS/server.log" | head -1)
60 [ -n "$BASE" ] && break
61 sleep 0.1
62done
63[ -n "$BASE" ] || { echo "the test server did not start:"; cat "$LOGS/server.log"; exit 1; }
64export BASE
65echo "BASE=$BASE logs: $LOGS"
66
67# run one suite in one mode, with a watchdog on its process group
68run() { # name mode → sets RC, OKS, BAD, SKIP
69 local name=$1 mode=$2 log="$LOGS/$1.$2.log"
70 if [ "$mode" = ext ]; then node --import ./lib/x2-mode.mjs "$name.mjs" > "$log" 2>&1 &
71 else node "$name.mjs" > "$log" 2>&1 &
72 fi
73 local pid=$!
74 ( sleep "$TIMEOUT"; echo "❌ run-modes: stopped after the suite's time limit" >> "$log"; kill -TERM -- -"$pid" 2>/dev/null; sleep 5; kill -KILL -- -"$pid" 2>/dev/null ) &
75 local dog=$!
76 wait "$pid"; RC=$?
77 kill -TERM -- -"$dog" 2>/dev/null; wait "$dog" 2>/dev/null
78 # ✅/❌ per check; the two batteries mark their cases with ✓/✗ at the start of a line
79 OKS=$(( $(grep -c '✅' "$log") + $(grep -c '^ *✓ ' "$log") ))
80 BAD=$(( $(grep -c '❌' "$log") + $(grep -c '^ *✗ ' "$log") ))
81 SKIP=$(grep -c 'NOT RUN' "$log")
82}
83verdict() { # → PASS | FAIL | NOT RUN, and "no IDE" when extension mode was not exercised
84 if [ "$RC" -eq 0 ] && [ "$BAD" -eq 0 ] && [ "$OKS" -gt 0 ]; then
85 if [ "${2:-}" = ext ] && grep -q 'x2-mode: not exercised' "$LOGS/$1.ext.log"; then echo "PASS, no IDE"; else echo PASS; fi
86 elif [ "$RC" -eq 0 ] && [ "$BAD" -eq 0 ] && [ "$SKIP" -gt 0 ]; then echo "NOT RUN"
87 else echo FAIL; fi
88}
89
90fails=0 suites=0 noide=""
91printf '\n%-24s %-22s %-30s\n' suite default extension
92for n in $BOTH; do
93 suites=$((suites + 1))
94 cols=""
95 for m in default ext; do
96 if [ "$MODE" != both ] && [ "$MODE" != "$m" ]; then cols="$cols$(printf '%-22s ' '-')"; continue; fi
97 run "$n" "$m"
98 v=$(verdict "$n" "$m")
99 [ "$v" = FAIL ] && fails=$((fails + 1))
100 [ "$v" = "PASS, no IDE" ] && noide="$noide $n"
101 cols="$cols$(printf '%-22s ' "$v $OKS ok $BAD bad")"
102 done
103 printf '%-24s %s\n' "$n" "$cols"
104 for m in default ext; do
105 f="$LOGS/$n.$m.log"
106 [ -f "$f" ] || continue
107 if [ "$(grep -c '❌\|^ *✗ ' "$f")" -gt 0 ] || ! grep -q '✅\|^ *✓ ' "$f"; then grep '❌\|^ *✗ \|Error\|NOT RUN' "$f" | head -4 | sed "s/^/ [$m] /"; fi
108 done
109done
110for n in $ONCE_LIST; do
111 suites=$((suites + 1))
112 run "$n" once
113 v=$(verdict)
114 [ "$v" = FAIL ] && fails=$((fails + 1))
115 printf '%-24s %-22s\n' "$n" "$v $OKS ok $BAD bad (once)"
116 [ "$v" = FAIL ] && grep '❌\|Error' "$LOGS/$n.once.log" | head -4 | sed 's/^/ /'
117done
118
119echo
120# (sin IDE el recinto no corrió: pasan, pero el modo extensiones no las probó)
121if [ -n "$noide" ]; then echo "ℹ️ extension mode not exercised (no page opened the IDE, the sandbox never ran):$noide"; fi
122if [ "$fails" -eq 0 ]; then echo "✅ run-modes: $suites suites, no failures"; else echo "❌ run-modes: $fails failing runs in $suites suites (logs: $LOGS)"; fi
123[ "$fails" -eq 0 ]