Files
archie/e2e/checks/accessibility.cjs

177 lines
9.4 KiB
JavaScript

// Keyboard and phone access: the task dialog fits and tabs in order, pages
// title correctly, the skip link and palette focus main content, and
// query-only tab switches keep focus where it is. Then the audit's two
// static sweeps (#1767): no unnamed controls on any route, and every keyboard
// focus stop's indicator clears 3:1 against the background behind it.
const assert = require('node:assert/strict');
const { staticRoutes } = require('./helpers/routes.cjs');
module.exports = async ({ page, go, shot, fail, settled, tolerate }) => {
// The extension catalogue 502 is a stack-environment issue, not this check.
tolerate(/extensions\/catalogue/);
await page.setViewportSize({ width: 390, height: 844 });
await go('/tasks');
await page.getByRole('button', { name: 'Jump to', exact: true }).click();
await page.getByRole('option', { name: 'New task', exact: true }).click();
const dialog = page.getByRole('dialog', { name: 'New task' });
await dialog.waitFor();
await settled();
const bounds = await dialog.boundingBox();
assert.ok(bounds.y >= 0 && bounds.y + bounds.height <= 844, 'New task dialog extends outside viewport');
await shot('phone-task-dialog-top');
const start = dialog.getByRole('button', { name: 'Start task', exact: true });
await start.scrollIntoViewIfNeeded();
const button = await start.boundingBox();
assert.ok(button.y >= 0 && button.y + button.height <= 844, 'Start task cannot be reached by scrolling');
await shot('phone-task-dialog');
const close = dialog.getByRole('button', { name: 'Close', exact: true });
await dialog.getByRole('button', { name: 'Cancel', exact: true }).focus();
await page.keyboard.press('Tab');
assert.ok(await close.evaluate(el => el === document.activeElement), 'Close is missing from keyboard tab order');
const closeBounds = await close.boundingBox();
assert.ok(closeBounds.y >= 0 && closeBounds.y + closeBounds.height <= 844, 'Close cannot be reached by keyboard');
await page.keyboard.press('Escape');
for (const [path, title] of [['/tasks', 'Tasks'], ['/events', 'Events'], ['/account', 'Your account'], ['/settings/models', 'Models | Instance'], ['/org/models', 'Models | Organisation']]) {
await go(path);
assert.equal(await page.title(), `${title} | Archie`);
}
await page.keyboard.press('Tab');
const skip = page.getByRole('link', { name: 'Skip to main content', exact: true });
assert.ok(await skip.evaluate(el => el === document.activeElement), 'Skip link is not the first keyboard stop');
await page.keyboard.press('Enter');
assert.equal(await page.evaluate(() => document.activeElement?.id), 'main-content');
await page.getByRole('button', { name: 'Sections', exact: true }).click();
await page.getByRole('menuitem', { name: 'Tasks', exact: true }).click();
await page.waitForFunction(() => document.activeElement?.id === 'main-content', null, { timeout: 5000 }).catch(() => {});
assert.equal(await page.evaluate(() => document.activeElement?.id), 'main-content', 'route navigation did not focus main');
await page.getByRole('button', { name: 'Jump to', exact: true }).click();
await page.getByRole('option', { name: 'Events', exact: true }).click();
await page.waitForFunction(() => document.activeElement?.id === 'main-content', null, { timeout: 5000 }).catch(() => {});
assert.equal(await page.evaluate(() => document.activeElement?.id), 'main-content', 'palette navigation did not focus main');
// The conversation panel is only open on some entries to this page; the
// focus check below needs it closed either way.
const hide = page.getByRole('button', { name: 'Hide conversation', exact: true });
if (await hide.count()) await hide.click();
const tab = page.getByRole('tab', { name: /^Map/ });
await tab.click();
assert.ok(await tab.evaluate(el => el === document.activeElement), 'query-only navigation stole focus');
await page.setViewportSize({ width: 1440, height: 900 });
// Unnamed controls: every visible input, textarea, select, button or
// widget role on each static route needs an accessible name (accname
// order: labelledby, label, content, value, placeholder, title).
for (const r of staticRoutes()) {
await go(r);
const unnamed = await page.evaluate(() => {
const name = (el) => {
const by = el.getAttribute('aria-labelledby');
if (by) {
const t = by.split(/\s+/).map((id) => (document.getElementById(id)?.textContent || '')).join(' ').trim();
if (t) return t;
}
const al = el.getAttribute('aria-label');
if (al && al.trim()) return al.trim();
if (el.id) {
const l = document.querySelector(`label[for="${CSS.escape(el.id)}"]`);
if (l && l.textContent.trim()) return l.textContent.trim();
}
const wrap = el.closest('label');
if (wrap && wrap.textContent.trim()) return wrap.textContent.trim();
if (el.tagName === 'BUTTON' || el.getAttribute('role') === 'button' || el.getAttribute('role') === 'tab' || el.getAttribute('role') === 'switch') return el.textContent.trim();
if (el.tagName === 'INPUT') {
const t = (el.getAttribute('type') || 'text').toLowerCase();
if (t === 'submit' || t === 'button' || t === 'reset') return el.value.trim();
if (t === 'image') return (el.getAttribute('alt') || '').trim();
}
return (el.getAttribute('placeholder') || el.getAttribute('title') || '').trim();
};
return [...document.querySelectorAll('button, input, textarea, select, [role="button"], [role="tab"], [role="switch"], [role="checkbox"]')]
.filter((el) => el.type !== 'hidden' && el.getClientRects().length)
.filter((el) => !name(el))
.map((el) => `${el.tagName.toLowerCase()}${el.id ? '#' + el.id : ''}: ${el.outerHTML.slice(0, 120)}`);
});
for (const u of unnamed) fail(`unnamed control on ${r}: ${u}`);
}
// Focus contrast: Tab through each static route; the focused element's
// outline or ring must clear 3:1 against the effective background behind it.
const focusProbe = () => page.evaluate(() => {
const el = document.activeElement;
if (!el || el === document.body || el === document.documentElement) return null;
const cs = getComputedStyle(el);
const parse = (c) => {
c = String(c).trim();
let m = c.match(/rgba?\(([^)]+)\)/);
if (m) {
const p = m[1].split(',').map(Number);
return { r: p[0], g: p[1], b: p[2], a: p.length > 3 ? p[3] : 1 };
}
m = c.match(/^#([0-9a-f]{3,8})$/i);
if (m) {
let h = m[1];
if (h.length === 3 || h.length === 4) h = [...h].map((x) => x + x).join('');
const n = parseInt(h.slice(0, 6), 16);
return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255, a: h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1 };
}
return null;
};
const bgBehind = (start) => {
for (let n = start; n; n = n.parentElement) {
const c = parse(getComputedStyle(n).backgroundColor);
if (c && c.a > 0) return c;
}
return { r: 255, g: 255, b: 255, a: 1 };
};
// An outer ring or outline is painted over the parent's background; an
// inset one over the element's own. Score against whichever is higher.
const bgSelf = bgBehind(el);
const bgParent = bgBehind(el.parentElement);
const over = (fg, bg) => {
const a = fg.a + bg.a * (1 - fg.a);
if (a === 0) return { r: 0, g: 0, b: 0 };
return { r: (fg.r * fg.a + bg.r * bg.a * (1 - fg.a)) / a, g: (fg.g * fg.a + bg.g * bg.a * (1 - fg.a)) / a, b: (fg.b * fg.a + bg.b * bg.a * (1 - fg.a)) / a };
};
const lum = (c) => {
const f = (v) => { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); };
return 0.2126 * f(c.r) + 0.7152 * f(c.g) + 0.0722 * f(c.b);
};
const ratio = (c1, c2) => {
const l1 = lum(c1), l2 = lum(c2);
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
};
let fg = null;
// The ring lives in --tw-ring-shadow on :focus-visible; read the var
// directly, then fall back to outline, any shadow layer, focus border.
if (el.matches(':focus-visible')) {
const m = cs.getPropertyValue('--tw-ring-shadow').match(/rgba?\([^)]+\)|#[0-9a-f]{3,8}\b/i);
if (m) { const c = parse(m[0]); if (c && c.a > 0) fg = c; }
}
if (!fg && cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) fg = parse(cs.outlineColor === 'currentcolor' ? cs.color : cs.outlineColor);
if (!fg && cs.boxShadow && cs.boxShadow !== 'none') {
for (const m of cs.boxShadow.matchAll(/rgba?\([^)]+\)|#[0-9a-f]{3,8}\b/gi)) {
const c = parse(m[0]);
if (c && c.a > 0) { fg = c; break; }
}
}
if (!fg && el.matches(':focus-visible') && parseFloat(cs.borderTopWidth) > 0) {
const c = parse(cs.borderTopColor);
if (c && c.a > 0) fg = c;
}
const sig = `${el.tagName}#${el.id}:${(el.textContent || '').trim().slice(0, 30)}`;
if (!fg) return { sig, ratio: 0 };
return { sig, ratio: Math.max(ratio(over(fg, bgSelf), bgSelf), ratio(over(fg, bgParent), bgParent)) };
});
for (const r of staticRoutes()) {
await go(r);
let prev = '';
for (let i = 0; i < 40; i++) {
await page.keyboard.press('Tab');
const info = await focusProbe();
if (!info || info.sig === prev) break;
prev = info.sig;
if (info.ratio < 3) fail(`focus indicator under 3:1 on ${r} at ${info.sig} (ratio ${info.ratio.toFixed(2)})`);
}
}
};