Add Playwright E2E test suite (25 tests across 4 specs)
- playwright.config.ts: headless CI setup with JSON/HTML reporters - e2e/global-setup.ts: app reachability check before tests - e2e/auth.spec.ts: 6 auth tests (sign-in, session, protected routes) - e2e/compare.spec.ts: 8 API tests (validation, SSE flow, DB persistence) - e2e/comparisons.spec.ts: 6 CRUD tests (list, detail, 404, view counts) - e2e/user.spec.ts: 5 user tests (stats, pagination, session binding) - e2e/helpers.ts: shared SSE parsing, auth, URL resolution utilities - e2e/README.md: setup and run instructions - AGENTS.md: updated with LLM provider notes and testing docs - .gitignore: added playwright report/result directories - package.json: added @playwright/test and test:e2e scripts
This commit is contained in:
77
e2e/user.spec.ts
Normal file
77
e2e/user.spec.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
import { resolveUrl, getSessionCookie } from "./helpers";
|
||||
|
||||
/**
|
||||
* E2E tests for user endpoints: stats, profile, and account management.
|
||||
*/
|
||||
test.describe("User API", () => {
|
||||
let cookie: string;
|
||||
|
||||
test.beforeEach(async ({ page }) => {
|
||||
cookie = await getSessionCookie(page);
|
||||
});
|
||||
|
||||
test("1. User stats returns 200 for authenticated user", async ({ page }) => {
|
||||
const res = await page.request.get(resolveUrl("/api/user/stats"), {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
expect(res.status()).toBe(200);
|
||||
const data = await res.json();
|
||||
expect(data).toHaveProperty("totalComparisons");
|
||||
expect(data).toHaveProperty("totalViews");
|
||||
expect(typeof data.totalComparisons).toBe("number");
|
||||
expect(typeof data.totalViews).toBe("number");
|
||||
});
|
||||
|
||||
test("2. User stats requires auth", async ({ page }) => {
|
||||
const res = await page.request.get(resolveUrl("/api/user/stats"));
|
||||
expect(res.status()).toBe(401);
|
||||
});
|
||||
|
||||
test("3. User stats increments after new comparison", async ({ page }) => {
|
||||
const beforeRes = await page.request.get(resolveUrl("/api/user/stats"), {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
const before = await beforeRes.json();
|
||||
const countBefore = before.totalComparisons ?? 0;
|
||||
|
||||
// Create a new comparison
|
||||
const createRes = await page.request.post(resolveUrl("/api/compare"), {
|
||||
data: { items: ["TypeScript", "JavaScript"] },
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: cookie,
|
||||
Accept: "text/event-stream",
|
||||
},
|
||||
timeout: 120_000,
|
||||
});
|
||||
|
||||
if (createRes.status() === 200) {
|
||||
// Wait briefly for DB write
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
|
||||
const afterRes = await page.request.get(resolveUrl("/api/user/stats"), {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
const after = await afterRes.json();
|
||||
expect(after.totalComparisons).toBeGreaterThanOrEqual(countBefore);
|
||||
}
|
||||
});
|
||||
|
||||
test("4. User comparisons list pagination works", async ({ page }) => {
|
||||
const res = await page.request.get(resolveUrl("/api/user/comparisons?page=1&limit=5"), {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
expect(res.status()).toBe(200);
|
||||
});
|
||||
|
||||
test("5. Auth session is bound to correct user", async ({ page }) => {
|
||||
const res = await page.request.get(resolveUrl("/api/user/stats"), {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
const data = await res.json();
|
||||
// Stats should be non-negative integers
|
||||
expect(data.totalComparisons).toBeGreaterThanOrEqual(0);
|
||||
expect(data.totalViews).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user