- tsconfig (allowJs, strict .ts), npm run build -> dist/, typecheck, dev via tsx, ts-jest. - src/index.js is now a launcher (dist build when it matches, else tsx); server moved to app.js. - /api/v2: defineRoute with zod -> request validation, response stripping, OpenAPI 3.1 at /docs/v2; one error shape and one paged-list shape. First endpoints: events, event, me, my registrations. - deploy.sh/install.sh build the backend; rollback to older releases skips the build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
49 lines
2.6 KiB
TypeScript
49 lines
2.6 KiB
TypeScript
import { test, expect, seed, API } from "./fixtures";
|
|
|
|
// /api/v2 (1.51.0) against the real server and database: paged public events, the v2 error
|
|
// shape, the signed-in person, and the OpenAPI reference at /docs/v2.
|
|
test.describe("API v2", () => {
|
|
test("public events come a page at a time; errors have a code", async ({ request }) => {
|
|
const res = await request.get(`${API}/api/v2/events?limit=2`);
|
|
expect(res.ok()).toBe(true);
|
|
const body = await res.json();
|
|
expect(body.data).toHaveLength(2);
|
|
expect(body.meta).toMatchObject({ page: 1, limit: 2 });
|
|
expect(body.meta.total).toBeGreaterThan(2);
|
|
|
|
const search = await (await request.get(`${API}/api/v2/events?q=free%20evening`)).json();
|
|
expect(search.data.map((e: { title: string }) => e.title)).toEqual(["E2E Free Evening"]);
|
|
|
|
const one = await (await request.get(`${API}/api/v2/events/${seed().events.paid}`)).json();
|
|
expect(one.data.ticketOptions.length).toBeGreaterThan(0);
|
|
|
|
const bad = await request.get(`${API}/api/v2/events?limit=1000`);
|
|
expect(bad.status()).toBe(400);
|
|
expect((await bad.json()).error).toMatchObject({ code: "VALIDATION_FAILED", fields: { limit: expect.any(String) } });
|
|
|
|
const missing = await request.get(`${API}/api/v2/events/00000000-0000-4000-8000-000000000000`);
|
|
expect((await missing.json()).error.code).toBe("NOT_FOUND");
|
|
});
|
|
|
|
test("me and my registrations need a token", async ({ request }) => {
|
|
expect((await request.get(`${API}/api/v2/me`)).status()).toBe(401);
|
|
const headers = { Authorization: `Bearer ${seed().users.user.token}` };
|
|
const me = await (await request.get(`${API}/api/v2/me`, { headers })).json();
|
|
expect(me.data).toMatchObject({ id: seed().users.user.id, role: "user" });
|
|
const regs = await (await request.get(`${API}/api/v2/me/registrations?past=true`, { headers })).json();
|
|
expect(regs.meta).toMatchObject({ page: 1, limit: 25 });
|
|
expect(regs.data.length).toBe(Math.min(25, regs.meta.total));
|
|
});
|
|
|
|
test("an admin can read the OpenAPI reference at /docs/v2", async ({ page, request }) => {
|
|
const headers = { Authorization: `Bearer ${seed().users.user.token}` };
|
|
expect((await request.get(`${API}/api/v2/openapi.json`, { headers })).status()).toBe(403);
|
|
|
|
await page.goto(`${API}/docs/v2?token=${seed().users.admin.token}`);
|
|
await expect(page.getByRole("heading", { name: /Cross Events API/ })).toBeVisible();
|
|
await expect(page.locator(".opblock-summary-path", { hasText: "/events/{id}" })).toBeVisible();
|
|
// The token isn't left in the address bar.
|
|
expect(page.url()).not.toContain("token=");
|
|
});
|
|
});
|