Repository navigation
Expand file tree
/
Copy pathtest-e2e.sh
More file actions
executable file
·254 lines (238 loc) · 12 KB
/
Copy pathtest-e2e.sh
File metadata and controls
executable file
·254 lines (238 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env bash
# Runs the Playwright suite against a freshly started server.
#
# This is the entry point CI uses, and it is the one to use locally when you
# want the same conditions CI gets. Interactively, `playwright-test` from the
# e2e directory is still the way to run a single spec (see e2e/AGENTS.md).
#
# It does not build. Run ./build.sh first — and note that the binary embeds
# web/static/, so a server started from a stale binary serves stale assets and
# the suite then measures something other than what you edited. The freshness
# check below exists to make that impossible rather than merely unlikely.
set -euo pipefail
# Every path below is repo-relative, including the freshness check — without
# this, running from elsewhere fails with a "stale app.css" message that is
# actively misleading about what went wrong.
cd "$(dirname "${BASH_SOURCE[0]}")"
BINARY=./mynotes
PORT=8091
# The same binary in -demo-server mode, for the demo-only project in
# e2e/playwright.config.ts. A demo has no database and no REST API, so it cannot
# be a route on the server above — it is a different process with a different
# baseURL. 8092 continues the block this suite owns (MyCal 8089, MyMail 8090).
DEMO_PORT=8092
# A fresh database per run, in a directory this script owns and removes.
# Reusing one is how an "empty" run silently becomes a run against whatever the
# last one left behind.
DATA_DIR=$(mktemp -d)
SERVER_PID=
DEMO_PID=
cleanup() {
for pid in "$SERVER_PID" "$DEMO_PID"; do
if [ -n "$pid" ]; then
kill "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
fi
done
rm -rf "$DATA_DIR"
}
trap cleanup EXIT
# An untrapped fatal signal terminates bash *without* running the EXIT trap, so
# the server would survive the script and squat the port — the exact condition
# the pre-flight check below refuses to start on, meaning the next run fails
# rather than the current one. SIGPIPE is the candidate: piping this script into
# `head` or `grep -m1` closes stdout under it.
#
# Adopted from MyMail, which measured that leak. **It does not reproduce here**,
# and the honest version of that is worth writing down rather than transcribing
# their result: `2>&1 | head -5`, `2>&1 | grep -m1 passed`, `| head -1` and a
# mid-run SIGINT were each tried against this script and cleanup ran in all four,
# leaving nothing on the port. Only SIGKILL leaks, and no trap can catch that.
# Kept anyway — it is one line, it costs nothing, the general claim about
# untrapped signals is true whether or not this script's pipelines reach it, and
# the three suites should not differ here by accident.
#
# Re-raising via `exit` rather than adding the signals to the EXIT trap's list:
# listing them there runs cleanup twice, once per trap.
trap 'exit 1' INT TERM PIPE
if [ ! -x "$BINARY" ]; then
echo "$BINARY not found or not executable — run ./build.sh first" >&2
exit 1
fi
# Refuse to start if the port is already taken. Ours would exit on bind failure
# while the readiness probe below succeeded against whatever is squatting, and
# the suite would then run against a database and a binary that are not the ones
# under test. That happened in the sibling repo this script came from.
#
# Checked here rather than by watching our own process afterwards: a background
# process that has exited is a zombie until reaped, and `kill -0` succeeds on a
# zombie — so the obvious liveness check silently passes in exactly this case.
# (It was tried there, against a real squatter, and reported 48/48 green.)
for p in "$PORT" "$DEMO_PORT"; do
if (exec 3<>"/dev/tcp/127.0.0.1/${p}") 2>/dev/null; then
exec 3<&- 3>&-
echo "Something is already listening on port ${p}." >&2
echo "Stop it first — otherwise these tests would run against it, not against this build." >&2
exit 1
fi
done
# Keep -public-url aligned with the baseURL in e2e/playwright.config.ts.
# Host and CSRF share a policy; localhost at the listener port is always allowed.
# Foreign Hosts receive 421 even for native requests without Origin/Referer;
# browser writes from foreign origins receive 403.
#
# The server binds 127.0.0.1 (the -addr default) and everything here addresses
# `localhost`. On a host that resolves localhost to ::1 only, the readiness probe
# below fails and the run stops with "Server failed to start" — loud and wrong
# about the cause, but not silent. Kept as `localhost` because -public-url and the
# config's baseURL have to agree with each other, and they are shared with the
# sibling suites.
"$BINARY" -port "$PORT" -data "$DATA_DIR" -public-url "http://localhost:${PORT}" &
SERVER_PID=$!
# GET /api/v1/notes answers on an empty database ({"total":0,"notes":[]}), so
# this probes the real API rather than the SPA shell — which the server returns
# for any unmatched path and which would therefore go 200 before the database
# was open.
for i in $(seq 1 40); do
if curl -sf "http://localhost:${PORT}/api/v1/notes" > /dev/null 2>&1; then
break
fi
if [ "$i" -eq 40 ]; then
echo "Server failed to start on port ${PORT}" >&2
exit 1
fi
sleep 0.5
done
# Prove the server is serving the build under test. A green suite run against a
# stale binary is not evidence, and it fails in the reassuring direction: the
# tests pass, describing a version of the app that is not the one on disk.
#
# Every emitted script and stylesheet, not a sample of them. Hashing app.css and
# app.js alone looks sufficient and is not: tsc emits one module per source file
# and there are ~55 of them under web/static/, so editing views/NoteView.ts leaves
# BOTH of those files byte-identical and a two-file check passes a server serving
# 53 stale modules. A guard that samples the assets is not a guard against
# staleness; it is a guard against staleness in the two files nobody was going to
# edit alone.
#
# Three exclusions, all deliberate, because an undocumented one reads as an
# oversight and quietly narrows what "fresh" means here:
# * third_party/ is committed rather than emitted (see AGENTS.md — no package
# manager runs in this build), so it cannot go stale relative to a build.
# * public/ is not served as a static file at all. web/static/public/page.css
# is only ever delivered concatenated onto render/note.css, at
# /public/note.css — handled separately below, because a find that included
# it here would fetch a 404 (the /public/ catch-all) and fail for the wrong
# reason.
# * *.html is not matched by the find. web/static/index.html is served through
# buildIndexHTML, which may splice a window.__serverConfig <script> into it,
# so it is byte-identical to disk for some -public-url values and not others —
# a comparison that passes for a reason unrelated to freshness is worse than
# none. render/index.html has no such rewrite and IS checked, separately
# below, at the URL it is actually reachable from.
# The demo worker's output IS included: it is served the same way and goes stale
# the same way.
stale=0
checked=0
while IFS= read -r path; do
asset=${path#web/static/}
if ! served=$(curl -sf "http://localhost:${PORT}/${asset}" | md5sum | cut -d' ' -f1); then
echo "Could not fetch /${asset} from the test server." >&2
exit 1
fi
ondisk=$(md5sum "$path" | cut -d' ' -f1)
if [ "$served" != "$ondisk" ]; then
echo "Server is serving a stale ${asset} (served $served, on disk $ondisk)." >&2
stale=$((stale + 1))
fi
checked=$((checked + 1))
done < <(find web/static \( -name '*.js' -o -name '*.css' \) \
-not -path '*/third_party/*' -not -path 'web/static/public/*' | sort)
# The published-page stylesheet, which is the one asset with no 1:1 URL. main.go
# concatenates render/note.css + '\n' + public/page.css at startup and serves the
# result at /public/note.css, so this is the only way page.css's freshness is
# observable from outside the process — and it is embedded in the binary exactly
# like everything above, so it goes stale exactly like everything above.
if ! served=$(curl -sf "http://localhost:${PORT}/public/note.css" | md5sum | cut -d' ' -f1); then
echo "Could not fetch /public/note.css from the test server." >&2
exit 1
fi
ondisk=$({ cat web/static/render/note.css; printf '\n'; cat web/static/public/page.css; } \
| md5sum | cut -d' ' -f1)
if [ "$served" != "$ondisk" ]; then
echo "Server is serving a stale /public/note.css (served $served, on disk $ondisk)." >&2
stale=$((stale + 1))
fi
checked=$((checked + 1))
# The render kit's host page, served verbatim at /render/ — main.go registers it
# explicitly because http.FileServer canonicalises /render/index.html to /render/
# and would otherwise leave it unreachable, so /render/index.html is a 301 and
# this is the URL to hash. Worth its own line rather than left out with the other
# HTML: it carries a hand-maintained import map and a <meta> CSP hash that
# web/AGENTS.md warns must be kept in sync with web/static/index.html, and it is
# the file tools/dist-renderer.sh copies into native clients.
if ! served=$(curl -sf "http://localhost:${PORT}/render/" | md5sum | cut -d' ' -f1); then
echo "Could not fetch /render/ from the test server." >&2
exit 1
fi
ondisk=$(md5sum web/static/render/index.html | cut -d' ' -f1)
if [ "$served" != "$ondisk" ]; then
echo "Server is serving a stale /render/ host page (served $served, on disk $ondisk)." >&2
stale=$((stale + 1))
fi
checked=$((checked + 1))
# A find that matches nothing would report zero stale files and read as a pass —
# the empty-set failure this kind of guard has been bitten by before. Note the
# floor is not 0 but 2: the two explicit checks above always run, so `checked`
# is never 0 even when the find matches nothing.
if [ "$checked" -le 2 ]; then
echo "Freshness check found no assets to compare — is web/static/ built?" >&2
exit 1
fi
if [ "$stale" -gt 0 ]; then
echo "${stale} of ${checked} assets are stale." >&2
echo "The binary embeds web/static/ — rebuild with ./build.sh and try again." >&2
exit 1
fi
# The demo server, for the `chromium-demo` project. Started after the freshness
# check rather than before it: it is the same binary serving the same embedded
# web/static/, so the comparison above establishes both processes are fresh —
# and running the check twice would only prove the same thing twice.
#
# -public-url is what main.go derives the demo bundle's <base href> from, so it
# must match the demo project's baseURL for the same reason the real server's
# does. No -data: -demo-server opens no database at all.
"$BINARY" -demo-server -port "$DEMO_PORT" -public-url "http://localhost:${DEMO_PORT}" &
DEMO_PID=$!
# There is no /api/v1 here to probe — a demo answers that from a service worker
# inside the browser — so this probes the SPA shell, and specifically the
# injected window.__serverConfig that puts the frontend in demo mode. That makes
# it a correctness check as well as a readiness one: a shell served without the
# injection would come up as a normal build with no backend, and every test in
# the demo project would then fail for a reason that has nothing to do with what
# it asserts.
for i in $(seq 1 40); do
if curl -sf "http://localhost:${DEMO_PORT}/" 2>/dev/null | grep -q '__serverConfig'; then
break
fi
if [ "$i" -eq 40 ]; then
echo "Demo server failed to start on port ${DEMO_PORT}, or served a shell with no __serverConfig" >&2
exit 1
fi
sleep 0.5
done
cd e2e
# `playwright-test` is a local wrapper for exactly this command and is the
# interactive entry point e2e/AGENTS.md names; CI has no such wrapper, so fall
# through to the bin npm links. (Both @playwright/test and playwright declare a
# `playwright` bin, so which package npm links is a hoisting detail — but the
# bin is stable either way, which a hardcoded node_modules/playwright/cli.js
# path would not be.)
if command -v playwright-test > /dev/null 2>&1; then
playwright-test "$@"
elif [ -x ./node_modules/.bin/playwright ]; then
./node_modules/.bin/playwright test "$@"
else
echo "Playwright is not installed — run 'npm ci' in e2e/ first." >&2
exit 1
fi