1 #!/bin/bash
2 # Phase 7: the sample UI app, from a served build.
3 #
4 # The stdlib web framework's only real consumer in this workspace is the smesh
5 # app, and until this phase nothing in the gate ran it: phases 0a/0b/1/3/5 test
6 # the compiler, and 5 drives wasm fixtures through a browser but never the app.
7 # So a change that broke the framework's contract with a real UI would have left
8 # every gate green. The relay and the app are separate repos now (../morly and
9 # ../smesh): this phase builds BOTH from source with the compiler under test,
10 # has the relay serve this app's static tree, and runs the app's own smoke suite
11 # (test/test_app.py) which covers its main flows.
12 #
13 # FAIL-CLOSED. Every precondition below is a FAIL, not a SKIP:
14 #
15 # * the app repo is missing
16 # * the compiler under test is missing
17 # * the relay will not build, or the wasm bundle will not build
18 # * the relay does not come up, or pytest/selenium is absent
19 # * any smoke test fails
20 #
21 # A skip would report "not verified" as though it were "nothing to verify", and
22 # the whole point of the phase is that the UI is verified on every default run.
23 # This host has the browser stack (selenium + a chromium), so there is no
24 # exception to make here.
25 #
26 # Both build products go to a temp dir, so the app checkout is never touched:
27 # the relay binary, and a staged COPY of web/static that the bundles are built
28 # into. The app TRACKS its bundles as build products, so building in place
29 # rewrote nine tracked binaries on every run and left the app repo dirty for a
30 # reason the app never asked for. Staging keeps the finding (did the bundles
31 # build and serve under this compiler?) without writing to a sibling repo.
32 set -uo pipefail
33 34 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
35 ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
36 source "$SCRIPT_DIR/util.sh"
37 38 echo ""
39 echo "=== Phase 7: UI app smoke (served build) ==="
40 41 # The app lives beside this checkout in the workspace. MOXIE_UI_APP overrides it
42 # for a different layout; the default is the workspace's own arrangement, not a
43 # guess about an installed tree.
44 APP="${MOXIE_UI_APP:-$(cd "$ROOT/.." && pwd)/smesh}"
45 MOXIE="${MOXIE:-$ROOT/moxie}"
46 SUITE_TIMEOUT="${UI_SUITE_TIMEOUT:-600}"
47 48 fail() {
49 log_fail "ui:${1}" "$2"
50 exit 1
51 }
52 53 # The app checks in its built bundles under web/static. Building them from the
54 # compiler under test is the point of the phase, so the whole static tree is
55 # copied first and every build output lands in the copy.
56 STAGE="$(tempdir ui)/static"
57 [ -d "$APP" ] || fail "preconditions" "app repo not found at $APP (set MOXIE_UI_APP)"
58 [ -f "$APP/test/test_app.py" ] || fail "preconditions" "no smoke suite at $APP/test/test_app.py"
59 [ -x "$MOXIE" ] || fail "preconditions" "compiler under test not executable: $MOXIE"
60 command -v python3 >/dev/null 2>&1 || fail "preconditions" "python3 not found"
61 python3 -c 'import selenium, pytest, websocket' >/dev/null 2>&1 \
62 || fail "preconditions" "python3 is missing selenium/pytest/websocket-client"
63 python3 -c 'import playwright' >/dev/null 2>&1 && : # playwright is for other phases; not required here
64 65 cp -a "$APP/web/static" "$STAGE" || fail "preconditions" "could not stage $APP/web/static"
66 67 # ── build the relay and the app bundle from source with the compiler under test
68 BUILD_LOG="$(tempdir ui)/build.log"
69 rm -rf "$BUILD_LOG"
70 71 # The relay is its OWN repo (RLY, default ../morly): the app is the front end and
72 # the relay lives elsewhere, so this phase builds the relay from THAT source and
73 # points it at this checkout's static tree. A relay build failure is a FAIL like
74 # any other: the earlier arrangement tried the build in the app repo, reported
75 # the outcome, and quietly served a prebuilt binary when it failed, which is
76 # exactly the state a broken compiler would keep green.
77 RLY="${MOXIE_RELAY_REPO:-$(cd "$ROOT/.." && pwd)/morly}"
78 [ -d "$RLY" ] || fail "preconditions" "relay repo not found at $RLY (set MOXIE_RELAY_REPO)"
79 80 # The app repo must not carry relay source. The relay was carved out to $RLY and
81 # its copy stayed behind in the app for two weeks, which is how a whole relay
82 # port landed in the wrong repo: the source was sitting there, so building the
83 # relay from the app looked like the right thing. A relay package, main.mx or
84 # cmd/ in the app tree is that mistake coming back, and this phase is where it
85 # has to show, because this is the phase that builds both sides.
86 for f in pkg main.mx cmd; do
87 if [ -e "$APP/$f" ]; then
88 fail "preconditions" "$APP/$f exists - the relay's source belongs in $RLY, not the app repo"
89 fi
90 done
91 RELAY_BIN="$(tempdir ui)/morly"
92 if ! (cd "$RLY" && MOXIEROOT="$ROOT" "$MOXIE" build -o "$RELAY_BIN" . >"$BUILD_LOG" 2>&1); then
93 fail "relay-build" "$(grep -m1 -oE '^; .*|mxc: .*' "$BUILD_LOG" | tail -1 | cut -c1-160)"
94 fi
95 [ -x "$RELAY_BIN" ] || fail "relay-build" "the relay build produced no binary"
96 relay_note="relay built from source at $RLY"
97 98 bundles=0
99 build_failed=""
100 for b in app signer relay-proxy store verify profile notif feed mls; do
101 [ -d "$APP/web/wasm/$b" ] || continue
102 if ! (cd "$APP" && MOXIEROOT="$ROOT" "$MOXIE" build -allow-closure-captures \
103 -target wasm -o "$STAGE/$b.wasm" "./web/wasm/$b/" \
104 >>"$BUILD_LOG" 2>&1); then
105 build_failed="bundle $b: $(grep -m1 -oE '^; .*|mxc: .*' "$BUILD_LOG" | tail -1)"
106 break
107 fi
108 bundles=$((bundles+1))
109 done
110 111 if [ -n "$build_failed" ]; then
112 fail "build" "$build_failed"
113 fi
114 115 # A served build needs the module AND the staged runtime beside it; a build that
116 # emitted the module but not $runtime/bridge-common.mjs would serve a page that
117 # never boots, and this is the phase that should catch that.
118 [ -s "$STAGE/app.wasm" ] || fail "build" "app.wasm was not produced"
119 [ -d "$STAGE/\$runtime" ] || fail "build" "the runtime staging (\$runtime) is missing"
120 [ -s "$STAGE/bridge-common.mjs" ] || fail "build" "bridge-common.mjs is missing"
121 log_pass "ui:build" "$bundles bundles + runtime staging; $relay_note"
122 123 # ── run the app's own smoke suite; it starts and stops the relay itself
124 PY_LOG="$(tempdir ui)/pytest.log"
125 rm -rf "$PY_LOG"
126 127 # The relay the suite starts is a Moxie SPAWN TREE: the binary forks one domain
128 # per worker, and `timeout` (or a kill) ends pytest without ending them. Every
129 # aborted or timed-out run therefore left a live relay behind - measured on the
130 # developer machine: a dozen morly processes still resident after one run was
131 # interrupted, and they are what makes the phase look like it is eating memory.
132 # The phase asked for that process, so the phase ends it, whatever the suite's
133 # own teardown managed, and says so when it finds any left.
134 relay_pids() { pgrep -f -- "$RELAY_BIN" 2>/dev/null || true; }
135 stop_relay() {
136 local pids n try
137 pids="$(relay_pids)"
138 [ -n "$pids" ] || return 0
139 n=$(printf '%s\n' "$pids" | wc -l)
140 kill $pids 2>/dev/null || true
141 try=0
142 while [ "$try" -lt 25 ]; do
143 sleep 0.2
144 pids="$(relay_pids)"
145 [ -n "$pids" ] || break
146 try=$((try + 1))
147 done
148 if [ -n "$pids" ]; then
149 kill -9 $pids 2>/dev/null || true
150 fi
151 printf ' stopped %s relay process(es) the suite left running\n' "$n"
152 }
153 trap stop_relay EXIT
154 155 (cd "$APP" && SMESH_RELAY_BIN="$RELAY_BIN" \
156 ORLY_STATIC_DIR="$STAGE" \
157 timeout "$SUITE_TIMEOUT" python3 -m pytest test/test_app.py -q) >"$PY_LOG" 2>&1
158 rc=$?
159 stop_relay
160 161 summary="$(grep -E '^[0-9]+ (passed|failed)|passed|failed' "$PY_LOG" | tail -1)"
162 if [ "$rc" = 0 ]; then
163 log_pass "ui:smoke" "${summary:-passed}"
164 else
165 log_fail "ui:smoke" "$(tail -6 "$PY_LOG" | tr '\n' ' ')"
166 fi
167 168 exit "$rc"
169