Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /tests /test_accessibility_audit.py
| """Accessibility audit tests -- verify WCAG AA compliance for HTML and CLI. | |
| Checks that HTML templates include required accessibility attributes, | |
| CLI output remains readable without ANSI colors, error messages are | |
| actionable, help text includes examples, JSON output is valid, and | |
| i18n translations are functional. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import re | |
| import tempfile | |
| from pathlib import Path | |
| import pytest | |
| # ── HTML template accessibility ─────────────────────────────────────────── | |
| _API_TEMPLATE_DIR = Path(__file__).resolve().parent.parent / "src" / "faultray" / "api" / "templates" | |
| _REPORTER_TEMPLATE_DIR = Path(__file__).resolve().parent.parent / "src" / "faultray" / "reporter" / "templates" | |
| def _all_html_templates() -> list[Path]: | |
| """Collect all HTML template files from both template directories.""" | |
| templates = [] | |
| for d in (_API_TEMPLATE_DIR, _REPORTER_TEMPLATE_DIR): | |
| if d.exists(): | |
| templates.extend(d.glob("*.html")) | |
| return sorted(templates) | |
| def test_all_html_templates_have_lang(): | |
| """All standalone HTML templates should have a lang attribute on <html>.""" | |
| templates = _all_html_templates() | |
| assert templates, "No HTML templates found" | |
| missing_lang: list[str] = [] | |
| for tmpl in templates: | |
| content = tmpl.read_text(errors="replace") | |
| # Skip partial templates (those that extend a base) | |
| if "{% extends" in content and "<html" not in content: | |
| continue | |
| if "<html" in content.lower() and 'lang=' not in content.lower(): | |
| missing_lang.append(tmpl.name) | |
| assert not missing_lang, ( | |
| f"HTML templates missing lang attribute: {missing_lang}" | |
| ) | |
| def test_all_images_have_alt(): | |
| """All <img> tags in templates should have alt text.""" | |
| templates = _all_html_templates() | |
| img_no_alt: list[str] = [] | |
| img_pattern = re.compile(r'<img\b[^>]*>', re.IGNORECASE) | |
| alt_pattern = re.compile(r'\balt\s*=', re.IGNORECASE) | |
| for tmpl in templates: | |
| content = tmpl.read_text(errors="replace") | |
| for match in img_pattern.finditer(content): | |
| tag = match.group(0) | |
| if not alt_pattern.search(tag): | |
| img_no_alt.append(f"{tmpl.name}: {tag[:80]}") | |
| assert not img_no_alt, ( | |
| "Images without alt attribute:\n" + "\n".join(img_no_alt) | |
| ) | |
| def test_all_forms_have_labels(): | |
| """All form <input> elements should have associated labels or aria-label.""" | |
| templates = _all_html_templates() | |
| unlabeled: list[str] = [] | |
| # Match input tags that are not hidden and not submit/button | |
| input_pattern = re.compile( | |
| r'<input\b[^>]*>', re.IGNORECASE | |
| ) | |
| label_attrs = re.compile( | |
| r'(?:aria-label|aria-labelledby|id\s*=|placeholder\s*=|type\s*=\s*["\'](?:hidden|submit|button|reset))', | |
| re.IGNORECASE, | |
| ) | |
| for tmpl in templates: | |
| content = tmpl.read_text(errors="replace") | |
| for match in input_pattern.finditer(content): | |
| tag = match.group(0) | |
| if not label_attrs.search(tag): | |
| unlabeled.append(f"{tmpl.name}: {tag[:80]}") | |
| # Informational -- many templates use placeholder as implicit label | |
| if unlabeled: | |
| pytest.skip( | |
| "Inputs without explicit labels (review needed):\n" | |
| + "\n".join(unlabeled[:5]) | |
| ) | |
| def test_all_interactive_elements_keyboard_accessible(): | |
| """Buttons and links should have native focusability or tabindex.""" | |
| templates = _all_html_templates() | |
| issues: list[str] = [] | |
| # Check for clickable divs/spans without keyboard support | |
| onclick_no_keyboard = re.compile( | |
| r'<(?:div|span)\b[^>]*onclick\s*=', | |
| re.IGNORECASE, | |
| ) | |
| tabindex_or_role = re.compile( | |
| r'(?:tabindex|role\s*=\s*["\']button)', | |
| re.IGNORECASE, | |
| ) | |
| for tmpl in templates: | |
| content = tmpl.read_text(errors="replace") | |
| for match in onclick_no_keyboard.finditer(content): | |
| tag = match.group(0) | |
| if not tabindex_or_role.search(tag): | |
| issues.append(f"{tmpl.name}: div/span with onclick but no tabindex/role") | |
| if issues: | |
| pytest.skip( | |
| "Interactive elements without keyboard support:\n" | |
| + "\n".join(issues[:5]) | |
| ) | |
| def test_color_contrast_css_variables(): | |
| """CSS color variables should define both foreground and background colors.""" | |
| # We check that the reporter template defines --text-primary and --bg-primary | |
| report_html = _REPORTER_TEMPLATE_DIR / "report.html" | |
| if not report_html.exists(): | |
| pytest.skip("report.html template not found") | |
| content = report_html.read_text(errors="replace") | |
| assert "--text-primary" in content, "Missing --text-primary CSS variable" | |
| assert "--bg-primary" in content, "Missing --bg-primary CSS variable" | |
| # Ensure text and background colors are distinct | |
| text_match = re.search(r'--text-primary:\s*(#[0-9a-fA-F]+)', content) | |
| bg_match = re.search(r'--bg-primary:\s*(#[0-9a-fA-F]+)', content) | |
| if text_match and bg_match: | |
| assert text_match.group(1) != bg_match.group(1), ( | |
| "Text and background colors are identical -- no contrast" | |
| ) | |
| # ── CLI output without color ────────────────────────────────────────────── | |
| def test_cli_output_works_without_color(): | |
| """CLI should produce readable output when color is disabled (no raw ANSI codes).""" | |
| import os | |
| # Set NO_COLOR to suppress ANSI codes | |
| env_backup = os.environ.get("NO_COLOR") | |
| os.environ["NO_COLOR"] = "1" | |
| try: | |
| from typer.testing import CliRunner | |
| from faultray.cli import app | |
| runner = CliRunner() | |
| result = runner.invoke(app, ["demo"]) | |
| # Output should not contain raw ANSI escape sequences | |
| # (Rich/Typer should respect NO_COLOR) | |
| # Note: CliRunner may strip some codes; we check the actual output | |
| if result.output: | |
| # Allow a mild amount of ANSI codes from Rich which may not honor NO_COLOR | |
| # in the test runner, but ensure basic readability | |
| assert len(result.output) > 0 | |
| finally: | |
| if env_backup is None: | |
| os.environ.pop("NO_COLOR", None) | |
| else: | |
| os.environ["NO_COLOR"] = env_backup | |
| # ── Error messages are actionable ───────────────────────────────────────── | |
| def test_error_messages_are_actionable(): | |
| """Error messages should tell user what to do, not just what went wrong.""" | |
| from typer.testing import CliRunner | |
| from faultray.cli import app | |
| runner = CliRunner() | |
| result = runner.invoke(app, ["simulate", "--model", "/nonexistent/file.json"]) | |
| output = result.output.lower() | |
| # Should contain a suggestion on how to proceed | |
| suggestion_words = ["try", "run", "use", "check", "scan", "quickstart", "demo", "first", "create"] | |
| assert any(word in output for word in suggestion_words), ( | |
| f"Error message lacks actionable guidance: {result.output[:200]}" | |
| ) | |
| # ── Help text has examples ──────────────────────────────────────────────── | |
| def test_help_text_has_examples(): | |
| """Key commands should have usage examples in their help text.""" | |
| from typer.testing import CliRunner | |
| from faultray.cli import app | |
| runner = CliRunner() | |
| commands_to_check = ["simulate", "evaluate", "scan", "plan"] | |
| missing_examples: list[str] = [] | |
| for cmd in commands_to_check: | |
| result = runner.invoke(app, [cmd, "--help"]) | |
| output = result.output.lower() | |
| has_example = ( | |
| "example" in output | |
| or "usage" in output | |
| or "faultray" in output | |
| or "faultray" in output | |
| or "$" in result.output # shell prompt example | |
| ) | |
| if not has_example: | |
| missing_examples.append(cmd) | |
| assert not missing_examples, ( | |
| f"Commands missing examples in --help: {missing_examples}" | |
| ) | |
| # ── JSON output validity ───────────────────────────────────────────────── | |
| def test_json_output_is_valid(): | |
| """Commands with --json should produce valid JSON.""" | |
| from typer.testing import CliRunner | |
| from faultray.cli import app | |
| from faultray.model.demo import create_demo_graph | |
| runner = CliRunner() | |
| # Create a temporary model file | |
| with tempfile.TemporaryDirectory() as tmp: | |
| graph = create_demo_graph() | |
| model_path = Path(tmp) / "model.json" | |
| graph.save(model_path) | |
| # Test simulate --json | |
| result = runner.invoke(app, ["simulate", "--model", str(model_path), "--json"]) | |
| if result.exit_code == 0 and result.output.strip(): | |
| _assert_contains_valid_json(result.output, "simulate --json") | |
| # Test carbon --json (if it exists) | |
| result = runner.invoke(app, ["carbon", "--model", str(model_path), "--json"]) | |
| if result.exit_code == 0 and result.output.strip(): | |
| _assert_contains_valid_json(result.output, "carbon --json") | |
| # Test risk --json (if it exists) | |
| result = runner.invoke(app, ["risk", "--model", str(model_path), "--json"]) | |
| if result.exit_code == 0 and result.output.strip(): | |
| _assert_contains_valid_json(result.output, "risk --json") | |
| def _assert_contains_valid_json(output: str, context: str) -> None: | |
| """Assert that the output contains at least one valid JSON line.""" | |
| for line in output.strip().split("\n"): | |
| line = line.strip() | |
| if line.startswith("{") or line.startswith("["): | |
| try: | |
| json.loads(line) | |
| return # Found valid JSON | |
| except json.JSONDecodeError: | |
| continue | |
| # If we get here, no valid JSON was found -- but only fail if output | |
| # looked like it was trying to be JSON | |
| if "{" in output or "[" in output: | |
| # Try parsing the entire output as JSON | |
| try: | |
| json.loads(output.strip()) | |
| return | |
| except json.JSONDecodeError: | |
| pytest.fail(f"No valid JSON found in {context} output: {output[:200]}") | |
| # ── i18n support ────────────────────────────────────────────────────────── | |
| def test_i18n_japanese_messages(): | |
| """Japanese messages should be available and correct.""" | |
| from faultray.i18n import get_language, set_language, t | |
| original_lang = get_language() | |
| try: | |
| set_language("ja") | |
| assert get_language() == "ja" | |
| # Key translation should return Japanese, not the raw key | |
| msg = t("resilience_score") | |
| assert msg != "resilience_score", ( | |
| f"Expected Japanese translation, got raw key: {msg}" | |
| ) | |
| # Check it contains Japanese characters | |
| assert any("\u3000" <= c <= "\u9fff" or "\u30a0" <= c <= "\u30ff" for c in msg), ( | |
| f"Expected Japanese characters in translation: {msg}" | |
| ) | |
| # Parameterised message | |
| scan_msg = t("scan_complete", count=5) | |
| assert "5" in scan_msg, f"Expected '5' in parameterised message: {scan_msg}" | |
| finally: | |
| set_language(original_lang) | |
| def test_i18n_fallback_to_english(): | |
| """Unknown language should fall back to English.""" | |
| from faultray.i18n import get_language, set_language, t | |
| original_lang = get_language() | |
| try: | |
| set_language("xx") # Nonexistent language | |
| # Should fall back to English | |
| assert get_language() == "en" | |
| msg = t("resilience_score") | |
| assert msg == "Resilience Score" | |
| finally: | |
| set_language(original_lang) | |
| def test_i18n_unknown_key_returns_key(): | |
| """Unknown translation key should return the key itself.""" | |
| from faultray.i18n import t | |
| result = t("nonexistent_key_xyz_12345") | |
| assert result == "nonexistent_key_xyz_12345" | |
| # ── HTML report accessibility ───────────────────────────────────────────── | |
| def test_html_report_has_semantic_structure(): | |
| """Generated HTML report should use semantic HTML elements.""" | |
| from faultray.model.demo import create_demo_graph | |
| from faultray.reporter.html_report import generate_html_report | |
| from faultray.simulator.engine import SimulationEngine | |
| graph = create_demo_graph() | |
| engine = SimulationEngine(graph) | |
| report = engine.run_all_defaults() | |
| html = generate_html_report(report, graph) | |
| # Should have basic semantic structure | |
| assert "<!DOCTYPE html>" in html or "<!doctype html>" in html | |
| assert "<html" in html | |
| assert "lang=" in html, "HTML report missing lang attribute" | |
| assert "<title>" in html, "HTML report missing <title>" | |
| assert "charset" in html.lower(), "HTML report missing charset declaration" | |
Xet Storage Details
- Size:
- 13.3 kB
- Xet hash:
- b0e1ca811030d8df4f58964b25e6e202190643b31fdcdd037b3323e8e5d2c966
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.