mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 10:40:42 +08:00
Web audit scaffolding: add Playwright config, fix tsconfig types, harden error fallback check and baseURL handling in compliance suite; add audit scripts and devdep for @playwright/test.
This commit is contained in:
@@ -6,7 +6,10 @@
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint"
|
||||
"lint": "next lint",
|
||||
"audit": "playwright test --project=ui-audit",
|
||||
"audit:ui": "playwright test --ui --project=ui-audit",
|
||||
"audit:report": "playwright show-report"
|
||||
},
|
||||
"dependencies": {
|
||||
"clsx": "^2.1.1",
|
||||
@@ -27,6 +30,7 @@
|
||||
"tailwind-merge": "^3.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "^1.49.0",
|
||||
"@types/node": "^25",
|
||||
"@types/react": "^19",
|
||||
"@types/react-dom": "^19",
|
||||
|
||||
25
web/playwright.config.ts
Normal file
25
web/playwright.config.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { defineConfig, devices } from "@playwright/test";
|
||||
|
||||
const BASE_URL =
|
||||
process.env.WEB_BASE_URL || process.env.NEXT_PUBLIC_API_BASE || "http://localhost:3000";
|
||||
const SERIAL_MODE = process.env.PW_SERIAL === "1";
|
||||
|
||||
export default defineConfig({
|
||||
testDir: "./tests",
|
||||
fullyParallel: !SERIAL_MODE,
|
||||
forbidOnly: !!process.env.CI,
|
||||
retries: process.env.CI ? 2 : 0,
|
||||
workers: SERIAL_MODE ? 1 : undefined,
|
||||
reporter: [["html", { open: "never" }], ["list"]],
|
||||
use: {
|
||||
baseURL: BASE_URL,
|
||||
trace: "on-first-retry",
|
||||
},
|
||||
projects: [
|
||||
{
|
||||
name: "ui-audit",
|
||||
testMatch: "**/*.audit.ts",
|
||||
use: { ...devices["Desktop Chrome"] },
|
||||
},
|
||||
],
|
||||
});
|
||||
86
web/tests/e2e/compliance-and-ux.audit.ts
Normal file
86
web/tests/e2e/compliance-and-ux.audit.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
|
||||
// Minimal compliance and UX checks without external deps (axe-core)
|
||||
// Focus on semantic landmarks, headings, alt text, link names, and basic error messaging.
|
||||
|
||||
const BASE_URL =
|
||||
process.env.WEB_BASE_URL || process.env.NEXT_PUBLIC_API_BASE || "http://localhost:3000";
|
||||
|
||||
async function expectAnyVisible(locators: import("@playwright/test").Locator[], message: string) {
|
||||
for (const loc of locators) {
|
||||
try {
|
||||
if (await loc.first().isVisible()) return;
|
||||
} catch {}
|
||||
}
|
||||
expect(false, message).toBe(true);
|
||||
}
|
||||
|
||||
test.describe("Compliance :: Accessibility & Semantics", () => {
|
||||
test("home page exposes main landmark and H1", async ({ page }) => {
|
||||
await page.goto(`${BASE_URL}/`);
|
||||
await page.waitForSelector("main", { timeout: 5000 });
|
||||
const main = page.locator("main");
|
||||
await expect(main, "Missing <main> landmark").toBeVisible();
|
||||
|
||||
const h1 = page.locator("h1").first();
|
||||
await expect(h1, "Missing top-level <h1>").toBeVisible();
|
||||
});
|
||||
|
||||
test("images provide alt text", async ({ page }) => {
|
||||
await page.goto(`${BASE_URL}/`);
|
||||
const missingAltCount = await page.$$eval(
|
||||
"img",
|
||||
(imgs) =>
|
||||
imgs.filter((img) => {
|
||||
const alt = img.getAttribute("alt");
|
||||
return !alt || alt.trim().length === 0;
|
||||
}).length
|
||||
);
|
||||
expect(missingAltCount, `Found ${missingAltCount} <img> without alt`).toBe(0);
|
||||
});
|
||||
|
||||
test("links have accessible names (text/aria-label/title)", async ({ page }) => {
|
||||
await page.goto(`${BASE_URL}/`);
|
||||
const namelessLinks = await page.$$eval(
|
||||
"a",
|
||||
(anchors) =>
|
||||
anchors.filter((a) => {
|
||||
const text = (a.textContent || "").trim();
|
||||
const aria = (a.getAttribute("aria-label") || "").trim();
|
||||
const title = (a.getAttribute("title") || "").trim();
|
||||
return text.length === 0 && aria.length === 0 && title.length === 0;
|
||||
}).length
|
||||
);
|
||||
expect(namelessLinks, `Found ${namelessLinks} <a> without accessible name`).toBe(0);
|
||||
});
|
||||
|
||||
test("viewport meta present for responsive UX", async ({ page }) => {
|
||||
await page.goto(`${BASE_URL}/`);
|
||||
const hasViewport = await page.$('meta[name="viewport"]');
|
||||
expect(!!hasViewport, "Missing viewport meta tag").toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe("Compliance :: Error Handling & UX Signals", () => {
|
||||
test("api error surfaces user-friendly feedback (alert or message)", async ({ page }) => {
|
||||
await page.route("**/api/v1/notebook/list", (route) =>
|
||||
route.fulfill({
|
||||
status: 500,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ detail: "Simulated Backend Failure" }),
|
||||
})
|
||||
);
|
||||
|
||||
await page.goto(`${BASE_URL}/notebook`);
|
||||
|
||||
await expectAnyVisible(
|
||||
[
|
||||
page.locator('[role="alert"]'),
|
||||
page.locator('[data-test="notebooks-empty"]'),
|
||||
page.locator('[data-testid="notebooks-empty"]'),
|
||||
page.locator('text=No notebooks'),
|
||||
],
|
||||
"Expected error banner or empty state after simulated failure"
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -13,14 +13,9 @@
|
||||
"isolatedModules": true,
|
||||
"jsx": "react-jsx",
|
||||
"incremental": true,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
"plugins": [{ "name": "next" }],
|
||||
"paths": { "@/*": ["./*"] },
|
||||
"types": ["node", "@playwright/test"]
|
||||
},
|
||||
"include": [
|
||||
"next-env.d.ts",
|
||||
|
||||
Reference in New Issue
Block a user