Nacre/tools/smoke.sh
2026-11-13 08:05:16 -06:00

166 lines
9 KiB
Shell
Executable file

#!/usr/bin/env bash
# Smoke run: start Nacre in a hidden nested Wayland session (tools/nested-session.sh: Xvfb -> niri, software GL) with a
# throwaway profile, play a NACRE_SCRIPT, collect results. Nothing appears on the user's screen.
#
# Usage: tools/smoke.sh <script.txt> [outdir]
# outdir defaults to target/smoke/<script name>-<time>. It gets:
# nacre.log stdout + stderr of the browser (RUST_BACKTRACE=1)
# selftest.log every script line run, plus js results and warnings
# <name>.png `shot <name>` screenshots; <name>.json `status <name>` files
# home/ the throwaway HOME / XDG_* folders (profile, history, session)
# session/ the nested session's logs (niri.log, xvfb.log) and env
# Exit 0 only if the browser exited by itself with status 0, no "panicked" is in nacre.log and the script
# wrote `status done` (so every script must end with `status done` then `quit`) and no `assert` failed.
#
# Environment:
# SMOKE_HOME=dir reuse this throwaway home (e.g. a previous run's outdir/home) to test session restore
# SMOKE_TIMEOUT=s kill the browser after s seconds (default 180)
# SMOKE_SIZE=WxH nested output size (default 1280x800); the window fills it (niri, no gaps, at 0,0)
# SMOKE_BIN=path browser binary (default target/release/nacre)
# SMOKE_ARGS="..." extra command-line arguments for the browser (e.g. --incognito)
# NACRE_NO_BRIDGE=1 passed through: no network bridge
# SMOKE_FAKE_DISCORD=1 start tests/smoke/net/fake-discord.py on $XDG_RUNTIME_DIR/discord-ipc-0; it logs every frame
# it gets to outdir/discord.log (one JSON line each)
# SMOKE_KEYRING=1 put a throwaway GNOME Keyring (password "smoke", unlocked) kept in home/ on the run's own
# session bus, so the OS keyring path is used (cookie key, S025); SMOKE_HOME reruns find the same
# key. Without it the run's bus (always private, no service activation) has no keyring.
# SMOKE_VISIBLE=1 show the window on the user's screen (their Wayland display) with the real GPU instead of the
# hidden nested session. The window may take focus or switch the workspace (D-045); a niri rule
# like window-rule { match app-id="^dev\.nacre\.Nacre$"; open-on-output "DP-1"; } is optional;
# only the Wayland socket is shared, profile/runtime dir/bus stay throwaway. Selftest page input
# (click, rclick, pagescroll, pagetype) is then synthesized in the page, never the user's pointer.
#
# Hidden runs give the selftest real compositor input: NACRE_WL_INPUT (tools/wl-input, built here when missing) for
# pointer clicks and wheel, wtype for keys, on the nested output "winit" (WL_INPUT_FLIP_Y=1), and the nested niri's
# NIRI_SOCKET for `resize`.
#
# Script commands (src/selftest.rs has the full list): wait <s>, waitload [s], open <url>, newtab <url>,
# page <nacre://...>, shot <name>, status <name>, set <json pointer> <json>, flyout <name>, do <action>,
# cmd <name> [arg], js <script>, assert <js>, click|rclick <x> <y>, pagescroll <x> <y> <dy>, pagetype <text>,
# log <text>, quit. Lines starting with # are notes.
# In the script, @PAGES@ is replaced by the file:// URL of tests/smoke/pages, @PAGESDIR@ by its path, @HOME@ by the
# throwaway home.
#
# Never uses the user's profile, display, Discord socket or session bus; never sets a proxy.
set -u
root=$(cd "$(dirname "$0")/.." && pwd)
script=${1:?usage: tools/smoke.sh <script.txt> [outdir]}
[ -f "$script" ] || { echo "smoke: no such script $script" >&2; exit 2; }
out=${2:-$root/target/smoke/$(basename "$script" .txt)-$(date +%Y%m%d-%H%M%S)}
bin=${SMOKE_BIN:-$root/target/release/nacre}
[ -x "$bin" ] || { echo "smoke: no binary at $bin (build first)" >&2; exit 2; }
visible=${SMOKE_VISIBLE:-}
if [ -n "$visible" ]; then
[ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "${XDG_RUNTIME_DIR:-}/$WAYLAND_DISPLAY" ] \
|| { echo "smoke: SMOKE_VISIBLE needs the user's Wayland session" >&2; exit 2; }
else
for tool in Xvfb niri wtype; do
command -v $tool >/dev/null || { echo "smoke: $tool is missing" >&2; exit 2; }
done
wl_input=$root/tools/wl-input/target/release/wl-input
if [ ! -x "$wl_input" ]; then
nice cargo build --release -q --manifest-path "$root/tools/wl-input/Cargo.toml" || { echo "smoke: wl-input did not build" >&2; exit 2; }
fi
fi
mkdir -p "$out"
out=$(cd "$out" && pwd)
home=${SMOKE_HOME:-$out/home}
mkdir -p "$home"
home=$(cd "$home" && pwd)
rm -f "$out"/*.png "$out"/*.json "$out/selftest.log" "$out/nacre.log"
sed -e "s|@PAGES@|file://$root/tests/smoke/pages|g" -e "s|@PAGESDIR@|$root/tests/smoke/pages|g" -e "s|@HOME@|$home|g" "$script" >"$out/script.txt"
session=
cleanup() {
[ -n "${discord_pid:-}" ] && kill "$discord_pid" 2>/dev/null
[ -n "${keyring_pid:-}" ] && kill "$keyring_pid" 2>/dev/null
[ -n "${bus_pid:-}" ] && kill "$bus_pid" 2>/dev/null
if [ -n "$session" ]; then
"$root/tools/nested-session.sh" stop "$session"
else
rm -rf "$runtime"
fi
}
trap cleanup EXIT
if [ -n "$visible" ]; then
runtime=$(mktemp -d /tmp/nacre-smoke-XXXXXX)
chmod 700 "$runtime"
# Share only the compositor socket. The window may take focus (D-045).
ln -s "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" "$runtime/wayland-0"
dispvars=(WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland GDK_BACKEND=wayland)
else
session=$out/session
"$root/tools/nested-session.sh" start "$session" "${SMOKE_SIZE:-1280x800}" >"$out/nested.log" 2>&1 \
|| { session=; echo "smoke: the nested session did not start (see $out/nested.log)" >&2; exit 1; }
# The nested session's runtime dir (its Wayland socket) is the run's XDG_RUNTIME_DIR.
runtime=$(cat "$session/runtime")
dispvars=($(source "$session/env"; for v in WAYLAND_DISPLAY XDG_SESSION_TYPE GDK_BACKEND LIBGL_ALWAYS_SOFTWARE \
WEBKIT_DMABUF_RENDERER_FORCE_SHM GSK_RENDERER GTK_A11Y NO_AT_BRIDGE GDK_DEBUG WL_INPUT_FLIP_Y NIRI_SOCKET; do echo "$v=${!v}"; done)
NACRE_WL_INPUT="$wl_input" NACRE_WL_OUTPUT=winit)
fi
if [ -n "${SMOKE_FAKE_DISCORD:-}" ]; then
: >"$out/discord.log"
python3 "$root/tests/smoke/net/fake-discord.py" "$runtime/discord-ipc-0" "$out/discord.log" &
discord_pid=$!
for _ in $(seq 50); do [ -S "$runtime/discord-ipc-0" ] && break; sleep 0.1; done
fi
# The run's own session bus (GTK registers the app on it), without service activation: no gvfs mounts, portals or
# the user's services; only the keyring with SMOKE_KEYRING.
cat >"$runtime/bus.conf" <<CONF
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig><type>session</type><listen>unix:path=$runtime/bus</listen>
<policy context="default"><allow send_destination="*" eavesdrop="true"/><allow eavesdrop="true"/><allow own="*"/></policy></busconfig>
CONF
env -i PATH="$PATH" HOME="$home" XDG_RUNTIME_DIR="$runtime" \
dbus-daemon --config-file="$runtime/bus.conf" --nofork --nopidfile >"$out/bus.log" 2>&1 &
bus_pid=$!
for _ in $(seq 50); do [ -S "$runtime/bus" ] && break; sleep 0.1; done
busvars=(DBUS_SESSION_BUS_ADDRESS="unix:path=$runtime/bus")
if [ -n "${SMOKE_KEYRING:-}" ]; then
mkdir -p "$home/.local/share"
# The login keyring's password is "smoke" (an empty one leaves no default collection).
printf 'smoke' | env -i PATH="$PATH" HOME="$home" XDG_DATA_HOME="$home/.local/share" XDG_RUNTIME_DIR="$runtime" \
"${busvars[@]}" gnome-keyring-daemon --foreground --unlock --components=secrets >"$out/keyring.log" 2>&1 &
keyring_pid=$!
for _ in $(seq 50); do
env "${busvars[@]}" dbus-send --session --dest=org.freedesktop.DBus --print-reply /org/freedesktop/DBus org.freedesktop.DBus.NameHasOwner \
string:org.freedesktop.secrets 2>/dev/null | grep -q true && break
sleep 0.1
done
fi
env -u WAYLAND_DISPLAY -u DISPLAY -u DBUS_SESSION_BUS_ADDRESS -u NACRE_HOME -u NIRI_SOCKET -u SWAYSOCK \
-u http_proxy -u https_proxy -u all_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY \
"${dispvars[@]}" "${busvars[@]}" \
HOME="$home" XDG_CONFIG_HOME="$home/.config" XDG_DATA_HOME="$home/.local/share" \
XDG_CACHE_HOME="$home/.cache" XDG_STATE_HOME="$home/.local/state" XDG_RUNTIME_DIR="$runtime" \
RUST_BACKTRACE=1 NACRE_SCRIPT="$out/script.txt" \
timeout -k 10 "${SMOKE_TIMEOUT:-180}" "$bin" ${SMOKE_ARGS:-} >"$out/nacre.log" 2>&1
code=$?
fail=0
if [ "$code" -ne 0 ]; then
echo "smoke: browser exited with $code$([ "$code" -eq 124 ] && echo ' (timeout)')"
fail=1
fi
if grep -q panicked "$out/nacre.log"; then
echo "smoke: panic in nacre.log:"
grep -m3 -A2 panicked "$out/nacre.log"
fail=1
fi
if grep -q "ASSERT FAILED" "$out/selftest.log" 2>/dev/null; then
echo "smoke: failed asserts:"
grep "ASSERT FAILED" "$out/selftest.log"
fail=1
fi
if [ ! -f "$out/done.json" ]; then
echo "smoke: script did not reach 'status done'"
fail=1
fi
echo "smoke: $([ $fail -eq 0 ] && echo ok || echo FAILED), results in $out"
exit $fail