/ opencodehub / tests / e2e / accessibility.spec.ts
Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
292 lines · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * Accessibility E2E Tests — WCAG 2.1 AA compliance
 *
 * Uses @axe-core/playwright to automatically detect accessibility violations
 * across key pages. Covers:
 * - WCAG 2.4.1: Skip navigation / bypass blocks
 * - WCAG 1.1.1: Non-text content (alt text, aria-labels)
 * - WCAG 1.3.1: Info and relationships (semantic HTML, ARIA roles)
 * - WCAG 1.4.3: Contrast (minimum 4.5:1)
 * - WCAG 2.1.1: Keyboard navigable
 * - WCAG 4.1.2: Name, role, value (ARIA attributes)
 */
import AxeBuilder from "@axe-core/playwright";
import { expect, test, type Page } from "@playwright/test";

interface AxeViolation {
  id: string;
  impact?: string;
  description: string;
  nodes: unknown[];
}

// Pages to test for accessibility
const PUBLIC_PAGES = [
  { name: "Home", path: "/" },
  { name: "Explore", path: "/explore" },
  { name: "Login", path: "/login" },
  { name: "Register", path: "/register" },
  // /docs/ redirects to external docs.opencodehub.space — skip in CI
];

// Authenticated pages (need a seeded user + session; these fail fast with a
// login redirect otherwise, which is still an a11y-valid page).
const AUTH_PAGES = [
  { name: "Dashboard", path: "/dashboard", expectRedirect: true },
  { name: "Notifications", path: "/notifications", expectRedirect: true },
  { name: "Settings", path: "/settings/profile", expectRedirect: true },
  { name: "Organizations", path: "/orgs", expectRedirect: true },
];

/**
 * Run axe-core on a page and return the results.
 * Tags filter ensures we check WCAG 2.1 AA rules.
 */
async function runAxe(page: Page) {
  return new AxeBuilder({ page })
    .withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"])
    .options({ rules: { "color-contrast": { enabled: false } } }) // Exclude subpixel antialiasing contrast heuristics in headless chrome
    .exclude(".toaster") // Exclude transient toast notifications
    .analyze();
}

test.describe("Accessibility — WCAG 2.1 AA compliance", () => {
  for (const { name, path } of PUBLIC_PAGES) {
    test(`${name} page (${path}) has no critical accessibility violations`, async ({
      page,
    }) => {
      await page.goto(path, { waitUntil: "networkidle" });

      const results = await runAxe(page);

      // Filter to critical and serious violations only
      const critical = (results.violations as AxeViolation[]).filter(
        (v: AxeViolation) => v.impact === "critical" || v.impact === "serious",
      );

      if (critical.length > 0) {
        const summary = critical
          .map(
            (v: AxeViolation) =>
              `[${v.impact}] ${v.id}: ${v.description} (${v.nodes.length} occurrences)`,
          )
          .join("\n");
        console.error(`Accessibility violations on ${name}:\n${summary}`);
      }

      expect(
        critical,
        `Expected no critical/serious a11y violations on ${name}`,
      ).toHaveLength(0);
    });
  }

  // Authenticated pages: when unauthenticated they redirect to /login
  // (still axe-scanned); with a session they render the full page. Either
  // way critical/serious violations must be zero.
  for (const { name, path } of AUTH_PAGES) {
    test(`${name} page (${path}) has no critical accessibility violations`, async ({
      page,
    }) => {
      await page.goto(path, { waitUntil: "networkidle" });

      const results = await runAxe(page);
      const critical = (results.violations as AxeViolation[]).filter(
        (v: AxeViolation) => v.impact === "critical" || v.impact === "serious",
      );
      if (critical.length > 0) {
        const summary = critical
          .map(
            (v: AxeViolation) =>
              `[${v.impact}] ${v.id}: ${v.description} (${v.nodes.length} occurrences)`,
          )
          .join("\n");
        console.error(`Accessibility violations on ${name}:\n${summary}`);
      }
      expect(
        critical,
        `Expected no critical/serious a11y violations on ${name}`,
      ).toHaveLength(0);
    });
  }

  test("Skip-to-content link is present and functional", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    // The skip link should exist
    const skipLink = page.locator('a[href="#main-content"]');
    await expect(skipLink).toBeAttached();

    // Should be visually hidden by default
    const box = await skipLink.boundingBox();
    // sr-only makes it 1x1px or off-screen — either way not visible
    expect(box === null || box.width <= 1 || box.height <= 1).toBeTruthy();

    // Focus the skip link and verify it becomes visible
    await skipLink.focus();
    await expect(skipLink).toBeFocused();

    // After focus, the sr-only class should be overridden by focus:not-sr-only
    const focusedBox = await skipLink.boundingBox();
    expect(focusedBox).not.toBeNull();
    if (focusedBox) {
      expect(focusedBox.width).toBeGreaterThan(10);
      expect(focusedBox.height).toBeGreaterThan(10);
    }

    // Main content target should exist
    const mainContent = page.locator("#main-content");
    await expect(mainContent).toBeAttached();
    await expect(mainContent).toHaveAttribute("role", "main");
  });

  test("Header has proper ARIA landmarks and roles", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    // Banner role on header
    const header = page.locator('header[role="banner"]');
    await expect(header).toBeAttached();

    // Main navigation
    const mainNav = page.locator('nav[aria-label="Main navigation"]');
    await expect(mainNav).toBeAttached();

    // Logo has accessible label
    const logoLink = page.locator('a[aria-label="OpenCodeHub home"]');
    await expect(logoLink).toBeAttached();

    // Search region
    const searchRegion = page.locator('[role="search"]');
    await expect(searchRegion).toBeAttached();
  });

  test("Footer has proper semantic structure", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    // Footer with contentinfo role
    const footer = page.locator('footer[role="contentinfo"]');
    await expect(footer).toBeAttached();

    // Footer navigation
    const footerNav = page.locator('nav[aria-label="Footer navigation"]');
    await expect(footerNav).toBeAttached();

    // External link has proper labeling
    const githubLink = page.locator(
      'a[aria-label="GitHub repository (opens in new tab)"]',
    );
    await expect(githubLink).toBeAttached();
    await expect(githubLink).toHaveAttribute("target", "_blank");
    await expect(githubLink).toHaveAttribute("rel", /noopener/);
  });

  test("Dropdown menus have proper ARIA attributes", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    // Create menu button
    const createBtn = page.locator('#create-menu button[aria-haspopup="true"]');
    // May not be visible if user not logged in — check if present
    const createBtnCount = await createBtn.count();

    if (createBtnCount > 0) {
      await expect(createBtn).toHaveAttribute("aria-expanded", "false");
      await expect(createBtn).toHaveAttribute(
        "aria-controls",
        "create-dropdown",
      );

      // The dropdown should have role="menu"
      const createDropdown = page.locator('#create-dropdown[role="menu"]');
      await expect(createDropdown).toBeAttached();

      // Menu items should have role="menuitem"
      const menuItems = createDropdown.locator('[role="menuitem"]');
      expect(await menuItems.count()).toBeGreaterThan(0);
    }
  });

  test("Mobile menu button has proper ARIA attributes", async ({ page }) => {
    // Set mobile viewport BEFORE navigation
    await page.setViewportSize({ width: 375, height: 812 });
    await page.goto("/", { waitUntil: "networkidle" });

    const mobileBtn = page.locator("#mobile-menu-btn");
    await expect(mobileBtn).toBeVisible({ timeout: 5000 });
    await expect(mobileBtn).toHaveAttribute("aria-label", "Menu");
    await expect(mobileBtn).toHaveAttribute("aria-expanded", "false");

    // Click to open
    await mobileBtn.click();
    await expect(mobileBtn).toHaveAttribute("aria-expanded", "true");

    // Mobile nav should be visible
    const mobileNav = page.locator("#mobile-nav");
    await expect(mobileNav).toBeVisible();
    await expect(mobileNav).toHaveAttribute("role", "dialog");
  });

  test("Keyboard navigation: Escape closes open dropdowns", async ({
    page,
  }) => {
    // Mobile viewport BEFORE navigation to test mobile menu
    await page.setViewportSize({ width: 375, height: 812 });
    await page.goto("/", { waitUntil: "networkidle" });

    const mobileBtn = page.locator("#mobile-menu-btn");
    await expect(mobileBtn).toBeVisible({ timeout: 5000 });
    await mobileBtn.click();

    const mobileNav = page.locator("#mobile-nav");
    await expect(mobileNav).toBeVisible();

    // Press Escape
    await page.keyboard.press("Escape");
    await expect(mobileNav).not.toBeVisible();
    await expect(mobileBtn).toHaveAttribute("aria-expanded", "false");
  });

  test("All images have alt text", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    const images = page.locator("img");
    const count = await images.count();

    for (let i = 0; i < count; i++) {
      const img = images.nth(i);
      const alt = await img.getAttribute("alt");
      const src = await img.getAttribute("src");
      expect(
        alt !== null && alt !== undefined,
        `Image ${src} is missing alt attribute`,
      ).toBeTruthy();
    }
  });

  test("Interactive elements are keyboard focusable", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    const skipLink = page.locator('a[href="#main-content"]');
    await skipLink.focus();
    await expect(skipLink).toBeFocused();

    await page.keyboard.press("Tab");
    const logo = page.locator('a[aria-label="OpenCodeHub home"]');
    await expect(logo).toBeFocused();
  });

  test("Page has exactly one main landmark", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    const mainElements = page.locator("main, [role='main']");
    const count = await mainElements.count();
    expect(count).toBe(1);
  });

  test("HTML lang attribute is set", async ({ page }) => {
    await page.goto("/", { waitUntil: "networkidle" });

    const lang = await page.locator("html").getAttribute("lang");
    expect(lang).toBe("en");
  });
});