Files
Homelab-manager/server/test/accessibility.test.ts
bobbanandClaude Sonnet 5.5 645816d5a8 Add a Swedish interface with a per-person language switch
A small translation system (the English text is its own key, so anything
without a Swedish entry stays English) and a Language switch in the user menu,
saved on the account (users.language) and following the browser when no choice
has been made. Swedish covers the menu, shared parts (search, tables, dialogs,
relative times), the sign-in page and the everyday pages: Dashboard, Alerts,
Servers, Updates, Maintenance, My work, Coming up, To do, Integrations and the
display settings. Server-written messages stay English for now.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-12 21:49:49 +01:00

100 lines
5.2 KiB
TypeScript

/**
* What an axe-core sweep of the main pages (both themes) found and what was done about it, kept from drifting: the shades chosen for text on each background really do read (WCAG AA, 4.5:1), they are
* the ones the style sheet uses, and the controls that had no name have one.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
const web = (...parts: string[]) => readFileSync(join(import.meta.dirname, "..", "..", "web", "src", ...parts), "utf8");
const channel = (v: number) => {
const c = v / 255;
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
};
const luminance = (hex: string) => {
const n = parseInt(hex.replace("#", ""), 16);
return 0.2126 * channel((n >> 16) & 255) + 0.7152 * channel((n >> 8) & 255) + 0.0722 * channel(n & 255);
};
/** WCAG contrast ratio of two #rrggbb colours. */
const contrast = (a: string, b: string) => {
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
};
/** The colour of a tint (a colour at some strength over white or the dark surface), for the backgrounds text sits on. */
const over = (hex: string, alpha: number, base: string) => {
const f = (h: string) => [(parseInt(h.slice(1), 16) >> 16) & 255, (parseInt(h.slice(1), 16) >> 8) & 255, parseInt(h.slice(1), 16) & 255];
const [a, b] = [f(hex), f(base)];
return `#${a.map((v, i) => Math.round(v * alpha + b[i] * (1 - alpha)).toString(16).padStart(2, "0")).join("")}`;
};
const css = web("styles.css");
const WHITE = "#ffffff";
const DARK_PAGE = "#111827";
const DARK_CARD = "#1f2937";
const DARK_PILL = "#232e3e";
describe("the shades the style sheet uses read on the surfaces they are used on", () => {
test("light theme: text colours, on white and on their own tint (the badge), at least 4.5:1", () => {
const shades: [string, string, string][] = [
["green", "#17692a", "#2fb344"],
["yellow", "#8a5600", "#f59f00"],
["orange", "#a14d00", "#f76707"],
["red", "#b32d2d", "#d63939"],
["blue", "#0558a8", "#066fd1"],
];
for (const [name, shade, tabler] of shades) {
assert.ok(css.includes(shade), `${name}'s shade ${shade} is in the style sheet`);
assert.ok(contrast(shade, WHITE) >= 4.5, `${name} on white: ${contrast(shade, WHITE).toFixed(2)}`);
assert.ok(contrast(shade, over(tabler, 0.1, WHITE)) >= 4.5, `${name} on its tint: ${contrast(shade, over(tabler, 0.1, WHITE)).toFixed(2)}`);
}
assert.ok(css.includes("#5b6372") && contrast("#5b6372", "#f0f1f2") >= 4.5, "grey text on the grey badge");
});
test("dark theme: the tabs and pills that are not chosen, the chosen pill, the switch button and the info alert, at least 4.5:1", () => {
for (const [what, fg, bg] of [
["inactive tab", "#9ca3af", DARK_PAGE],
["inactive pill", "#9ca3af", DARK_CARD],
["chosen pill, outlined primary button", "#5aa9ff", DARK_PILL],
["the switch button that is on", "#5aa9ff", "#1e2c3d"],
["the info alert", "#7cc0f7", "#223245"],
] as const) {
assert.ok(css.includes(fg), `${what}: ${fg} is in the style sheet`);
assert.ok(contrast(fg, bg) >= 4.5, `${what}: ${contrast(fg, bg).toFixed(2)}`);
}
});
test("the light theme's chosen pill and info alert take the dark blue", () => {
assert.ok(contrast("#0558a8", "#f0f7fd") >= 4.5, "the info alert's tint");
assert.match(css, /:root:not\(\[data-bs-theme="dark"\]\) \.alert-info\s*\{\s*color:\s*#0558a8/);
assert.match(css, /:root:not\(\[data-bs-theme="dark"\]\) :is\(\.text-primary, \.nav-pills \.nav-link\.active\)\s*\{\s*color:\s*#0558a8/);
});
});
describe("links, targets and names", () => {
test("a link in the middle of a sentence is underlined, and one that is a whole line is at least 24 px tall", () => {
assert.match(css, /:is\(p, li, \.small, \.text-secondary, \.form-hint, \.page-intro\) a:not\([^)]*\.nav-link[^)]*\)\s*\{\s*text-decoration:\s*underline/);
assert.match(css, /:is\(li, \.small\) > a:only-child:not\([^)]*\)\s*\{\s*display:\s*inline-block;\s*padding-block:\s*0\.3rem/);
});
test("the controls the sweep found without a name have one", () => {
assert.match(web("pages", "Alerts.tsx"), /aria-label=\{t\("Severity"\)\}/);
assert.match(web("pages", "Alerts.tsx"), /aria-label=\{t\("Kind of alert"\)\}/);
assert.match(web("pages", "Secrets.tsx"), /aria-label=\{t\("Status"\)\}/);
assert.match(web("pages", "Users.tsx"), /aria-label=\{`Role of \$\{u\.name \?\? u\.email \?\? "this user"\}`\}/);
});
test("a map node is named by what is written on it first (WCAG 2.5.3), with a space between the two parts of the text", () => {
const map = web("pages", "Map.tsx");
assert.match(map, /"aria-label": `\$\{n\.label\}\$\{n\.sub \? ` \$\{n\.sub\}` : ""\}, \$\{KIND_WORD\[n\.kind\]\}, \$\{STATUS_WORD\[n\.status\]\}`/);
assert.match(map, /\{" "\}\s*<span className="map-sub">/);
});
test("keyboard focus shows a ring on every control, by one rule for them all", () => {
assert.match(css, /:where\(a, button, input, select, textarea, summary, \[tabindex\]\):focus-visible\s*\{/);
});
});