trykeyline-browser 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/audit/probe.mjs +390 -0
- package/audit.mjs +209 -0
- package/bin/trykeyline-browser.mjs +79 -0
- package/browse/args.mjs +44 -0
- package/browse/capture.mjs +181 -0
- package/browse/compare.mjs +260 -0
- package/browse/findings.mjs +125 -0
- package/browse/playwright.mjs +78 -0
- package/browse/report.mjs +265 -0
- package/browse/routes.mjs +252 -0
- package/browse/serve.mjs +61 -0
- package/browse/walk.mjs +117 -0
- package/browse.mjs +175 -0
- package/package.json +53 -4
- package/probe.mjs +26 -0
- package/smoke/clicks.mjs +320 -0
- package/smoke/config.mjs +184 -0
- package/smoke/forms.mjs +329 -0
- package/smoke/keyboard.mjs +100 -0
- package/smoke/links.mjs +120 -0
- package/smoke/page.mjs +575 -0
- package/smoke/phone.mjs +126 -0
- package/smoke/shift.mjs +103 -0
- package/smoke/util.mjs +113 -0
- package/smoke.mjs +412 -0
package/smoke/forms.mjs
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Submit every form three ways, each from a fresh load: empty, with one value
|
|
3
|
+
* the field can't accept, and filled in properly. The first two must say what's
|
|
4
|
+
* wrong, next to the field; the last must show that it worked.
|
|
5
|
+
*
|
|
6
|
+
* A submit is a write. Forms run only on a test host, and a form whose words
|
|
7
|
+
* say delete, pay, unsubscribe and the like only with --allow-writes. Search
|
|
8
|
+
* forms only read, so they run anywhere, and only filled in.
|
|
9
|
+
*/
|
|
10
|
+
import { add } from "../browse/findings.mjs";
|
|
11
|
+
import { shotKey } from "../browse/capture.mjs";
|
|
12
|
+
import { isDestructive } from "./config.mjs";
|
|
13
|
+
import { PAGE } from "./page.mjs";
|
|
14
|
+
import { normalUrl, open, openStep, pathOf, sleep } from "./util.mjs";
|
|
15
|
+
|
|
16
|
+
const TEXTLIKE = new Set(["text", "email", "url", "tel", "password", "search", "number", "textarea", ""]);
|
|
17
|
+
const ERROR_WORDS = /\b(error|invalid|required|failed|wrong|try again|couldn.t|can.t be|cannot be|must|missing|not valid|please (enter|fill|provide|choose))\b/i;
|
|
18
|
+
|
|
19
|
+
function today(offset = 0) {
|
|
20
|
+
const d = new Date(Date.now() + offset);
|
|
21
|
+
return d.toISOString().slice(0, 10);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A value a reasonable person would type into this field. */
|
|
25
|
+
export function validValue(f) {
|
|
26
|
+
const hint = `${f.name ?? ""} ${f.label ?? ""}`.toLowerCase();
|
|
27
|
+
let v;
|
|
28
|
+
switch (f.type) {
|
|
29
|
+
case "email":
|
|
30
|
+
v = "keyline.smoke@example.test";
|
|
31
|
+
break;
|
|
32
|
+
case "url":
|
|
33
|
+
v = "https://example.test";
|
|
34
|
+
break;
|
|
35
|
+
case "tel":
|
|
36
|
+
v = "5555550123";
|
|
37
|
+
break;
|
|
38
|
+
case "number":
|
|
39
|
+
case "range":
|
|
40
|
+
v = f.min != null && f.min !== "" ? String(f.min) : f.max != null && f.max !== "" ? String(Math.min(1, Number(f.max))) : "1";
|
|
41
|
+
break;
|
|
42
|
+
case "date":
|
|
43
|
+
v = today();
|
|
44
|
+
break;
|
|
45
|
+
case "datetime-local":
|
|
46
|
+
v = `${today()}T12:00`;
|
|
47
|
+
break;
|
|
48
|
+
case "time":
|
|
49
|
+
v = "12:00";
|
|
50
|
+
break;
|
|
51
|
+
case "month":
|
|
52
|
+
v = today().slice(0, 7);
|
|
53
|
+
break;
|
|
54
|
+
case "color":
|
|
55
|
+
v = "#336699";
|
|
56
|
+
break;
|
|
57
|
+
case "password":
|
|
58
|
+
v = "Keyline-smoke-1";
|
|
59
|
+
break;
|
|
60
|
+
case "search":
|
|
61
|
+
v = "keyline";
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
if (/e-?mail/.test(hint)) v = "keyline.smoke@example.test";
|
|
65
|
+
else if (/phone|mobile|tel/.test(hint)) v = "5555550123";
|
|
66
|
+
else if (/zip|postal|post code/.test(hint)) v = "94107";
|
|
67
|
+
else if (/url|website|link/.test(hint)) v = "https://example.test";
|
|
68
|
+
else if (/first/.test(hint)) v = "Keyline";
|
|
69
|
+
else if (/last|surname/.test(hint)) v = "Tester";
|
|
70
|
+
else if (/name/.test(hint)) v = "Keyline Tester";
|
|
71
|
+
else if (/city|town/.test(hint)) v = "Springfield";
|
|
72
|
+
else if (f.tag === "textarea") v = "A message from Keyline's smoke test.";
|
|
73
|
+
else v = "Keyline smoke test";
|
|
74
|
+
}
|
|
75
|
+
if (f.minLength && v.length < f.minLength) v = v.padEnd(f.minLength, "x");
|
|
76
|
+
return v;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A value this field should refuse, or null when it has no rule to break. */
|
|
80
|
+
export function invalidValue(f) {
|
|
81
|
+
const hint = `${f.name ?? ""} ${f.label ?? ""}`.toLowerCase();
|
|
82
|
+
if (f.type === "email" || (f.type === "text" && /e-?mail/.test(hint))) return { value: "not-an-email", why: "not an email address" };
|
|
83
|
+
if (f.type === "url") return { value: "not a url", why: "not a URL" };
|
|
84
|
+
if (f.type === "number" && f.min != null && f.min !== "") return { value: String(Number(f.min) - 1), why: `below the minimum, ${f.min}` };
|
|
85
|
+
if (f.type === "number" && f.max != null && f.max !== "") return { value: String(Number(f.max) + 1), why: `above the maximum, ${f.max}` };
|
|
86
|
+
if (f.pattern) return { value: "!!", why: `doesn't match the pattern ${f.pattern}` };
|
|
87
|
+
if (f.minLength > 1) return { value: "a", why: `shorter than ${f.minLength} characters` };
|
|
88
|
+
if (f.type === "tel" || /phone|mobile/.test(hint)) return { value: "not a number", why: "not a phone number" };
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const isSearch = (form) => form.role === "search" || form.fields.some((f) => f.type === "search") || (form.fields.length === 1 && /^(q|query|search|s|keywords?)$/i.test(form.fields[0].name ?? ""));
|
|
93
|
+
|
|
94
|
+
async function fill(page, fields, values) {
|
|
95
|
+
for (const f of fields) {
|
|
96
|
+
if (!f.visible) continue;
|
|
97
|
+
const loc = page.locator(f.sel).first();
|
|
98
|
+
const v = values.get(f);
|
|
99
|
+
try {
|
|
100
|
+
if (f.tag === "select") {
|
|
101
|
+
if (v === "") await loc.selectOption({ index: 0 }, { timeout: 1500 });
|
|
102
|
+
else {
|
|
103
|
+
const count = await loc.locator("option").count();
|
|
104
|
+
await loc.selectOption({ index: count > 1 ? 1 : 0 }, { timeout: 1500 });
|
|
105
|
+
}
|
|
106
|
+
} else if (f.type === "checkbox") {
|
|
107
|
+
if (v === "") await loc.uncheck({ timeout: 1500 });
|
|
108
|
+
else if (f.required) await loc.check({ timeout: 1500 });
|
|
109
|
+
} else if (f.type === "radio") {
|
|
110
|
+
if (v !== "" && f.required) await loc.check({ timeout: 1500 });
|
|
111
|
+
} else if (f.type === "file" || f.type === "range") {
|
|
112
|
+
// Left as it is.
|
|
113
|
+
} else if (TEXTLIKE.has(f.type) || f.tag === "textarea" || v != null) {
|
|
114
|
+
await loc.fill(v ?? "", { timeout: 1500 });
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
// A field that won't take input (read-only, custom widget) keeps its value.
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Submit one way and see what the page says. */
|
|
123
|
+
async function attempt(context, { base, route, form, values, quiet }) {
|
|
124
|
+
const { page, log } = await open(context, { base, route });
|
|
125
|
+
const origin = new URL(base).origin;
|
|
126
|
+
const requests = [];
|
|
127
|
+
const failures = [];
|
|
128
|
+
let dialog = null;
|
|
129
|
+
page.on("request", (req) => {
|
|
130
|
+
if (req.method() !== "GET" || (req.resourceType() === "document" && req.url() !== page.url())) requests.push(`${req.method()} ${pathOf(req.url(), base)}`);
|
|
131
|
+
});
|
|
132
|
+
page.on("response", (res) => {
|
|
133
|
+
const m = res.request().method();
|
|
134
|
+
if (res.status() >= 400 && (m !== "GET" || res.request().resourceType() === "document") && res.url().startsWith(origin)) failures.push(`${m} ${pathOf(res.url(), base)} → ${res.status()}`);
|
|
135
|
+
});
|
|
136
|
+
page.on("dialog", (d) => {
|
|
137
|
+
dialog = `${d.type()}: ${d.message().slice(0, 120)}`;
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
if (!(await page.locator(form.sel).count())) return { missing: true };
|
|
141
|
+
await fill(page, form.fields, values);
|
|
142
|
+
await page.evaluate(PAGE.listenInvalid, { formSel: form.sel });
|
|
143
|
+
const before = await page.evaluate(PAGE.textSnapshot, { formSel: form.sel });
|
|
144
|
+
await page.evaluate(() => (window.__klFormMark = true));
|
|
145
|
+
const errorsBefore = log.pageErrors.length;
|
|
146
|
+
if (form.submit) await page.locator(form.submit.sel).first().click({ timeout: 2500 }).catch(() => page.evaluate(PAGE.requestSubmit, { formSel: form.sel }));
|
|
147
|
+
else await page.evaluate(PAGE.requestSubmit, { formSel: form.sel });
|
|
148
|
+
|
|
149
|
+
const t0 = Date.now();
|
|
150
|
+
while (Date.now() - t0 < quiet) {
|
|
151
|
+
await sleep(150);
|
|
152
|
+
if (requests.length || dialog || normalUrl(page.url()) !== normalUrl(before.url)) break;
|
|
153
|
+
}
|
|
154
|
+
await page.waitForLoadState("load", { timeout: 8000 }).catch(() => {});
|
|
155
|
+
await page.waitForLoadState("networkidle", { timeout: 3000 }).catch(() => {});
|
|
156
|
+
await sleep(400);
|
|
157
|
+
const reloaded = !(await page.evaluate(() => window.__klFormMark === true).catch(() => false));
|
|
158
|
+
const native = reloaded ? [] : await page.evaluate(PAGE.readInvalid).catch(() => []);
|
|
159
|
+
const after = await page.evaluate(PAGE.textSnapshot, { formSel: form.sel }).catch(() => null);
|
|
160
|
+
const had = new Set(before.items.map((t) => t.text));
|
|
161
|
+
const fresh = (after?.items ?? []).filter((t) => !had.has(t.text));
|
|
162
|
+
const near = (item) =>
|
|
163
|
+
before.fieldBoxes.some((b) => item.box.y < b.y + b.h + 160 && item.box.y + item.box.h > b.y - 80 && item.box.x < b.x + b.w + 400 && item.box.x + item.box.w > b.x - 400) ||
|
|
164
|
+
(form.box && item.box.y >= form.box.y - 40 && item.box.y <= form.box.y + form.box.h + 80);
|
|
165
|
+
return {
|
|
166
|
+
native,
|
|
167
|
+
fresh,
|
|
168
|
+
freshNear: fresh.filter(near),
|
|
169
|
+
freshFar: fresh.filter((t) => !near(t)),
|
|
170
|
+
ariaInvalid: after?.invalid ?? [],
|
|
171
|
+
moved: normalUrl(page.url()) !== normalUrl(before.url) || reloaded,
|
|
172
|
+
url: page.url(),
|
|
173
|
+
requests,
|
|
174
|
+
failures,
|
|
175
|
+
dialog,
|
|
176
|
+
errors: log.pageErrors.slice(errorsBefore).map((e) => e.text),
|
|
177
|
+
reset: after && after.formConnected && after.values !== before.values,
|
|
178
|
+
gone: after && !after.formConnected,
|
|
179
|
+
};
|
|
180
|
+
} finally {
|
|
181
|
+
await page.close().catch(() => {});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function checkForms({ run, context, base, route, inventory, settings, policy, skipped, log, find }) {
|
|
186
|
+
const forms = (inventory?.forms ?? []).filter((f) => f.visible && f.fields.some((x) => x.visible)).slice(0, 4);
|
|
187
|
+
if (!forms.length) return;
|
|
188
|
+
log(` forms ${route} ${forms.length}`);
|
|
189
|
+
for (const form of forms) {
|
|
190
|
+
const search = isSearch(form);
|
|
191
|
+
const label = form.name ? `the “${form.name}” form` : form.submit?.text ? `the form sent with “${form.submit.text}”` : "the form";
|
|
192
|
+
const destructive = isDestructive(form.name, form.submit?.text, form.text.slice(0, 120)) || isDestructive(pathOf(form.action, base).replace(/[/_-]/g, " "));
|
|
193
|
+
const why = search && !destructive ? null : policy.blocks({ write: true, destructive });
|
|
194
|
+
if (why) {
|
|
195
|
+
skipped.push({ what: `${label} on ${route}`, why });
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const visibleFields = form.fields.filter((f) => f.visible);
|
|
199
|
+
const press = `Click “${form.submit?.text || "Submit"}”`;
|
|
200
|
+
const at = {
|
|
201
|
+
page: route,
|
|
202
|
+
viewport: "desktop",
|
|
203
|
+
theme: "light",
|
|
204
|
+
shot: shotKey("desktop", "light"),
|
|
205
|
+
box: form.box,
|
|
206
|
+
selector: form.sel,
|
|
207
|
+
...(find?.({ sel: form.sel, text: form.submit?.text }) ?? {}),
|
|
208
|
+
};
|
|
209
|
+
const steps = (...s) => [openStep(route), ...s];
|
|
210
|
+
run.stats.forms++;
|
|
211
|
+
|
|
212
|
+
// 1 and 2: wrong input should be refused, out loud, by the field.
|
|
213
|
+
const tries = [];
|
|
214
|
+
if (!search) {
|
|
215
|
+
tries.push({ how: "empty", values: new Map(visibleFields.map((f) => [f, ""])), steps: ["Leave every field empty", press], required: visibleFields.some((f) => f.required) });
|
|
216
|
+
const bad = visibleFields.map((f) => [f, invalidValue(f)]).find(([, v]) => v);
|
|
217
|
+
if (bad) {
|
|
218
|
+
const [field, inv] = bad;
|
|
219
|
+
const values = new Map(visibleFields.map((f) => [f, f === field ? inv.value : validValue(f)]));
|
|
220
|
+
tries.push({ how: "invalid", values, steps: [`Fill the form properly, except “${field.label || field.name}”: type “${inv.value}” (${inv.why})`, press], required: true });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
for (const t of tries) {
|
|
224
|
+
const r = await attempt(context, { base, route, form, values: t.values, quiet: settings.quietMs });
|
|
225
|
+
if (r.missing) break;
|
|
226
|
+
if (r.native.length || r.freshNear.length) continue; // the browser's message, or the app's, by the field
|
|
227
|
+
if (r.freshFar.length || r.dialog) {
|
|
228
|
+
const where = r.dialog ? `a browser ${r.dialog}` : `“${r.freshFar[0].text}”`;
|
|
229
|
+
add(run, {
|
|
230
|
+
...at,
|
|
231
|
+
rule: "flow/validation-far",
|
|
232
|
+
severity: "warning",
|
|
233
|
+
title: `${cap(label)} reports ${t.how === "empty" ? "missing" : "wrong"} input away from the field`,
|
|
234
|
+
evidence: `the only message is ${where}`,
|
|
235
|
+
detail: "The message exists but not where the eye is. Put it under the field it's about, and point the field at it with aria-describedby.",
|
|
236
|
+
fix: "/keyline patterns",
|
|
237
|
+
steps: steps(...t.steps, `The message appears at ${r.dialog ? "an alert" : r.freshFar[0].sel}, away from the field`),
|
|
238
|
+
});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (r.ariaInvalid.length) {
|
|
242
|
+
add(run, {
|
|
243
|
+
...at,
|
|
244
|
+
rule: "flow/silent-validation",
|
|
245
|
+
severity: "warning",
|
|
246
|
+
title: `${cap(label)} marks fields invalid without saying why`,
|
|
247
|
+
evidence: `aria-invalid on ${r.ariaInvalid.slice(0, 3).join(", ")}; no message appears`,
|
|
248
|
+
detail: "A red border alone doesn't tell anyone what to fix, and colour isn't seen by everyone. Add the message beside the field.",
|
|
249
|
+
fix: "/keyline patterns",
|
|
250
|
+
steps: steps(...t.steps, "The field turns invalid but no words say why"),
|
|
251
|
+
});
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (r.moved || r.requests.length) {
|
|
255
|
+
if (!t.required) continue; // nothing was required, so sending it is fine
|
|
256
|
+
add(run, {
|
|
257
|
+
...at,
|
|
258
|
+
rule: "flow/accepts-invalid",
|
|
259
|
+
severity: "warning",
|
|
260
|
+
title: `${cap(label)} sends ${t.how === "empty" ? "empty required fields" : "a value it should refuse"}`,
|
|
261
|
+
evidence: r.requests[0] ?? `moved to ${pathOf(r.url, base)}`,
|
|
262
|
+
detail: "Nothing stops the submit, and nothing comes back by the field. Validate before sending, and show the server's answer next to the field it's about.",
|
|
263
|
+
fix: "/keyline patterns",
|
|
264
|
+
steps: steps(...t.steps, r.requests[0] ? `It sends ${r.requests[0]} with no message` : "The page moves on with no message"),
|
|
265
|
+
});
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
add(run, {
|
|
269
|
+
...at,
|
|
270
|
+
rule: "flow/silent-validation",
|
|
271
|
+
severity: "error",
|
|
272
|
+
title: `${cap(label)} refuses ${t.how === "empty" ? "an empty submit" : "a wrong value"} in silence`,
|
|
273
|
+
evidence: `no message, no aria-invalid, no request within ${settings.quietMs / 1000} s`,
|
|
274
|
+
detail: "The submit does nothing and says nothing: the person can't tell whether it broke or what to change. Show what's wrong under each field.",
|
|
275
|
+
fix: "/keyline patterns",
|
|
276
|
+
steps: steps(...t.steps, "Nothing appears: no error, no change"),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 3: done right, it should visibly work.
|
|
281
|
+
if (visibleFields.some((f) => f.type === "password")) {
|
|
282
|
+
skipped.push({ what: `a proper submit of ${label} on ${route}`, why: "it takes a password; sign-in is set up in keyline.smoke.json, not guessed" });
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (visibleFields.some((f) => f.type === "file" && f.required)) continue;
|
|
286
|
+
const values = new Map(visibleFields.map((f) => [f, validValue(f)]));
|
|
287
|
+
const r = await attempt(context, { base, route, form, values, quiet: settings.quietMs });
|
|
288
|
+
if (r.missing) continue;
|
|
289
|
+
const fillStep = search ? `Type “${validValue(visibleFields[0])}”` : "Fill every field with a plausible value";
|
|
290
|
+
if (r.failures.length || r.errors.length) {
|
|
291
|
+
add(run, {
|
|
292
|
+
...at,
|
|
293
|
+
rule: "flow/submit-failed",
|
|
294
|
+
severity: "error",
|
|
295
|
+
title: `${cap(label)} fails when filled in properly`,
|
|
296
|
+
evidence: r.failures[0] ?? r.errors[0],
|
|
297
|
+
steps: steps(fillStep, press, r.failures[0] ? `The request fails: ${r.failures[0]}` : `An exception: ${r.errors[0]}`),
|
|
298
|
+
});
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
const refusal = [...r.freshNear, ...r.freshFar].find((t) => ERROR_WORDS.test(t.text));
|
|
302
|
+
if (refusal && !r.moved) {
|
|
303
|
+
add(run, {
|
|
304
|
+
...at,
|
|
305
|
+
rule: "flow/submit-refused",
|
|
306
|
+
severity: "info",
|
|
307
|
+
title: `${cap(label)} refuses a plausible submission`,
|
|
308
|
+
evidence: `“${refusal.text}”`,
|
|
309
|
+
detail: "Keyline's test values were turned down. That may be right (a real account, a unique name), or the rule may be stricter than its label says.",
|
|
310
|
+
steps: steps(fillStep, press, `It answers “${refusal.text}”`),
|
|
311
|
+
});
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (r.native.length) continue; // the browser refused one of Keyline's values (a pattern it couldn't guess): no verdict
|
|
315
|
+
if (r.moved || r.fresh.length || r.dialog || r.reset || r.gone) continue;
|
|
316
|
+
add(run, {
|
|
317
|
+
...at,
|
|
318
|
+
rule: r.requests.length ? "flow/silent-success" : "flow/dead-submit",
|
|
319
|
+
severity: r.requests.length ? "warning" : "error",
|
|
320
|
+
title: r.requests.length ? `${cap(label)} gives no sign it worked` : `Submitting ${label} does nothing`,
|
|
321
|
+
evidence: r.requests.length ? `${r.requests[0]} succeeded; the page didn't change` : `no request, no message, no change within ${settings.quietMs / 1000} s`,
|
|
322
|
+
detail: r.requests.length ? "The data was sent, but the person sees the same form and will likely send it again. Confirm it: a message, a cleared form, or the next page." : "The submit is wired to nothing.",
|
|
323
|
+
fix: r.requests.length ? "/keyline patterns" : undefined,
|
|
324
|
+
steps: steps(fillStep, press, "Nothing on the page changes"),
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const cap = (s) => s[0].toUpperCase() + s.slice(1);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tab through a page from the top, as a keyboard user does, until focus leaves
|
|
3
|
+
* the page. Along the way: does each stop show that it has focus, is it on
|
|
4
|
+
* screen at all, and does focus ever loop back on itself before the end
|
|
5
|
+
* (a trap)? A loop inside an open modal dialog is what a modal should do; a
|
|
6
|
+
* loop anywhere else is a trap.
|
|
7
|
+
*/
|
|
8
|
+
import { add } from "../browse/findings.mjs";
|
|
9
|
+
import { shotKey } from "../browse/capture.mjs";
|
|
10
|
+
import { PAGE } from "./page.mjs";
|
|
11
|
+
import { open, openStep } from "./util.mjs";
|
|
12
|
+
|
|
13
|
+
const name = (f) => (f.text ? `“${f.text}”` : f.sel);
|
|
14
|
+
|
|
15
|
+
export async function checkKeyboard({ run, context, base, route, log, find }) {
|
|
16
|
+
const { page, error } = await open(context, { base, route });
|
|
17
|
+
try {
|
|
18
|
+
if (error) return;
|
|
19
|
+
const prep = await page.evaluate(PAGE.keyboardPrep);
|
|
20
|
+
const budget = Math.min(prep.total + 8, 160);
|
|
21
|
+
log(` keys ${route} ${prep.total} focusable`);
|
|
22
|
+
const order = [];
|
|
23
|
+
const invisible = new Map();
|
|
24
|
+
const hidden = new Map();
|
|
25
|
+
let trap = null;
|
|
26
|
+
let presses = 0;
|
|
27
|
+
for (; presses < budget; presses++) {
|
|
28
|
+
await page.keyboard.press("Tab");
|
|
29
|
+
const f = await page.evaluate(PAGE.keyboardRead).catch(() => null);
|
|
30
|
+
if (!f) break; // Tab moved to another document
|
|
31
|
+
if (f.body) {
|
|
32
|
+
if (order.length) break; // a full lap: focus left the page
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const prev = order.findIndex((o) => o.id === f.id);
|
|
36
|
+
if (prev >= 0) {
|
|
37
|
+
if (f.iframe && order.at(-1)?.id === f.id) continue; // moving through a frame's own controls
|
|
38
|
+
const cycle = order.slice(prev);
|
|
39
|
+
if (cycle.every((o) => o.inModal)) break; // an open modal keeps focus, as it should
|
|
40
|
+
trap = { cycle, at: prev };
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
order.push(f);
|
|
44
|
+
if (!f.onScreen) hidden.set(f.id, f);
|
|
45
|
+
else if (f.ring === false) invisible.set(f.id, f);
|
|
46
|
+
}
|
|
47
|
+
run.stats.tabbed++;
|
|
48
|
+
const at = { page: route, viewport: "desktop", theme: "light", shot: shotKey("desktop", "light") };
|
|
49
|
+
|
|
50
|
+
if (trap) {
|
|
51
|
+
const first = trap.cycle[0];
|
|
52
|
+
const loop = [...trap.cycle.map(name), name(first)].join(" → ");
|
|
53
|
+
add(run, {
|
|
54
|
+
...at,
|
|
55
|
+
box: first.onScreen ? first.box : undefined,
|
|
56
|
+
selector: first.sel,
|
|
57
|
+
...(find?.(first) ?? {}),
|
|
58
|
+
rule: "flow/focus-trap",
|
|
59
|
+
severity: "error",
|
|
60
|
+
title: trap.cycle.length === 1 ? `Tab gets stuck on ${name(first)}` : `Tab loops between ${trap.cycle.length} controls and can't leave`,
|
|
61
|
+
evidence: `${loop}; ${order.length} of ${prep.total} focusable elements reached`,
|
|
62
|
+
detail: "A keyboard user can't get past this point: everything after it on the page is unreachable. Remove the key handler that moves focus back, or scope it to an open modal.",
|
|
63
|
+
steps: [openStep(route), `Press Tab ${trap.at + 1} times, to ${name(first)}`, "Keep pressing Tab", `Focus goes ${loop} and never reaches the rest of the page`],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (invisible.size) {
|
|
67
|
+
const list = [...invisible.values()];
|
|
68
|
+
const first = list[0];
|
|
69
|
+
add(run, {
|
|
70
|
+
...at,
|
|
71
|
+
box: first.box,
|
|
72
|
+
selector: first.sel,
|
|
73
|
+
...(find?.(first) ?? {}),
|
|
74
|
+
rule: "flow/focus-invisible",
|
|
75
|
+
severity: "error",
|
|
76
|
+
title: list.length === 1 ? `${name(first)} shows no sign of focus` : `${list.length} controls show no sign of focus`,
|
|
77
|
+
evidence: `${list.slice(0, 6).map(name).join(", ")}${list.length > 6 ? ", …" : ""}: no outline, ring, colour or underline changes on focus`,
|
|
78
|
+
detail: "A keyboard user can't see where they are. Give every focusable element a visible :focus-visible style, in the focus colour.",
|
|
79
|
+
fix: "/keyline color",
|
|
80
|
+
steps: [openStep(route), `Press Tab until focus reaches ${name(first)}`, "Nothing on screen changes to show it"],
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (hidden.size) {
|
|
84
|
+
const list = [...hidden.values()];
|
|
85
|
+
add(run, {
|
|
86
|
+
...at,
|
|
87
|
+
selector: list[0].sel,
|
|
88
|
+
...(find?.(list[0]) ?? {}),
|
|
89
|
+
rule: "flow/focus-hidden",
|
|
90
|
+
severity: "warning",
|
|
91
|
+
title: list.length === 1 ? `Tab stops on ${name(list[0])}, which can't be seen` : `Tab stops on ${list.length} things that can't be seen`,
|
|
92
|
+
evidence: `${list.slice(0, 6).map(name).join(", ")}${list.length > 6 ? ", …" : ""}: off screen, zero-size or transparent when focused`,
|
|
93
|
+
detail: "Focus disappears for a while: usually the links of a closed menu or drawer. Hide closed panels with `display: none`, `hidden` or `inert`, not only by moving them away.",
|
|
94
|
+
steps: [openStep(route), `Press Tab until focus reaches ${name(list[0])}`, "Focus is somewhere you can't see"],
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
} finally {
|
|
98
|
+
await page.close().catch(() => {});
|
|
99
|
+
}
|
|
100
|
+
}
|
package/smoke/links.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every same-origin link, requested once: what it answers, and how many
|
|
3
|
+
* redirects it takes to get there. A link that ends in 4xx or 5xx is broken;
|
|
4
|
+
* two or more redirects is a chain worth flattening; a URL seen twice in one
|
|
5
|
+
* chain is a loop no browser will finish.
|
|
6
|
+
*/
|
|
7
|
+
import { add } from "../browse/findings.mjs";
|
|
8
|
+
import { shotKey } from "../browse/capture.mjs";
|
|
9
|
+
import { isDestructivePath } from "./config.mjs";
|
|
10
|
+
import { openStep, pathOf, pool } from "./util.mjs";
|
|
11
|
+
|
|
12
|
+
const MAX_HOPS = 8;
|
|
13
|
+
|
|
14
|
+
/** Follow one URL's redirects by hand, so each hop is seen. */
|
|
15
|
+
export async function follow(request, url) {
|
|
16
|
+
const chain = [];
|
|
17
|
+
let current = url;
|
|
18
|
+
for (let hop = 0; hop <= MAX_HOPS; hop++) {
|
|
19
|
+
if (chain.some((c) => c.url === current)) return { chain, status: chain.at(-1)?.status ?? null, loop: true };
|
|
20
|
+
let res;
|
|
21
|
+
try {
|
|
22
|
+
res = await request.fetch(current, { method: "GET", maxRedirects: 0, failOnStatusCode: false, timeout: 15000 });
|
|
23
|
+
} catch (err) {
|
|
24
|
+
chain.push({ url: current, status: null, error: String(err?.message ?? err).split("\n")[0] });
|
|
25
|
+
return { chain, status: null, error: chain.at(-1).error };
|
|
26
|
+
}
|
|
27
|
+
const status = res.status();
|
|
28
|
+
chain.push({ url: current, status });
|
|
29
|
+
const location = res.headers()["location"];
|
|
30
|
+
await res.dispose().catch(() => {});
|
|
31
|
+
if (status >= 300 && status < 400 && location) {
|
|
32
|
+
current = new URL(location, current).href;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
return { chain, status };
|
|
36
|
+
}
|
|
37
|
+
return { chain, status: chain.at(-1)?.status ?? null, tooLong: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* `inventories` is route → the desktop inventory. Adds findings to `run`;
|
|
42
|
+
* returns the broken target paths so the walk's own 404 findings for them can
|
|
43
|
+
* be folded into these.
|
|
44
|
+
*/
|
|
45
|
+
export async function checkLinks({ run, request, base, inventories, settings, skipped, log }) {
|
|
46
|
+
const targets = new Map(); // href → [{ route, link }]
|
|
47
|
+
for (const [route, inv] of inventories) {
|
|
48
|
+
for (const link of inv?.links ?? []) {
|
|
49
|
+
if (!link.sameOrigin) continue;
|
|
50
|
+
if (!targets.has(link.href)) targets.set(link.href, []);
|
|
51
|
+
const list = targets.get(link.href);
|
|
52
|
+
const same = list.find((t) => t.route === route);
|
|
53
|
+
if (!same) list.push({ route, link });
|
|
54
|
+
else if (!same.link.visible && link.visible) same.link = link; // outline the copy a reader can see
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const hrefs = [...targets.keys()].filter((href) => {
|
|
58
|
+
if (settings.forbidUrl(href)) {
|
|
59
|
+
skipped.push({ what: `link ${pathOf(href, base)}`, why: "the config forbids it" });
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
// Following /logout or /delete/42 with GET can do exactly that, and the request shares the session.
|
|
63
|
+
if (isDestructivePath(href) && !settings.allowWrites) {
|
|
64
|
+
skipped.push({ what: `link ${pathOf(href, base)}`, why: "its URL looks destructive; --allow-writes includes it" });
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
});
|
|
69
|
+
log(` links ${hrefs.length} same-origin targets`);
|
|
70
|
+
const results = new Map();
|
|
71
|
+
await pool(hrefs, 6, async (href) => results.set(href, await follow(request, href)));
|
|
72
|
+
|
|
73
|
+
const broken = new Set();
|
|
74
|
+
for (const [href, r] of results) {
|
|
75
|
+
const redirects = r.chain.filter((c) => c.status >= 300 && c.status < 400).length;
|
|
76
|
+
const path = pathOf(href, base);
|
|
77
|
+
const hops = r.chain.map((c) => `${pathOf(c.url, base)} ${c.status ?? c.error}`).join(" → ");
|
|
78
|
+
for (const { route, link } of targets.get(href)) {
|
|
79
|
+
const at = {
|
|
80
|
+
page: route,
|
|
81
|
+
viewport: "desktop",
|
|
82
|
+
theme: "light",
|
|
83
|
+
shot: link.box ? shotKey("desktop", "light") : undefined,
|
|
84
|
+
box: link.box ?? undefined,
|
|
85
|
+
selector: link.sel,
|
|
86
|
+
};
|
|
87
|
+
const click = `${link.visible ? "Click" : "Find (it's hidden at this width)"} the link “${link.text || path}”`;
|
|
88
|
+
if (r.loop) {
|
|
89
|
+
add(run, { ...at, rule: "flow/redirect-loop", severity: "error", title: `A link redirects in a circle: ${path}`, evidence: hops, steps: [openStep(route), click, "The browser gives up: the redirects come back to a URL already visited"], detail: "Following this link never arrives anywhere; browsers stop and show an error page." });
|
|
90
|
+
} else if (r.error) {
|
|
91
|
+
add(run, { ...at, rule: "flow/broken-link", severity: "error", title: `A link fails to load: ${path}`, evidence: hops, steps: [openStep(route), click, `The request fails: ${r.error}`] });
|
|
92
|
+
broken.add(new URL(r.chain.at(-1).url).pathname);
|
|
93
|
+
} else if (r.status >= 400) {
|
|
94
|
+
const final = r.chain.at(-1).url;
|
|
95
|
+
add(run, {
|
|
96
|
+
...at,
|
|
97
|
+
rule: "flow/broken-link",
|
|
98
|
+
severity: "error",
|
|
99
|
+
title: `A link leads to a ${r.status}: ${path}`,
|
|
100
|
+
evidence: hops,
|
|
101
|
+
detail: r.status >= 500 ? "The page this link opens fails on the server." : "The page this link opens doesn't exist. Fix the href or restore the page.",
|
|
102
|
+
steps: [openStep(route), click, `It answers ${r.status}${final !== href ? ` at ${pathOf(final, base)}` : ""}`],
|
|
103
|
+
});
|
|
104
|
+
broken.add(new URL(final).pathname.replace(/\/+$/, "") || "/");
|
|
105
|
+
} else if (r.tooLong || redirects >= 2) {
|
|
106
|
+
add(run, {
|
|
107
|
+
...at,
|
|
108
|
+
rule: "flow/redirect-chain",
|
|
109
|
+
severity: "warning",
|
|
110
|
+
title: `A link takes ${redirects} redirects to arrive: ${path}`,
|
|
111
|
+
evidence: hops,
|
|
112
|
+
detail: "Each hop is a round trip before the page starts loading. Point the link at where it ends up.",
|
|
113
|
+
steps: [openStep(route), click, `It redirects ${redirects} times before answering`],
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
run.stats.links = results.size;
|
|
119
|
+
return broken;
|
|
120
|
+
}
|