forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-deployed-app.mjs
More file actions
347 lines (315 loc) · 13.9 KB
/
Copy pathcheck-deployed-app.mjs
File metadata and controls
347 lines (315 loc) · 13.9 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Loads the deployed app in a real browser and asks whether it draws a trail.
//
// Tier 2 of #431, tracked in #467. `pipeline/check_deployment.py` (tier 1)
// proves the bucket will answer a browser; only this proves the *app* works.
// They are different questions and both have failed independently:
//
// tier 1 can a browser REACH the data daily, no downloads
// tier 2 does the APP draw a trail with it daily, one real page load
//
// WHY A BROWSER AND NOT ANOTHER FETCH. #427 took the map down for eight days
// while every check stayed green, because none of them was a browser. Tier 1
// closed that by sending an `Origin` header. It still cannot see a build that
// shipped without its data URL, a service worker serving a stale shell, a
// bundle that throws before the first fetch, or a CORS rule that passes a
// bare GET and fails the app's real request. All of those are "the map is
// broken" and none of them is visible to curl.
//
// SILENCE IS NOT SUCCESS, and this is the trap the check is written around.
// #431 specifies the assertion as "the status strip does not say *No trail
// line*, and no data-error notice is showing". Both are NEGATIVE, and the app
// only raises them once a download has failed - so a build that never gets as
// far as requesting anything shows neither, and a purely negative check would
// call that healthy. `App.tsx` makes it explicit: `trailLinesMissing` is
// `!haveTrailLines && dataError !== null`, so with no error there is no flag,
// whether or not a trail exists.
//
// So the negative assertions are kept AND a positive one is added: the page
// must actually have received the trail artifact over the network. A CORS
// refusal surfaces here as a failed request, which is exactly the #427 shape.
import * as nodeFs from 'node:fs'
import { chromium } from 'playwright'
const DEFAULT_URL = 'https://ourhike.org/app/'
// Long, because this is a real page loading a real 12 MB artifact over a
// public CDN, and a slow morning is not an outage. The check reports what it
// saw either way; the timeout only bounds how long it waits to see it.
const LOAD_TIMEOUT_MS = 120_000
// The strings the app uses to say it is broken. Read from the rendered page
// rather than imported, deliberately: this runs against a DEPLOYED build,
// which may be older than this checkout, and importing the current source's
// copy would assert against a string that build never shipped.
const NO_TRAIL_FLAG = 'No trail line'
const argv = process.argv.slice(2)
const flag = (name, fallback) => {
const at = argv.indexOf(name)
return at === -1 ? fallback : argv[at + 1]
}
const url = flag('--url', process.env.APP_URL || DEFAULT_URL)
// Where the app is built to fetch its data from. Passed in rather than
// guessed so the check can report "requested nothing from the bucket" as a
// failure instead of silently having nothing to match against.
const dataBase = (flag('--data-base', process.env.DATA_BASE_URL || '') || '').replace(
/\/+$/,
'',
)
const jsonOut = flag('--json', null)
const exitZero = argv.includes('--exit-zero')
const report = []
const add = (check, state, detail) => report.push({ check, state, detail })
/**
* The artifact keys the client treats a 404 on as "this release has no such
* file" rather than as a failure.
*
* Read out of `lib/trailData.ts` and `lib/config.ts` rather than listed here.
* The check and the app have to agree about which downloads are optional, and
* a copy in this file is exactly the kind of second home that goes stale
* without anybody noticing - which is how a by-design 404 on
* `elevation_profile.json` was reported for three days as "the deployed app
* does not draw a trail" (#520).
*
* Throws rather than returning an empty list: a regex that quietly stopped
* matching would make every optional 404 look like an outage again, which is
* the bug this function exists to end.
*/
function optionalArtifactKeys() {
const { readFileSync } = nodeFs
const dir = new URL('../src/lib/', import.meta.url)
const trailData = readFileSync(new URL('trailData.ts', dir), 'utf8')
const config = readFileSync(new URL('config.ts', dir), 'utf8')
// `await` is what distinguishes a CALL from the declaration - without it the
// pattern also matches `async function fetchOptionalArtifact(artifactKey`
// and captures the parameter name, which resolves to no key and trips the
// guard below. Found by running it rather than by reading it.
const names = [...trailData.matchAll(/await fetchOptionalArtifact\(\s*(\w+)/g)].map(
(m) => m[1],
)
const keys = names
.map((name) => config.match(new RegExp(`${name}\\s*=\\s*'([^']+)'`))?.[1])
.filter(Boolean)
if (keys.length === 0 || keys.length !== names.length) {
throw new Error(
'could not read the optional-artifact contract out of src/lib/trailData.ts and src/lib/config.ts. ' +
'They have been restructured, and this check must be updated rather than left treating every ' +
'404 as an outage.',
)
}
return keys
}
const browser = await chromium.launch({
args: ['--no-sandbox'],
// Playwright pins a Chromium build per release and refuses to launch a
// different one. CI installs the matching build, which is the normal path;
// this override exists for an environment that already has a Chromium and
// cannot download (a sandbox with one preinstalled, a distro package).
// Absent means "use the one Playwright manages", which is the default.
...(process.env.CHROMIUM_EXECUTABLE_PATH
? { executablePath: process.env.CHROMIUM_EXECUTABLE_PATH }
: {}),
})
// A fresh context per run: no service worker, no IndexedDB, no stored
// preferences. The question is what a hiker opening this for the first time
// gets, and a warm cache would answer a different one - and would hide
// exactly the failure where the bucket is unreachable but a cached copy makes
// the screen look fine.
const context = await browser.newContext()
const page = await context.newPage()
// Everything the page asked the data bucket for, and how it went. A CORS
// refusal is a `requestfailed`, not a response - which is what makes this the
// assertion that would have caught #427 from the app's side.
const dataResponses = []
const failures = []
const consoleErrors = []
page.on('response', (response) => {
if (dataBase && response.url().startsWith(dataBase)) {
dataResponses.push({ url: response.url(), status: response.status() })
}
})
page.on('requestfailed', (request) => {
failures.push({
url: request.url(),
reason: request.failure()?.errorText ?? 'unknown',
})
})
page.on('pageerror', (error) => consoleErrors.push(String(error)))
try {
const response = await page.goto(url, {
waitUntil: 'domcontentloaded',
timeout: LOAD_TIMEOUT_MS,
})
const status = response?.status() ?? 0
add('loads', status === 200 ? 'ok' : 'failed', `${url} answered ${status}`)
// Onboarding stands between a first-time visitor and the map, so the check
// has to pass through it the way a hiker does.
//
// Several steps, and the control differs per step - "Skip" and "Continue"
// on the first, "Not now" on the location one. So this clicks whichever of
// them is on screen, in preference order, rather than naming one: the first
// version of this waited for "Not now", never found it on step one, and
// reported the whole flow skipped while quietly never reaching the map.
//
// "Skip"/"Not now" ahead of "Continue" because a headless browser has no
// location to grant and the map is what is being checked, not the
// permission prompt.
const ONBOARDING_CONTROLS = ['Skip', 'Not now', 'Continue']
// Wait for ANY of them to exist before asking which, because
// `locator.isVisible()` is an immediate check rather than an auto-waiting
// one - its `timeout` bounds resolving the locator, it does not wait for
// the element to appear. Measured: written that way this failed 1 run in 4,
// reporting "no onboarding control appeared" on a page that was simply
// still rendering. A health check that cries wolf one morning in four is
// the thing #431 spends its length warning against.
const anyControl = page.getByRole('button', { name: /^(Skip|Not now|Continue)$/ })
let steps = 0
for (let attempt = 0; attempt < 6; attempt += 1) {
await anyControl
.first()
.waitFor({ state: 'visible', timeout: attempt === 0 ? 30_000 : 3_000 })
.catch(() => {})
let clicked = false
for (const name of ONBOARDING_CONTROLS) {
const control = page.getByRole('button', { name, exact: true })
if (await control.isVisible().catch(() => false)) {
await control.click()
steps += 1
clicked = true
break
}
}
if (!clicked) break
}
add(
'onboarding',
steps > 0 ? 'ok' : 'failed',
steps > 0
? `cleared in ${steps} step(s)`
: `no onboarding control appeared (looked for ${ONBOARDING_CONTROLS.join(', ')}) - ` +
'the check never reached the map, so everything below it is about the wrong screen',
)
// The app fetches trail lines on its own once it believes it is online, so
// this waits for the network to settle rather than driving a download.
await page.waitForLoadState('networkidle', { timeout: LOAD_TIMEOUT_MS }).catch(() => {})
const body =
(await page
.locator('body')
.innerText()
.catch(() => '')) || ''
// --- The two negative assertions #431 asks for ---
add(
'no-trail-line-flag',
body.includes(NO_TRAIL_FLAG) ? 'failed' : 'ok',
body.includes(NO_TRAIL_FLAG)
? `the status strip says "${NO_TRAIL_FLAG}" - the app has data but no trail`
: 'the status strip does not claim the trail is missing',
)
const alerts = await page
.locator('[role="alert"]')
.allInnerTexts()
.catch(() => [])
const notices = alerts.map((text) => text.trim()).filter((text) => text !== '')
add(
'no-data-error',
notices.length === 0 ? 'ok' : 'failed',
notices.length === 0
? 'no data-error notice showing'
: `showing: ${notices.join(' | ')}`,
)
// --- The positive assertion, without which the two above mean nothing ---
if (!dataBase) {
add(
'fetched-data',
'skipped',
'no --data-base given, so nothing could be matched against the bucket',
)
} else {
const ok = dataResponses.filter((r) => r.status === 200 || r.status === 206)
const bad = dataResponses.filter((r) => r.status >= 400)
if (ok.length === 0) {
add(
'fetched-data',
'failed',
`the app requested nothing readable from ${dataBase}. This is the case the two ` +
'assertions above cannot see: with no request there is no error, and with no error ' +
'there is no flag.',
)
} else {
add('fetched-data', 'ok', `${ok.length} successful response(s) from the bucket`)
}
// A 404 on an OPTIONAL artifact is not an error, and treating it as one is
// how this check spent three days claiming the app did not draw a trail
// while it drew one perfectly (#520).
//
// `lib/trailData.ts` fetches `spurs.json` and `elevation_profile.json`
// through `fetchOptionalArtifact`, which returns null on 404 with the
// reasoning written out: "an artifact a release may predate […] a phone
// pointed at an older release should still get its trails and POIs rather
// than an error". The bucket genuinely does not publish
// `elevation_profile.json` today, so the app asks, gets a 404, and carries
// on exactly as designed.
//
// Read from the client's own source rather than restated here, for the
// reason verify_release.py gives about the same class of contract: a
// hand-kept copy is a second home for the fact, and this check exists
// because the two drifting apart is invisible from either side.
const optional = optionalArtifactKeys()
const expected = bad.filter(
(r) => r.status === 404 && optional.some((key) => r.url.endsWith(`/${key}`)),
)
const real = bad.filter((r) => !expected.includes(r))
if (real.length > 0) {
add('bucket-errors', 'failed', real.map((r) => `${r.status} ${r.url}`).join(', '))
} else if (expected.length > 0) {
// Said out loud rather than silently passed. The app working without it
// is by design; the artifact being absent is still worth somebody
// knowing, because it means a feature ships dark.
add(
'bucket-errors',
'noted',
`${expected.length} optional artifact(s) not published, which the app handles by design: ` +
expected.map((r) => r.url.split('/').pop()).join(', '),
)
}
}
const dataFailures = dataBase
? failures.filter((f) => f.url.startsWith(dataBase))
: failures
add(
'no-blocked-requests',
dataFailures.length === 0 ? 'ok' : 'failed',
dataFailures.length === 0
? 'nothing the page asked for was refused'
: dataFailures.map((f) => `${f.reason} ${f.url}`).join(', '),
)
// Reported, never failed. A third-party tile host having a moment is not an
// outage of ours, and #431 is explicit that it must not be able to declare
// one.
if (consoleErrors.length > 0) {
add('page-errors', 'noted', consoleErrors.slice(0, 3).join(' | '))
}
} catch (error) {
add('loads', 'failed', `${error.name}: ${error.message}`)
} finally {
await browser.close()
}
for (const line of report) {
console.log(
` ${line.state.toUpperCase().padEnd(8)} ${line.check.padEnd(20)} ${line.detail}`,
)
}
const failed = report.filter((line) => line.state === 'failed')
const verdict = {
checked_at: new Date().toISOString().slice(0, 10),
url,
data_base: dataBase || null,
checks: report,
failed,
}
if (jsonOut) {
const { writeFileSync } = await import('node:fs')
writeFileSync(jsonOut, `${JSON.stringify(verdict, null, 2)}\n`)
}
console.log(
failed.length === 0
? '\nThe deployed app loads its map data and does not report itself broken.'
: `\n${failed.length} check(s) failed.`,
)
process.exit(exitZero || failed.length === 0 ? 0 : 1)