125 lines
4.6 KiB
JavaScript
125 lines
4.6 KiB
JavaScript
"use strict";
|
|
/**
|
|
* AccessibilityCollector — runs axe-core after state changes to detect WCAG violations.
|
|
* Converts axe violations to IAnomaly with severity mapped from impact level.
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.AccessibilityCollector = exports.DEFAULT_A11Y_CONFIG = void 0;
|
|
const crypto = __importStar(require("crypto"));
|
|
exports.DEFAULT_A11Y_CONFIG = {
|
|
enabled: true,
|
|
minImpact: 'serious',
|
|
wcagLevel: 'AA',
|
|
};
|
|
const IMPACT_TO_SEVERITY = {
|
|
minor: 'low',
|
|
moderate: 'medium',
|
|
serious: 'high',
|
|
critical: 'critical',
|
|
};
|
|
const IMPACT_RANK = {
|
|
minor: 0,
|
|
moderate: 1,
|
|
serious: 2,
|
|
critical: 3,
|
|
};
|
|
class AccessibilityCollector {
|
|
constructor(config = {}) {
|
|
this.config = { ...exports.DEFAULT_A11Y_CONFIG, ...config };
|
|
}
|
|
async collect(page, stateId, sessionId, actionTrace) {
|
|
if (!this.config.enabled)
|
|
return [];
|
|
try {
|
|
const violations = await this.runAxe(page);
|
|
const minRank = IMPACT_RANK[this.config.minImpact] ?? 2;
|
|
const anomalies = [];
|
|
for (const violation of violations) {
|
|
const impact = violation.impact ?? 'minor';
|
|
if ((IMPACT_RANK[impact] ?? 0) < minRank)
|
|
continue;
|
|
const severity = IMPACT_TO_SEVERITY[impact] ?? 'medium';
|
|
anomalies.push({
|
|
id: crypto.randomUUID(),
|
|
type: 'accessibility_violation',
|
|
severity,
|
|
observationId: stateId,
|
|
actionTrace,
|
|
description: `[axe] ${violation.description}`,
|
|
evidence: {
|
|
rawErrors: [
|
|
`Rule: ${violation.id}`,
|
|
`Impact: ${impact}`,
|
|
`Affected nodes: ${violation.nodes.length}`,
|
|
`Help: ${violation.helpUrl}`,
|
|
],
|
|
},
|
|
timestamp: Date.now(),
|
|
});
|
|
}
|
|
return anomalies;
|
|
}
|
|
catch {
|
|
// axe might fail if page is not in a valid state
|
|
return [];
|
|
}
|
|
}
|
|
async runAxe(page) {
|
|
try {
|
|
const { AxeBuilder } = await Promise.resolve().then(() => __importStar(require('@axe-core/playwright')));
|
|
const results = await new AxeBuilder({ page })
|
|
.withTags(['wcag2a', 'wcag2aa'])
|
|
.analyze();
|
|
return results.violations;
|
|
}
|
|
catch {
|
|
// Fallback: try via page.evaluate if AxeBuilder fails
|
|
const results = await page.evaluate(async () => {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const win = window;
|
|
if (typeof win.axe === 'undefined')
|
|
return [];
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
const r = await win.axe.run();
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
return r.violations;
|
|
}).catch(() => []);
|
|
return Array.isArray(results) ? results : [];
|
|
}
|
|
}
|
|
}
|
|
exports.AccessibilityCollector = AccessibilityCollector;
|