also looking at this
LSU's own coaching surface, and what auditing the site turned up
#12
86 files
+3943
-1190
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -1,41 +1,47 @@ | ||
| 1 | 1 | /* eslint-disable no-console */ |
| 2 | 2 | /** |
| 3 | * Audit the built site for the mistakes a template cannot catch itself. | |
| 3 | * The mistakes a template cannot catch about itself. | |
| 4 | 4 | * |
| 5 | 5 | * A page renders perfectly well with a link to a page that was renamed, two |
| 6 | * elements sharing an id, an h3 under an h1, or an image whose alt was never | |
| 7 | * written. Nothing errors, nothing fails to build, and nobody notices until | |
| 8 | * somebody using a screen reader or a keyboard does. | |
| 6 | * elements sharing an id, an h3 under an h1, a link whose only content is an | |
| 7 | * icon, or an image whose alt was never written. Nothing errors and nothing | |
| 8 | * fails to build; it is noticed by the first person using a keyboard or a | |
| 9 | * screen reader, which is to say not by us. | |
| 9 | 10 | * |
| 10 | * bun run build && bun scripts/audit-html.ts | |
| 11 | * The rules live here and run twice. tests/html-audit.test.ts applies them to | |
| 12 | * the rendered views, so the suite catches a regression with no build; this | |
| 13 | * script applies them to dist, so a release can be checked against what was | |
| 14 | * actually produced rather than what the source implies: | |
| 11 | 15 | * |
| 12 | * Reads dist/, so it checks what is actually deployed rather than what the | |
| 13 | * source says should be. Exits non-zero on any finding. | |
| 16 | * bun run build && bun run audit:html | |
| 17 | * | |
| 18 | * Exits non-zero on any finding. | |
| 14 | 19 | */ |
| 15 | 20 | |
| 16 | import { readdirSync } from 'node:fs' | |
| 21 | import { readFileSync, readdirSync } from 'node:fs' | |
| 17 | 22 | import { join, relative } from 'node:path' |
| 18 | 23 | import process from 'node:process' |
| 19 | 24 | import server from '../config/server' |
| 20 | 25 | |
| 21 | 26 | const ROOT = 'dist' |
| 22 | 27 | |
| 23 | interface Finding { | |
| 28 | export interface Finding { | |
| 24 | 29 | page: string |
| 25 | 30 | rule: string |
| 26 | 31 | detail: string |
| 27 | 32 | } |
| 28 | 33 | |
| 29 | function pages(dir = ROOT): string[] { | |
| 34 | /** Built pages, depth first. `_`-prefixed directories are build internals. */ | |
| 35 | export function builtPages(dir: string = ROOT): string[] { | |
| 30 | 36 | return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { |
| 31 | 37 | const path = join(dir, entry.name) |
| 32 | if (entry.isDirectory()) return entry.name.startsWith('_') ? [] : pages(path) | |
| 38 | if (entry.isDirectory()) return entry.name.startsWith('_') ? [] : builtPages(path) | |
| 33 | 39 | return entry.name.endsWith('.html') ? [path] : [] |
| 34 | 40 | }) |
| 35 | 41 | } |
| 36 | 42 | |
| 37 | 43 | /** The URL a built file is served at. */ |
| 38 | function urlOf(file: string): string { | |
| 44 | export function urlOf(file: string): string { | |
| 39 | 45 | const path = `/${relative(ROOT, file).replace(/\.html$/, '')}` |
| 40 | 46 | return path === '/index' ? '/' : path |
| 41 | 47 | } |
| @@ -55,19 +61,20 @@ function attr(tag: string, name: string): string | null { | ||
| 55 | 61 | return new RegExp(`\\s${name}(?=[\\s/>])`, 'i').test(tag) ? '' : null |
| 56 | 62 | } |
| 57 | 63 | |
| 58 | const built = new Set(pages().map(urlOf)) | |
| 59 | const redirected = new Set(Object.keys(server.redirects ?? {})) | |
| 60 | const findings: Finding[] = [] | |
| 64 | /** Every URL the deployed site answers, which is what a link may point at. */ | |
| 65 | export function redirectSources(): string[] { | |
| 66 | return Object.keys(server.redirects ?? {}) | |
| 67 | } | |
| 61 | 68 | |
| 62 | for (const file of pages()) { | |
| 63 | const page = urlOf(file) | |
| 64 | const raw = await Bun.file(file).text() | |
| 69 | /** The rules, over one page. */ | |
| 70 | export function auditPage(page: string, raw: string, served: Set<string>): Finding[] { | |
| 71 | const findings: Finding[] = [] | |
| 65 | 72 | const body = visible(raw) |
| 66 | 73 | |
| 67 | // 1. Internal links point at something this build serves. | |
| 74 | // 1. Internal links point at something this site serves. | |
| 68 | 75 | for (const [, href] of raw.matchAll(/<a\b[^>]*\shref="(\/[^"#?]*)[^"]*"/gi)) { |
| 69 | 76 | const target = href!.length > 1 ? href!.replace(/\/$/, '') : href! |
| 70 | if (built.has(target) || redirected.has(target)) continue | |
| 77 | if (served.has(target)) continue | |
| 71 | 78 | // Assets are files on disk rather than pages. |
| 72 | 79 | if (/\.[a-z0-9]{2,5}$/i.test(target) || target.startsWith('/assets/')) continue |
| 73 | 80 | findings.push({ page, rule: 'dead-link', detail: target }) |
| @@ -97,8 +104,7 @@ for (const file of pages()) { | ||
| 97 | 104 | } |
| 98 | 105 | |
| 99 | 106 | // 5. A link a screen reader can announce. |
| 100 | for (const [match, tag, inner] of body.matchAll(/(<a\b[^>]*>)([\s\S]*?)<\/a>/gi)) { | |
| 101 | void match | |
| 107 | for (const [, tag, inner] of body.matchAll(/(<a\b[^>]*>)([\s\S]*?)<\/a>/gi)) { | |
| 102 | 108 | const text = inner!.replace(/<[^>]+>/g, '').replace(/&[a-z]+;/gi, ' ').trim() |
| 103 | 109 | if (text) continue |
| 104 | 110 | if (attr(tag!, 'aria-label') || attr(tag!, 'title')) continue |
| @@ -110,19 +116,34 @@ for (const file of pages()) { | ||
| 110 | 116 | // 6. A document that states its language. |
| 111 | 117 | if (!/<html\b[^>]*\slang="[^"]+"/i.test(raw)) |
| 112 | 118 | findings.push({ page, rule: 'no-lang', detail: '' }) |
| 119 | ||
| 120 | return findings | |
| 113 | 121 | } |
| 114 | 122 | |
| 115 | const byRule = new Map<string, Finding[]>() | |
| 116 | for (const finding of findings) { | |
| 117 | if (!byRule.has(finding.rule)) byRule.set(finding.rule, []) | |
| 118 | byRule.get(finding.rule)!.push(finding) | |
| 123 | /** Findings grouped by rule, for a terminal. */ | |
| 124 | export function formatFindings(findings: Finding[]): string { | |
| 125 | const byRule = new Map<string, Finding[]>() | |
| 126 | for (const finding of findings) { | |
| 127 | if (!byRule.has(finding.rule)) byRule.set(finding.rule, []) | |
| 128 | byRule.get(finding.rule)!.push(finding) | |
| 129 | } | |
| 130 | ||
| 131 | return [...byRule] | |
| 132 | .sort((a, b) => b[1].length - a[1].length) | |
| 133 | .map(([rule, rows]) => [ | |
| 134 | `${rule} (${rows.length})`, | |
| 135 | ...rows.slice(0, 12).map(row => ` ${row.page} ${row.detail}`), | |
| 136 | ...(rows.length > 12 ? [` ... ${rows.length - 12} more`] : []), | |
| 137 | ].join('\n')) | |
| 138 | .join('\n\n') | |
| 119 | 139 | } |
| 120 | 140 | |
| 121 | console.log(`audited ${built.size} pages`) | |
| 122 | for (const [rule, rows] of [...byRule].sort((a, b) => b[1].length - a[1].length)) { | |
| 123 | console.log(`\n${rule} (${rows.length})`) | |
| 124 | for (const row of rows.slice(0, 12)) console.log(` ${row.page} ${row.detail}`) | |
| 125 | if (rows.length > 12) console.log(` ... ${rows.length - 12} more`) | |
| 141 | if (import.meta.main) { | |
| 142 | const files = builtPages() | |
| 143 | const served = new Set([...files.map(urlOf), ...redirectSources()]) | |
| 144 | const findings = files.flatMap(file => auditPage(urlOf(file), readFileSync(file, 'utf8'), served)) | |
| 145 | ||
| 146 | console.log(`audited ${files.length} pages`) | |
| 147 | console.log(findings.length === 0 ? '\nno findings' : `\n${formatFindings(findings)}`) | |
| 148 | process.exit(findings.length === 0 ? 0 : 1) | |
| 126 | 149 | } |
| 127 | if (findings.length === 0) console.log('\nno findings') | |
| 128 | process.exit(findings.length === 0 ? 0 : 1) | |
| @@ -0,0 +1,54 @@ | ||
| 1 | import { describe, expect, it } from 'bun:test' | |
| 2 | import { readdirSync } from 'node:fs' | |
| 3 | import { join, resolve } from 'node:path' | |
| 4 | import { processDirectives } from '@stacksjs/stx' | |
| 5 | import { auditPage, formatFindings, redirectSources } from '../scripts/audit-html' | |
| 6 | ||
| 7 | /** | |
| 8 | * The same rules scripts/audit-html.ts runs over dist, run here over the | |
| 9 | * rendered views so a regression fails the suite rather than waiting for | |
| 10 | * somebody to build and remember to check. | |
| 11 | * | |
| 12 | * One gap worth knowing: component resolution here is not the build's. | |
| 13 | * `<dialog>` on /soul-races resolves through the @stacksjs/components bun | |
| 14 | * plugin at build time and renders as nothing in this pass, so what is inside | |
| 15 | * that dialog is checked by the script against dist rather than here. | |
| 16 | */ | |
| 17 | ||
| 18 | function viewFiles(dir = 'resources/views', prefix = ''): { url: string, file: string }[] { | |
| 19 | return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { | |
| 20 | if (entry.isDirectory()) | |
| 21 | return viewFiles(join(dir, entry.name), `${prefix}/${entry.name}`) | |
| 22 | if (!entry.name.endsWith('.stx')) | |
| 23 | return [] | |
| 24 | const name = entry.name.slice(0, -'.stx'.length) | |
| 25 | return [{ url: name === 'index' && prefix === '' ? '/' : `${prefix}/${name}`, file: join(dir, entry.name) }] | |
| 26 | }) | |
| 27 | } | |
| 28 | ||
| 29 | const views = viewFiles() | |
| 30 | const served = new Set([...views.map(view => view.url), ...redirectSources()]) | |
| 31 | ||
| 32 | async function render(file: string): Promise<string> { | |
| 33 | const path = resolve(file) | |
| 34 | return processDirectives(await Bun.file(path).text(), {}, path, { | |
| 35 | enabled: true, | |
| 36 | cache: false, | |
| 37 | componentsDir: resolve('resources/components'), | |
| 38 | layoutsDir: resolve('resources/layouts'), | |
| 39 | partialsDir: resolve('resources/partials'), | |
| 40 | }, new Set()) | |
| 41 | } | |
| 42 | ||
| 43 | describe('every page the site serves', () => { | |
| 44 | it('has no dead internal link, duplicate id, skipped heading or unnamed link', async () => { | |
| 45 | const findings = (await Promise.all(views.map(async view => auditPage(view.url, await render(view.file), served)))).flat() | |
| 46 | ||
| 47 | expect(formatFindings(findings)).toBe('') | |
| 48 | }, 120_000) | |
| 49 | ||
| 50 | it('checks every view, not a handful', () => { | |
| 51 | expect(views.length).toBeGreaterThanOrEqual(23) | |
| 52 | expect(views.map(view => view.url)).toContain('/coaching/feedback') | |
| 53 | }) | |
| 54 | }) | |