@su-sws/synthetic-web-team 0.1.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/AGENTS.md +149 -0
- package/README.md +321 -0
- package/package.json +58 -0
- package/packages/cli/bin/sws.mjs +722 -0
- package/packages/cli/src/axe.mjs +164 -0
- package/packages/cli/src/browser.mjs +192 -0
- package/packages/cli/src/checks.mjs +1254 -0
- package/packages/cli/src/github.mjs +131 -0
- package/packages/cli/src/history.mjs +137 -0
- package/packages/cli/src/perf.mjs +245 -0
- package/packages/cli/src/preflight.mjs +185 -0
- package/packages/cli/src/report.mjs +315 -0
- package/packages/cli/src/states.mjs +556 -0
- package/packages/wizard/bin/wizard.mjs +767 -0
- package/packages/wizard/src/detect.mjs +135 -0
- package/packages/wizard/src/emit.mjs +553 -0
- package/skills/role-accessibility-lead/SKILL.md +183 -0
- package/skills/role-api-architect/SKILL.md +65 -0
- package/skills/role-backend-developer/SKILL.md +66 -0
- package/skills/role-compliance-officer/SKILL.md +73 -0
- package/skills/role-content-designer/SKILL.md +145 -0
- package/skills/role-devops/SKILL.md +200 -0
- package/skills/role-discoverability/SKILL.md +164 -0
- package/skills/role-frontend-developer/SKILL.md +167 -0
- package/skills/role-fullstack-developer/SKILL.md +56 -0
- package/skills/role-identity-engineer/SKILL.md +77 -0
- package/skills/role-information-architect/SKILL.md +136 -0
- package/skills/role-infrastructure-architect/SKILL.md +60 -0
- package/skills/role-qa-engineer/SKILL.md +81 -0
- package/skills/role-security-operations/SKILL.md +56 -0
- package/skills/role-software-architect/SKILL.md +75 -0
- package/skills/role-staff-technical-architect/SKILL.md +73 -0
- package/skills/role-strategist/SKILL.md +122 -0
- package/skills/role-user-researcher/SKILL.md +66 -0
- package/skills/role-ux-designer/SKILL.md +159 -0
- package/skills/sws-compliance-check/SKILL.md +153 -0
- package/skills/sws-decanter/SKILL.md +191 -0
- package/skills/sws-deploy/SKILL.md +162 -0
- package/skills/sws-diverge/SKILL.md +131 -0
- package/skills/sws-github/SKILL.md +171 -0
- package/skills/sws-install/SKILL.md +97 -0
- package/skills/sws-netlify/SKILL.md +110 -0
- package/skills/sws-onboard/SKILL.md +85 -0
- package/skills/sws-preflight/SKILL.md +87 -0
- package/skills/sws-prior-art/SKILL.md +137 -0
- package/skills/sws-vercel/SKILL.md +117 -0
- package/standards/fragments/global-footer.yml +137 -0
- package/standards/fragments/identity-bar.yml +180 -0
- package/standards/hosting/capabilities.yml +215 -0
- package/standards/hosting/github-pages.yml +306 -0
- package/standards/hosting/netlify.yml +234 -0
- package/standards/hosting/vercel.yml +284 -0
- package/standards/patterns/components.md +162 -0
- package/standards/patterns/content.md +130 -0
- package/standards/patterns/decanter.md +352 -0
- package/standards/patterns/discoverability.md +121 -0
- package/standards/patterns/forms.md +116 -0
- package/standards/patterns/ia.md +126 -0
- package/standards/patterns/motion.md +267 -0
- package/standards/patterns/sws-conventions.md +225 -0
- package/standards/policy/accessibility.md +241 -0
- package/standards/policy/brand.md +213 -0
- package/standards/policy/escalation.md +53 -0
- package/standards/policy/identity.md +96 -0
- package/standards/policy/minsec.md +119 -0
- package/standards/policy/minweb.md +87 -0
- package/standards/policy/privacy.md +114 -0
- package/standards/policy/procurement.md +79 -0
- package/standards/prior-art/README.md +205 -0
- package/standards/prior-art/catalog.yml +409 -0
- package/standards/prior-art/patterns/INDEX.md +53 -0
- package/standards/prior-art/repos.yml +734 -0
- package/standards/recipes/astro-ssr/RECIPE.md +274 -0
- package/standards/recipes/astro-ssr/acceptance.yml +168 -0
- package/standards/recipes/astro-static/RECIPE.md +468 -0
- package/standards/recipes/astro-static/acceptance.yml +800 -0
- package/standards/recipes/next-ssr/RECIPE.md +407 -0
- package/standards/recipes/next-ssr/acceptance.yml +187 -0
- package/standards/scope.md +111 -0
- package/standards/stack/performance-budget.yml +95 -0
- package/standards/stack/reference-versions.yml +168 -0
- package/standards/stack/requirements.yml +154 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// axe runner. Separate from the check modules because it needs a real browser,
|
|
2
|
+
// which the CLI deliberately does not depend on.
|
|
3
|
+
//
|
|
4
|
+
// THE RULE THIS FILE EXISTS TO OBEY: a check that cannot run reports `unknown`,
|
|
5
|
+
// never `pass`. A harness in this project once printed "TOTAL VIOLATIONS: 0"
|
|
6
|
+
// while axe had silently failed to load. Every early return below writes a
|
|
7
|
+
// status other than `ok`, and the reader in checks.mjs turns anything that is
|
|
8
|
+
// not `ok` into `unknown`. There is no code path here that can produce a clean
|
|
9
|
+
// result without axe having actually reported one.
|
|
10
|
+
//
|
|
11
|
+
// Shared browser plumbing lives in browser.mjs, alongside the perf runner.
|
|
12
|
+
|
|
13
|
+
import { existsSync } from 'node:fs';
|
|
14
|
+
import { join, relative } from 'node:path';
|
|
15
|
+
import {
|
|
16
|
+
serve, launchChromium, settleAnimations, revealAll, routeFor, newestMtime, writeResults, fromProject,
|
|
17
|
+
} from './browser.mjs';
|
|
18
|
+
|
|
19
|
+
export const RESULTS_PATH = join('.sws', 'axe-results.json');
|
|
20
|
+
export const SCHEMA = 1;
|
|
21
|
+
|
|
22
|
+
// The policy target, and nothing above it. Siteimprove's AAA and best-practice
|
|
23
|
+
// findings are reported separately and are not this check's business.
|
|
24
|
+
export const TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'];
|
|
25
|
+
|
|
26
|
+
export { newestMtime };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Run axe over every built route. Always resolves; never throws. The returned
|
|
30
|
+
* payload's `status` is `ok` only when axe genuinely reported on every route.
|
|
31
|
+
*/
|
|
32
|
+
export async function runAxe({ root, dist, html }) {
|
|
33
|
+
const started = new Date().toISOString();
|
|
34
|
+
const bail = (status, detail) => writeResults(root, RESULTS_PATH, {
|
|
35
|
+
schema: SCHEMA, status, detail, generated: started, tags: TAGS, routes: [],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!dist || !existsSync(dist)) return bail('no-build', 'no build output found; run the build first');
|
|
39
|
+
if (!html.length) {
|
|
40
|
+
return bail('no-build', `no HTML found in ${relative(root, dist) || dist}; run the build first`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Dependencies live in the project. Missing ones are a normal, expected state.
|
|
44
|
+
let AxeBuilder;
|
|
45
|
+
try {
|
|
46
|
+
// Exports both `default` and `AxeBuilder`; take whichever is a constructor
|
|
47
|
+
// so an upstream packaging change does not silently break the run.
|
|
48
|
+
const axeMod = await fromProject(root, '@axe-core/playwright');
|
|
49
|
+
AxeBuilder = typeof axeMod.default === 'function' ? axeMod.default : axeMod.AxeBuilder;
|
|
50
|
+
if (typeof AxeBuilder !== 'function') throw new Error('@axe-core/playwright did not export a constructor');
|
|
51
|
+
} catch (err) {
|
|
52
|
+
return bail('deps-missing',
|
|
53
|
+
`${err.message}. Install with: npm i -D @playwright/test @axe-core/playwright && npx playwright install chromium`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const { browser, playwrightPkg, error } = await launchChromium(root);
|
|
57
|
+
if (error) {
|
|
58
|
+
return bail(/Playwright package/.test(error) ? 'deps-missing' : 'browser-unavailable', error);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const srv = await serve(dist);
|
|
62
|
+
const versions = { playwright: playwrightPkg, browser: browser.version() };
|
|
63
|
+
const routes = [];
|
|
64
|
+
let engine = null;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const context = await browser.newContext();
|
|
68
|
+
for (const file of html) {
|
|
69
|
+
const route = routeFor(dist, file);
|
|
70
|
+
const page = await context.newPage();
|
|
71
|
+
const pageErrors = [];
|
|
72
|
+
page.on('pageerror', (e) => pageErrors.push(e.message));
|
|
73
|
+
try {
|
|
74
|
+
const resp = await page.goto(`${srv.origin}${route}`, { waitUntil: 'load', timeout: 30_000 });
|
|
75
|
+
if (!resp || !resp.ok()) throw new Error(`HTTP ${resp ? resp.status() : 'no response'}`);
|
|
76
|
+
|
|
77
|
+
// TWO STEPS, IN THIS ORDER, EACH FOR A DIFFERENT MEASURED BUG.
|
|
78
|
+
//
|
|
79
|
+
// 1. revealAll: entry motion holds below-fold content at opacity 0
|
|
80
|
+
// until it scrolls into view, and axe SKIPS invisible elements. So
|
|
81
|
+
// without scrolling, whole sections go unaudited and the clean
|
|
82
|
+
// result is a false negative -- which is worse than a false
|
|
83
|
+
// positive, because a false positive gets investigated. Measured on
|
|
84
|
+
// this project's own site: 116 rule-nodes unscrolled, 145 scrolled.
|
|
85
|
+
//
|
|
86
|
+
// 2. settleAnimations: axe's color-contrast rule reads *computed*
|
|
87
|
+
// colour, so an element captured mid-fade reports the blend against
|
|
88
|
+
// its backdrop. This site once produced four such failures at ratios
|
|
89
|
+
// of 1.05 to 1.55 (#f9f9f9 on #ffffff), all false.
|
|
90
|
+
//
|
|
91
|
+
// Reveal first, then settle: scrolling is what starts the animations
|
|
92
|
+
// that then need to finish.
|
|
93
|
+
await revealAll(page);
|
|
94
|
+
await settleAnimations(page);
|
|
95
|
+
|
|
96
|
+
const results = await new AxeBuilder({ page }).withTags(TAGS).analyze();
|
|
97
|
+
|
|
98
|
+
// THE GUARD. axe returning four empty buckets means it did not run. A
|
|
99
|
+
// real audit always classifies rules somewhere. Without this, a failed
|
|
100
|
+
// injection reads as a clean page, which is the bug in the header above.
|
|
101
|
+
const classified = (results.violations?.length ?? 0) + (results.passes?.length ?? 0)
|
|
102
|
+
+ (results.incomplete?.length ?? 0) + (results.inapplicable?.length ?? 0);
|
|
103
|
+
if (!results.testEngine?.version || classified === 0) {
|
|
104
|
+
throw new Error('axe reported no rule results at all, so it did not run');
|
|
105
|
+
}
|
|
106
|
+
engine ??= results.testEngine;
|
|
107
|
+
|
|
108
|
+
routes.push({
|
|
109
|
+
route,
|
|
110
|
+
file: relative(root, file),
|
|
111
|
+
status: 'ok',
|
|
112
|
+
counts: {
|
|
113
|
+
violations: results.violations.length,
|
|
114
|
+
passes: results.passes.length,
|
|
115
|
+
incomplete: results.incomplete.length,
|
|
116
|
+
// Node counts, not just rule counts. Rules stay flat while coverage
|
|
117
|
+
// changes, so this is the number that shows whether entry motion is
|
|
118
|
+
// hiding content from the audit.
|
|
119
|
+
passNodes: results.passes.reduce((a, r) => a + r.nodes.length, 0),
|
|
120
|
+
violationNodes: results.violations.reduce((a, r) => a + r.nodes.length, 0),
|
|
121
|
+
},
|
|
122
|
+
pageErrors,
|
|
123
|
+
violations: results.violations.map((v) => ({
|
|
124
|
+
id: v.id, impact: v.impact, help: v.help, helpUrl: v.helpUrl,
|
|
125
|
+
tags: v.tags.filter((t) => TAGS.includes(t)),
|
|
126
|
+
nodes: v.nodes.slice(0, 5).map((n) => ({
|
|
127
|
+
target: n.target.join(' '),
|
|
128
|
+
failureSummary: (n.failureSummary || '').replace(/\s+/g, ' ').trim(),
|
|
129
|
+
})),
|
|
130
|
+
nodeCount: v.nodes.length,
|
|
131
|
+
})),
|
|
132
|
+
});
|
|
133
|
+
} catch (err) {
|
|
134
|
+
// One bad route does not invalidate the others, but it does mean the run
|
|
135
|
+
// is incomplete, which the aggregate status below reflects.
|
|
136
|
+
routes.push({ route, file: relative(root, file), status: 'error', error: err.message, pageErrors });
|
|
137
|
+
} finally {
|
|
138
|
+
await page.close();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
} finally {
|
|
142
|
+
await browser.close().catch(() => {});
|
|
143
|
+
await srv.stop();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const failed = routes.filter((r) => r.status !== 'ok');
|
|
147
|
+
return writeResults(root, RESULTS_PATH, {
|
|
148
|
+
schema: SCHEMA,
|
|
149
|
+
// `partial` is deliberately not `ok`: an incomplete run cannot award points.
|
|
150
|
+
status: failed.length === 0 ? 'ok' : (failed.length === routes.length ? 'error' : 'partial'),
|
|
151
|
+
detail: failed.length ? `${failed.length} of ${routes.length} route(s) could not be audited` : undefined,
|
|
152
|
+
generated: started,
|
|
153
|
+
finished: new Date().toISOString(),
|
|
154
|
+
distMtime: newestMtime(dist),
|
|
155
|
+
tags: TAGS,
|
|
156
|
+
versions: { ...versions, axe: engine?.version ?? null },
|
|
157
|
+
totals: {
|
|
158
|
+
routes: routes.length,
|
|
159
|
+
violations: routes.reduce((a, r) => a + (r.counts?.violations ?? 0), 0),
|
|
160
|
+
incomplete: routes.reduce((a, r) => a + (r.counts?.incomplete ?? 0), 0),
|
|
161
|
+
},
|
|
162
|
+
routes,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Shared plumbing for the two checks that need a real browser: axe and the
|
|
2
|
+
// performance budget. Extracted so they cannot drift apart, because the
|
|
3
|
+
// interesting parts are the same for both — serving a build with a framework
|
|
4
|
+
// `base` prefix, resolving Playwright from the consumer's project rather than
|
|
5
|
+
// this CLI, and waiting for animations before measuring anything.
|
|
6
|
+
|
|
7
|
+
import { createServer } from 'node:http';
|
|
8
|
+
import { readFileSync, existsSync, statSync, readdirSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { join, extname, sep } from 'node:path';
|
|
10
|
+
import { createRequire } from 'node:module';
|
|
11
|
+
import { pathToFileURL } from 'node:url';
|
|
12
|
+
|
|
13
|
+
// How long to let CSS animations settle before measuring. Too short and axe
|
|
14
|
+
// reads colour mid-fade; see the long note at the analyze() call in axe.mjs.
|
|
15
|
+
export const ANIMATION_BUDGET_MS = 2500;
|
|
16
|
+
|
|
17
|
+
const TYPES = {
|
|
18
|
+
'.html': 'text/html; charset=utf-8', '.css': 'text/css; charset=utf-8',
|
|
19
|
+
'.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8',
|
|
20
|
+
'.json': 'application/json', '.xml': 'application/xml',
|
|
21
|
+
'.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg',
|
|
22
|
+
'.jpeg': 'image/jpeg', '.webp': 'image/webp', '.avif': 'image/avif',
|
|
23
|
+
'.gif': 'image/gif', '.ico': 'image/x-icon', '.woff': 'font/woff',
|
|
24
|
+
'.woff2': 'font/woff2', '.ttf': 'font/ttf', '.txt': 'text/plain; charset=utf-8',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Astro's `base` is the wrinkle: with base set, files sit at the dist root but
|
|
28
|
+
// internal links point at /<base>/..., so a naive server 404s every asset and
|
|
29
|
+
// the page gets measured unstyled. Rather than parsing framework config, try
|
|
30
|
+
// progressively looser resolutions.
|
|
31
|
+
function resolveFile(dist, urlPath) {
|
|
32
|
+
const clean = decodeURIComponent(urlPath.split('?')[0].split('#')[0]);
|
|
33
|
+
const segments = clean.split('/').filter((s) => s && s !== '.' && s !== '..');
|
|
34
|
+
for (const segs of [segments, segments.slice(1)]) {
|
|
35
|
+
const base = join(dist, ...segs);
|
|
36
|
+
for (const p of [base, `${base}.html`, join(base, 'index.html')]) {
|
|
37
|
+
// Containment: never serve outside dist, even though the input is ours.
|
|
38
|
+
if (!p.startsWith(dist + sep) && p !== dist) continue;
|
|
39
|
+
if (existsSync(p) && statSync(p).isFile()) return p;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Static server over the build output. Resolves to { server, port, origin, stop }. */
|
|
46
|
+
export function serve(dist) {
|
|
47
|
+
return new Promise((resolvePromise) => {
|
|
48
|
+
const server = createServer((req, res) => {
|
|
49
|
+
const file = resolveFile(dist, req.url || '/');
|
|
50
|
+
if (!file) { res.writeHead(404); res.end('not found'); return; }
|
|
51
|
+
res.writeHead(200, { 'content-type': TYPES[extname(file).toLowerCase()] || 'application/octet-stream' });
|
|
52
|
+
res.end(readFileSync(file));
|
|
53
|
+
});
|
|
54
|
+
server.listen(0, '127.0.0.1', () => {
|
|
55
|
+
const { port } = server.address();
|
|
56
|
+
resolvePromise({
|
|
57
|
+
server, port,
|
|
58
|
+
origin: `http://127.0.0.1:${port}`,
|
|
59
|
+
stop: () => new Promise((r) => server.close(r)),
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Files the CLI itself writes into the build output. Excluded from the staleness
|
|
66
|
+
// calculation because we know we wrote them and they are not a rebuild.
|
|
67
|
+
//
|
|
68
|
+
// Without this, the recommended pipeline defeats its own guard: `sws check
|
|
69
|
+
// --badge dist/badge.json` bumps the directory mtime, so a later `sws doctor`
|
|
70
|
+
// decides the axe and perf results predate the build and withholds 13 points.
|
|
71
|
+
// A false `unknown` is safe but it is still a false alarm, and false alarms are
|
|
72
|
+
// what get a check switched off. Observed while wiring this project's own Pages
|
|
73
|
+
// workflow.
|
|
74
|
+
//
|
|
75
|
+
// Deliberately a short, explicit list rather than a pattern: anything else
|
|
76
|
+
// appearing in dist after a measurement genuinely should invalidate it.
|
|
77
|
+
export const CLI_ARTIFACTS = new Set(['badge.json']);
|
|
78
|
+
|
|
79
|
+
// Newest mtime anywhere in dist. Both runners record it and both readers
|
|
80
|
+
// recompute it, so they MUST agree on the exclusions — hence one shared set.
|
|
81
|
+
// A pass recorded against a previous build is not evidence about this one, and
|
|
82
|
+
// in a report the two are indistinguishable.
|
|
83
|
+
export function newestMtime(dir) {
|
|
84
|
+
let newest = 0;
|
|
85
|
+
(function walk(d, depth) {
|
|
86
|
+
for (const name of readdirSync(d)) {
|
|
87
|
+
if (depth === 0 && CLI_ARTIFACTS.has(name)) continue;
|
|
88
|
+
const p = join(d, name);
|
|
89
|
+
const st = statSync(p);
|
|
90
|
+
if (st.isDirectory()) walk(p, depth + 1);
|
|
91
|
+
else newest = Math.max(newest, st.mtimeMs);
|
|
92
|
+
}
|
|
93
|
+
})(dir, 0);
|
|
94
|
+
return newest;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function writeResults(root, relPath, payload) {
|
|
98
|
+
mkdirSync(join(root, '.sws'), { recursive: true });
|
|
99
|
+
writeFileSync(join(root, relPath), `${JSON.stringify(payload, null, 2)}\n`);
|
|
100
|
+
return payload;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Resolve a module from the project's node_modules, not the CLI's.
|
|
104
|
+
//
|
|
105
|
+
// CJS interop matters here and cost a debugging round. Both `playwright` and
|
|
106
|
+
// `@playwright/test` are CommonJS, so `import()` returns a namespace whose named
|
|
107
|
+
// exports are whatever cjs-module-lexer detected statically — `chromium` is not
|
|
108
|
+
// among them and lands only on `.default`. Reading `mod.chromium` alone looks
|
|
109
|
+
// exactly like "Playwright is not installed". Merge default onto the namespace
|
|
110
|
+
// so callers see one shape.
|
|
111
|
+
export async function fromProject(root, name) {
|
|
112
|
+
const req = createRequire(join(root, 'package.json'));
|
|
113
|
+
const mod = await import(pathToFileURL(req.resolve(name)).href);
|
|
114
|
+
const d = mod.default;
|
|
115
|
+
return (d && typeof d === 'object') ? { ...d, ...mod } : mod;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Find Playwright in the project and launch Chromium.
|
|
120
|
+
* Returns { browser, playwrightPkg } or { error } — never throws, because a
|
|
121
|
+
* missing browser is a normal state that must read as `unknown`, not a crash.
|
|
122
|
+
*/
|
|
123
|
+
export async function launchChromium(root) {
|
|
124
|
+
let pw, playwrightPkg;
|
|
125
|
+
for (const name of ['playwright', '@playwright/test', 'playwright-core']) {
|
|
126
|
+
try { pw = await fromProject(root, name); playwrightPkg = name; break; } catch { /* next */ }
|
|
127
|
+
}
|
|
128
|
+
const chromium = pw?.chromium ?? pw?.default?.chromium;
|
|
129
|
+
if (!chromium?.launch) {
|
|
130
|
+
return { error: 'no Playwright package exporting `chromium`. Install with: npm i -D @playwright/test && npx playwright install chromium' };
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
const browser = await chromium.launch();
|
|
134
|
+
return { browser, playwrightPkg };
|
|
135
|
+
} catch (err) {
|
|
136
|
+
// Overwhelmingly "browser not downloaded". Say the fix.
|
|
137
|
+
return { error: `could not launch Chromium: ${err.message.split('\n')[0]}. Try: npx playwright install chromium` };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Let entry animations finish before measuring. Bounded, because an infinite
|
|
143
|
+
* animation (a spinner) never finishes. Never throws.
|
|
144
|
+
*/
|
|
145
|
+
export async function settleAnimations(page, budget = ANIMATION_BUDGET_MS) {
|
|
146
|
+
await page.evaluate(async (ms) => {
|
|
147
|
+
const anims = document.getAnimations ? document.getAnimations() : [];
|
|
148
|
+
if (!anims.length) return;
|
|
149
|
+
await Promise.race([
|
|
150
|
+
// allSettled: a cancelled animation rejects, and that is not an error.
|
|
151
|
+
Promise.allSettled(anims.map((a) => a.finished)),
|
|
152
|
+
new Promise((r) => setTimeout(r, ms)),
|
|
153
|
+
]);
|
|
154
|
+
}, budget).catch(() => { /* measuring an unsettled page beats failing the route */ });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Scroll the whole page, then return to the top.
|
|
159
|
+
*
|
|
160
|
+
* REQUIRED BEFORE AUDITING, and the reason is a measured coverage gap rather
|
|
161
|
+
* than a theory. Entry animation (see standards/patterns/motion.md) holds
|
|
162
|
+
* below-fold content at `opacity: 0` until it scrolls into view. axe correctly
|
|
163
|
+
* treats an invisible element as not applicable -- so it SKIPS it. On this
|
|
164
|
+
* project's own docs site that meant 13 blocks were never audited, and axe
|
|
165
|
+
* reported a clean 0 violations while covering 116 rule-nodes instead of 145.
|
|
166
|
+
*
|
|
167
|
+
* A false negative in an accessibility check is worse than a false positive: a
|
|
168
|
+
* false positive gets investigated, a false negative gets trusted.
|
|
169
|
+
*
|
|
170
|
+
* Also triggers `client:visible` islands, which is the same reason the perf
|
|
171
|
+
* runner needs it -- lazily hydrated JavaScript is still JavaScript the reader
|
|
172
|
+
* downloads.
|
|
173
|
+
*/
|
|
174
|
+
export async function revealAll(page, { step = 600, settle = 120 } = {}) {
|
|
175
|
+
try {
|
|
176
|
+
const height = await page.evaluate(() => document.body.scrollHeight);
|
|
177
|
+
for (let y = 0; y < height; y += step) {
|
|
178
|
+
await page.evaluate((to) => window.scrollTo(0, to), y);
|
|
179
|
+
await page.waitForTimeout(settle);
|
|
180
|
+
}
|
|
181
|
+
await page.evaluate(() => window.scrollTo(0, 0));
|
|
182
|
+
await page.waitForTimeout(settle);
|
|
183
|
+
} catch {
|
|
184
|
+
// Auditing an unscrolled page beats failing the route.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** dist-relative file path -> the URL path a reader would visit. */
|
|
189
|
+
export function routeFor(dist, file) {
|
|
190
|
+
const rel = file.slice(dist.length + 1).split(sep).join('/');
|
|
191
|
+
return `/${rel}`.replace(/\/index\.html$/, '/').replace(/\.html$/, '');
|
|
192
|
+
}
|