🧪 Playwright e2e suite and wider API contract coverage
Browse files at this commitbb1fd0dc13d76fc0b3e553ffc46400fa03f34e8cParent: e7c0151- feat
- 🧪 Playwright e2e suite and wider API contract coverage
11 changed files +242 -11
- .gitignore +2 -0
- bun.lock +9 -0
- e2e/auth-shell.spec.ts +24 -0
- package.json +2 -0
- playwright.config.ts +21 -0
- src/api/__tests__/contract/bank-questions.contract.test.ts +2 -2
- src/api/__tests__/contract/content.contract.test.ts +45 -0
- src/api/__tests__/contract/core.contract.test.ts +33 -0
- src/api/__tests__/contract/helpers/live-client.ts +48 -9
- src/api/__tests__/contract/learning.contract.test.ts +53 -0
- vitest.contract.config.ts +3 -0
.gitignoremodified+2 -0
| @@ -27,6 +27,8 @@ dist-ssr | ||
| 27 | 27 | *.tsbuildinfo |
| 28 | 28 | .od-skills/ |
| 29 | 29 | critique.json |
| 30 | +playwright-report | |
| 31 | +test-results | |
| 30 | 32 | |
| 31 | 33 | # SurrealDB CLI REPL history (kb queries); see .claude/kb-sync.sh |
| 32 | 34 | history.txt |
bun.lockmodified+9 -0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | "tailwind-merge": "^3.6.0", |
| 16 | 16 | }, |
| 17 | 17 | "devDependencies": { |
| 18 | + "@playwright/test": "^1.62.1", | |
| 18 | 19 | "@solidjs/testing-library": "^0.8.10", |
| 19 | 20 | "@tailwindcss/postcss": "^4.3.3", |
| 20 | 21 | "@tanstack/solid-router-devtools": "^1.167.0", |
| @@ -206,6 +207,8 @@ | ||
| 206 | 207 | |
| 207 | 208 | "@oxc-project/types": ["@oxc-project/types@0.139.0", "", {}, "sha512-r9gHphtCs+1M7J0pw6Sn/hh/Wpa/iQrOOkrNAlVLF/gHq+/CJmHIWKKUUhdWjcD6CIa8idarspCsASiXCXvFUw=="], |
| 208 | 209 | |
| 210 | + "@playwright/test": ["@playwright/test@1.62.1", "", { "dependencies": { "playwright": "1.62.1" }, "bin": { "playwright": "cli.js" } }, "sha512-DTcUc8qii+cpHvtOwggMtBRMjKZHXYWdw8syRYu2vtzuq4Wxphqq4NfCs5Zt44L6mA8rfDfj+PHnxFc/FeK6mQ=="], | |
| 211 | + | |
| 209 | 212 | "@rolldown/binding-android-arm64": ["@rolldown/binding-android-arm64@1.1.5", "", { "os": "android", "cpu": "arm64" }, "sha512-lZg8fqIv2v7FF237bwMgzGZEJvGL79/s5knJ/i6FmsGF4XXlzccZ4jb+TrFIxtSSxFtIpdsgrPZeMk1I9AFcyQ=="], |
| 210 | 213 | |
| 211 | 214 | "@rolldown/binding-darwin-arm64": ["@rolldown/binding-darwin-arm64@1.1.5", "", { "os": "darwin", "cpu": "arm64" }, "sha512-51Bnx9pNiMRKSUNtBfySkNJ9vMU9Hh3I1ozDd6gyPPYzaXCfnptUcEZxXGYFn+ul2dtcMUiqGR1Yai2K10uoTw=="], |
| @@ -584,6 +587,10 @@ | ||
| 584 | 587 | |
| 585 | 588 | "picomatch": ["picomatch@4.0.5", "", {}, "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A=="], |
| 586 | 589 | |
| 590 | + "playwright": ["playwright@1.62.1", "", { "dependencies": { "playwright-core": "1.62.1" }, "optionalDependencies": { "fsevents": "2.3.2" }, "bin": { "playwright": "cli.js" } }, "sha512-0M+L3LAD8/nm554LOla9Ayx0j0tmFZ0FBcoQ7F1VuVHpM/XpiC8RcDzBQB8W5+hA8L22THxELzeF+2WcUzvcLg=="], | |
| 591 | + | |
| 592 | + "playwright-core": ["playwright-core@1.62.1", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-wPYSwEBJY9GHraISXqyqtx0na0LpO3XEX7jNDhntbex7tzUS7kLnZsOlFruFJB4Hi/rhDMjXGqHewDZ68nYZVw=="], | |
| 593 | + | |
| 587 | 594 | "postcss": ["postcss@8.5.22", "", { "dependencies": { "nanoid": "^3.3.16", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-KBDEIpLrvpv16pp3K0Fw+UCoZfopFjjgeB+0tA/aaThfEE74kKDLrgg603YvOWJyg3+WYtyq3xYsQWsIyZlPqQ=="], |
| 588 | 595 | |
| 589 | 596 | "pretty-format": ["pretty-format@27.5.1", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="], |
| @@ -708,6 +715,8 @@ | ||
| 708 | 715 | |
| 709 | 716 | "babel-plugin-jsx-dom-expressions/parse5": ["parse5@7.3.0", "", { "dependencies": { "entities": "^6.0.0" } }, "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw=="], |
| 710 | 717 | |
| 718 | + "playwright/fsevents": ["fsevents@2.3.2", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="], | |
| 719 | + | |
| 711 | 720 | "vitest/vite": ["vite@6.4.3", "", { "dependencies": { "esbuild": "^0.25.0", "fdir": "^6.4.4", "picomatch": "^4.0.2", "postcss": "^8.5.3", "rollup": "^4.34.9", "tinyglobby": "^0.2.13" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", "jiti": ">=1.21.0", "less": "*", "lightningcss": "^1.21.0", "sass": "*", "sass-embedded": "*", "stylus": "*", "sugarss": "*", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["less", "lightningcss", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": "bin/vite.js" }, "sha512-NTKlcQjlAK7MlQoyb6LgaqHc8sso/pVyUJYWMws3jg21uTJw/LddqIFPcPqP6PzpgbIcZyKI85sFE4HBrQDA8A=="], |
| 712 | 721 | |
| 713 | 722 | "@tailwindcss/node/lightningcss/lightningcss-android-arm64": ["lightningcss-android-arm64@1.32.0", "", { "os": "android", "cpu": "arm64" }, "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg=="], |
e2e/auth-shell.spec.tsadded+24 -0
| @@ -0,0 +1,24 @@ | ||
| 1 | +import { expect, test } from "@playwright/test"; | |
| 2 | + | |
| 3 | +const username = process.env.E2E_ADMIN_USERNAME ?? "admin"; | |
| 4 | +const password = process.env.E2E_ADMIN_PASSWORD ?? "admin123"; | |
| 5 | + | |
| 6 | +test("sign-in reaches the authenticated shell with keyboard-accessible navigation", async ({ page }) => { | |
| 7 | + await page.goto("/login"); | |
| 8 | + await expect(page.locator("#login-username")).toBeVisible(); | |
| 9 | + await page.locator("#login-username").fill(username); | |
| 10 | + await page.locator("#login-password").fill(password); | |
| 11 | + await page.locator("form").getByRole("button", { name: /log in|sign in|giriş yap/i }).click(); | |
| 12 | + | |
| 13 | + await expect(page).toHaveURL(/\/$/); | |
| 14 | + await expect(page.getByRole("main")).toBeVisible(); | |
| 15 | + | |
| 16 | + // The mobile tab bar is absent on desktop but must be reachable and named on | |
| 17 | + // a touch viewport. This catches accidental removal of its accessible nav. | |
| 18 | + if (test.info().project.name === "mobile") { | |
| 19 | + const navigation = page.getByRole("navigation", { name: /menu|menü/i }); | |
| 20 | + await expect(navigation).toBeVisible(); | |
| 21 | + await navigation.getByRole("button", { name: /account|hesap/i }).focus(); | |
| 22 | + await expect(navigation.getByRole("button", { name: /account|hesap/i })).toBeFocused(); | |
| 23 | + } | |
| 24 | +}); |
package.jsonmodified+2 -0
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | "preview": "vite preview", |
| 10 | 10 | "check": "tsc --noEmit", |
| 11 | 11 | "test": "vitest run", |
| 12 | + "test:e2e": "playwright test", | |
| 12 | 13 | "test:watch": "vitest", |
| 13 | 14 | "test:contract": "vitest run --config vitest.contract.config.ts", |
| 14 | 15 | "sync-skills": "bun run scripts/sync-skills.ts", |
| @@ -26,6 +27,7 @@ | ||
| 26 | 27 | "tailwind-merge": "^3.6.0" |
| 27 | 28 | }, |
| 28 | 29 | "devDependencies": { |
| 30 | + "@playwright/test": "^1.62.1", | |
| 29 | 31 | "@solidjs/testing-library": "^0.8.10", |
| 30 | 32 | "@tailwindcss/postcss": "^4.3.3", |
| 31 | 33 | "@tanstack/solid-router-devtools": "^1.167.0", |
playwright.config.tsadded+21 -0
| @@ -0,0 +1,21 @@ | ||
| 1 | +import { defineConfig, devices } from "@playwright/test"; | |
| 2 | + | |
| 3 | +export default defineConfig({ | |
| 4 | + testDir: "./e2e", | |
| 5 | + fullyParallel: false, | |
| 6 | + retries: process.env.CI ? 2 : 0, | |
| 7 | + use: { | |
| 8 | + baseURL: "http://127.0.0.1:4173", | |
| 9 | + trace: "on-first-retry", | |
| 10 | + }, | |
| 11 | + projects: [ | |
| 12 | + { name: "desktop", use: { ...devices["Desktop Chrome"] } }, | |
| 13 | + { name: "mobile", use: { ...devices["iPhone 13"], browserName: "chromium" } }, | |
| 14 | + ], | |
| 15 | + webServer: { | |
| 16 | + command: "bun run dev -- --host 127.0.0.1 --port 4173", | |
| 17 | + url: "http://127.0.0.1:4173/login", | |
| 18 | + reuseExistingServer: !process.env.CI, | |
| 19 | + timeout: 30_000, | |
| 20 | + }, | |
| 21 | +}); |
src/api/__tests__/contract/bank-questions.contract.test.tsmodified+2 -2
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { api, contractBaseUrl, isLive, json, loginAdmin, pngFile, SKIP_MESSAGE } from "./helpers/live-client"; | |
| 1 | +import { api, configuredExamKind, contractBaseUrl, isLive, json, loginAdmin, pngFile, SKIP_MESSAGE } from "./helpers/live-client"; | |
| 2 | 2 | import type { BankQuestion, Course, ExamQuestion, Subject } from "@/api/client"; |
| 3 | 3 | |
| 4 | 4 | if (!isLive) console.warn(SKIP_MESSAGE); |
| @@ -228,7 +228,7 @@ describe.skipIf(!isLive)(`bank-questions contract @ ${contractBaseUrl}`, () => { | ||
| 228 | 228 | const template = await createTemplate(`${tag} original text`); |
| 229 | 229 | const exam = await json<{ id: string }>(`/courses/${courseId}/exams`, { |
| 230 | 230 | method: "POST", |
| 231 | - body: { title: tag, kind: "final" }, | |
| 231 | + body: { title: tag, kind: await configuredExamKind() }, | |
| 232 | 232 | }); |
| 233 | 233 | |
| 234 | 234 | const copy = await json<ExamQuestion>( |
src/api/__tests__/contract/content.contract.test.tsadded+45 -0
| @@ -0,0 +1,45 @@ | ||
| 1 | +import { api, contractBaseUrl, expectPage, isLive, json, loginAdmin, pngFile, SKIP_MESSAGE } from "./helpers/live-client"; | |
| 2 | + | |
| 3 | +if (!isLive) console.warn(SKIP_MESSAGE); | |
| 4 | + | |
| 5 | +describe.skipIf(!isLive)(`content contract @ ${contractBaseUrl}`, () => { | |
| 6 | + let noteId = ""; | |
| 7 | + let eventId = ""; | |
| 8 | + | |
| 9 | + beforeAll(async () => { | |
| 10 | + await loginAdmin(); | |
| 11 | + const note = await json<{ id: string; title: string }>("/notes", { | |
| 12 | + method: "POST", | |
| 13 | + body: { title: `contract-note-${Date.now()}`, content: "contract body" }, | |
| 14 | + }); | |
| 15 | + noteId = note.id; | |
| 16 | + }); | |
| 17 | + | |
| 18 | + afterAll(async () => { | |
| 19 | + if (eventId) await api(`/events/${eventId}`, { method: "DELETE" }); | |
| 20 | + if (noteId) await api(`/notes/${noteId}`, { method: "DELETE" }); | |
| 21 | + }); | |
| 22 | + | |
| 23 | + it("supports note pagination and native multipart file lifecycle", async () => { | |
| 24 | + expectPage(await json("/notes?limit=1")); | |
| 25 | + const form = new FormData(); | |
| 26 | + form.append("file", pngFile("contract.png")); | |
| 27 | + const file = await json<{ id: string; content_type: string }>(`/notes/${noteId}/files`, { | |
| 28 | + method: "POST", | |
| 29 | + body: form, | |
| 30 | + }); | |
| 31 | + expect(file.content_type).toBe("image/png"); | |
| 32 | + expectPage(await json(`/notes/${noteId}/files?limit=10`)); | |
| 33 | + expect((await api(`/notes/${noteId}/files/${file.id}`, { method: "DELETE" })).status).toBe(204); | |
| 34 | + }); | |
| 35 | + | |
| 36 | + it("preserves event audience and paginated listing", async () => { | |
| 37 | + const event = await json<{ id: string; audience: { kind: string } }>("/events", { | |
| 38 | + method: "POST", | |
| 39 | + body: { title: `contract-event-${Date.now()}`, audience: { kind: "school" } }, | |
| 40 | + }); | |
| 41 | + eventId = event.id; | |
| 42 | + expect(event.audience.kind).toBe("school"); | |
| 43 | + expectPage(await json("/events?limit=1")); | |
| 44 | + }); | |
| 45 | +}); |
src/api/__tests__/contract/core.contract.test.tsadded+33 -0
| @@ -0,0 +1,33 @@ | ||
| 1 | +import { api, contractBaseUrl, expectPage, isLive, json, loginAdmin, SKIP_MESSAGE } from "./helpers/live-client"; | |
| 2 | + | |
| 3 | +if (!isLive) console.warn(SKIP_MESSAGE); | |
| 4 | + | |
| 5 | +describe.skipIf(!isLive)(`core contract @ ${contractBaseUrl}`, () => { | |
| 6 | + beforeAll(loginAdmin); | |
| 7 | + | |
| 8 | + it("keeps the authenticated user DTO stable", async () => { | |
| 9 | + const me = await json<Record<string, unknown>>("/auth/me"); | |
| 10 | + expect(typeof me.id).toBe("string"); | |
| 11 | + expect(typeof me.username).toBe("string"); | |
| 12 | + expect(["student", "parent", "teacher", "manager", "admin"]).toContain(me.role); | |
| 13 | + }); | |
| 14 | + | |
| 15 | + it("exposes the limits document used by client validation", async () => { | |
| 16 | + const limits = await json<Record<string, Record<string, unknown>>>("/limits"); | |
| 17 | + expect(typeof limits.user?.max_username_len).toBe("number"); | |
| 18 | + expect(typeof limits.note?.max_files).toBe("number"); | |
| 19 | + expect(typeof limits.request?.max_page_limit).toBe("number"); | |
| 20 | + }); | |
| 21 | + | |
| 22 | + it("uses the documented page envelope on administrative list routes", async () => { | |
| 23 | + for (const path of ["/users?limit=1", "/appointments?limit=1", "/payments/plans?limit=1"]) { | |
| 24 | + expectPage(await json(path)); | |
| 25 | + } | |
| 26 | + }); | |
| 27 | + | |
| 28 | + it("rejects anonymous protected reads", async () => { | |
| 29 | + // This raw request deliberately has no cookie, independent of the suite's jar. | |
| 30 | + const response = await fetch(`${contractBaseUrl}/auth/me`, { signal: AbortSignal.timeout(10_000) }); | |
| 31 | + expect(response.status).toBe(401); | |
| 32 | + }); | |
| 33 | +}); |
src/api/__tests__/contract/helpers/live-client.tsmodified+48 -9
| @@ -3,6 +3,8 @@ | ||
| 3 | 3 | * none of its own in bun/node) plus a hard timeout so a dead port fails fast |
| 4 | 4 | * instead of hanging the run. Boot recipe: see vitest.contract.config.ts. |
| 5 | 5 | */ |
| 6 | +import { expect } from "vitest"; | |
| 7 | + | |
| 6 | 8 | // The app's tsconfig has no node types (browser build), so reach env this way. |
| 7 | 9 | const env: Record<string, string | undefined> = |
| 8 | 10 | (globalThis as { process?: { env: Record<string, string | undefined> } }).process?.env ?? {}; |
| @@ -16,10 +18,17 @@ export const SKIP_MESSAGE = | ||
| 16 | 18 | "CONTRACT_BASE_URL=http://127.0.0.1:8081 bun run test:contract"; |
| 17 | 19 | |
| 18 | 20 | const TIMEOUT_MS = 10_000; |
| 21 | +const MAX_RATE_LIMIT_RETRIES = 12; | |
| 19 | 22 | let cookie = ""; |
| 20 | 23 | |
| 21 | 24 | type Init = { method?: string; body?: unknown; headers?: Record<string, string> }; |
| 22 | 25 | |
| 26 | +function retryDelayMs(response: Response, attempt: number): number { | |
| 27 | + const retryAfter = Number(response.headers.get("Retry-After")); | |
| 28 | + if (Number.isFinite(retryAfter) && retryAfter > 0) return retryAfter * 1_000; | |
| 29 | + return Math.min(1_000 * 2 ** attempt, 5_000); | |
| 30 | +} | |
| 31 | + | |
| 23 | 32 | /** Raw request — returns the Response so tests can assert on status codes. */ |
| 24 | 33 | export async function api(path: string, init: Init = {}): Promise<Response> { |
| 25 | 34 | const headers: Record<string, string> = { ...init.headers }; |
| @@ -33,20 +42,27 @@ export async function api(path: string, init: Init = {}): Promise<Response> { | ||
| 33 | 42 | body = JSON.stringify(init.body); |
| 34 | 43 | } |
| 35 | 44 | |
| 36 | - const res = await fetch(`${contractBaseUrl}${path}`, { | |
| 37 | - method: init.method ?? "GET", | |
| 38 | - headers, | |
| 39 | - body, | |
| 40 | - redirect: "manual", | |
| 41 | - signal: AbortSignal.timeout(TIMEOUT_MS), | |
| 42 | - }); | |
| 45 | + let res: Response | undefined; | |
| 46 | + for (let attempt = 0; attempt <= MAX_RATE_LIMIT_RETRIES; attempt += 1) { | |
| 47 | + res = await fetch(`${contractBaseUrl}${path}`, { | |
| 48 | + method: init.method ?? "GET", | |
| 49 | + headers, | |
| 50 | + body, | |
| 51 | + redirect: "manual", | |
| 52 | + signal: AbortSignal.timeout(TIMEOUT_MS), | |
| 53 | + }); | |
| 54 | + // A 429 has not executed the request, so retrying it is safe even for | |
| 55 | + // contract-suite POST/PATCH probes. Production calls remain unchanged. | |
| 56 | + if (res.status !== 429 || attempt === MAX_RATE_LIMIT_RETRIES) break; | |
| 57 | + await new Promise<void>((resolve) => setTimeout(resolve, retryDelayMs(res!, attempt))); | |
| 58 | + } | |
| 43 | 59 | |
| 44 | - const setCookies = res.headers.getSetCookie?.() ?? []; | |
| 60 | + const setCookies = res!.headers.getSetCookie?.() ?? []; | |
| 45 | 61 | for (const raw of setCookies) { |
| 46 | 62 | const pair = raw.split(";")[0]; |
| 47 | 63 | if (pair.startsWith("session=")) cookie = pair; |
| 48 | 64 | } |
| 49 | - return res; | |
| 65 | + return res!; | |
| 50 | 66 | } |
| 51 | 67 | |
| 52 | 68 | /** Request + JSON body, throwing on a non-2xx so setup failures are loud. */ |
| @@ -70,6 +86,14 @@ export async function loginAdmin(): Promise<void> { | ||
| 70 | 86 | }); |
| 71 | 87 | } |
| 72 | 88 | |
| 89 | +/** Exam kinds are school policy, not a globally fixed enum. */ | |
| 90 | +export async function configuredExamKind(): Promise<string> { | |
| 91 | + const settings = await json<{ exam_kinds?: Array<{ name?: unknown }> }>("/settings"); | |
| 92 | + const kind = settings.exam_kinds?.find((entry) => typeof entry.name === "string" && entry.name.trim())?.name; | |
| 93 | + if (!kind || typeof kind !== "string") throw new Error("GET /settings returned no configured exam kind"); | |
| 94 | + return kind; | |
| 95 | +} | |
| 96 | + | |
| 73 | 97 | /** Smallest valid 1x1 PNG, for the image-upload steps. */ |
| 74 | 98 | export function pngFile(name: string): File { |
| 75 | 99 | const bytes = Uint8Array.from( |
| @@ -80,3 +104,18 @@ export function pngFile(name: string): File { | ||
| 80 | 104 | ); |
| 81 | 105 | return new File([bytes], name, { type: "image/png" }); |
| 82 | 106 | } |
| 107 | + | |
| 108 | +/** Assert the list envelope shared by all paginated REST routes. */ | |
| 109 | +export function expectPage(data: unknown): asserts data is { | |
| 110 | + items: unknown[]; | |
| 111 | + total: number; | |
| 112 | + limit: number | null; | |
| 113 | + offset: number; | |
| 114 | +} { | |
| 115 | + expect(data).toMatchObject({}); | |
| 116 | + expect(Array.isArray((data as { items?: unknown }).items)).toBe(true); | |
| 117 | + expect(typeof (data as { total?: unknown }).total).toBe("number"); | |
| 118 | + const limit = (data as { limit?: unknown }).limit; | |
| 119 | + expect(limit === null || typeof limit === "number").toBe(true); | |
| 120 | + expect(typeof (data as { offset?: unknown }).offset).toBe("number"); | |
| 121 | +} |
src/api/__tests__/contract/learning.contract.test.tsadded+53 -0
| @@ -0,0 +1,53 @@ | ||
| 1 | +import { api, configuredExamKind, contractBaseUrl, expectPage, isLive, json, loginAdmin, SKIP_MESSAGE } from "./helpers/live-client"; | |
| 2 | + | |
| 3 | +if (!isLive) console.warn(SKIP_MESSAGE); | |
| 4 | + | |
| 5 | +describe.skipIf(!isLive)(`learning contract @ ${contractBaseUrl}`, () => { | |
| 6 | + let courseId = ""; | |
| 7 | + let examId = ""; | |
| 8 | + | |
| 9 | + beforeAll(async () => { | |
| 10 | + await loginAdmin(); | |
| 11 | + const tag = `contract-learning-${Date.now()}`; | |
| 12 | + const course = await json<{ id: string; title: string; kind: string }>("/courses", { | |
| 13 | + method: "POST", | |
| 14 | + body: { title: tag, kind: "course" }, | |
| 15 | + }); | |
| 16 | + courseId = course.id; | |
| 17 | + expect(course).toMatchObject({ title: tag, kind: "course" }); | |
| 18 | + }); | |
| 19 | + | |
| 20 | + afterAll(async () => { | |
| 21 | + if (courseId) await api(`/courses/${courseId}`, { method: "DELETE" }); | |
| 22 | + }); | |
| 23 | + | |
| 24 | + it("returns filtered courses through the standard page envelope", async () => { | |
| 25 | + const page = await json<{ items: Array<{ id: string }> }>(`/courses?kind=course&q=contract-learning&limit=10`); | |
| 26 | + expectPage(page); | |
| 27 | + expect(page.items.some((course) => course.id === courseId)).toBe(true); | |
| 28 | + }); | |
| 29 | + | |
| 30 | + it("supports course subjects and exam-question lists", async () => { | |
| 31 | + const subject = await json<{ id: string; course: string }>(`/courses/${courseId}/subjects`, { | |
| 32 | + method: "POST", | |
| 33 | + body: { name: "contract subject" }, | |
| 34 | + }); | |
| 35 | + expect(subject.course).toBe(courseId); | |
| 36 | + | |
| 37 | + const examKind = await configuredExamKind(); | |
| 38 | + const exam = await json<{ id: string; course: string; draft: boolean }>(`/courses/${courseId}/exams`, { | |
| 39 | + method: "POST", | |
| 40 | + body: { title: "contract exam", kind: examKind, draft: true }, | |
| 41 | + }); | |
| 42 | + examId = exam.id; | |
| 43 | + expect(exam.course).toBe(courseId); | |
| 44 | + | |
| 45 | + const question = await json<{ id: string; exam: string }>(`/exams/${examId}/questions`, { | |
| 46 | + method: "POST", | |
| 47 | + body: { subject_id: subject.id, text: "Contract question", kind: "text", points: 1 }, | |
| 48 | + }); | |
| 49 | + expect(question.exam).toBe(examId); | |
| 50 | + expectPage(await json(`/exams/${examId}/questions?limit=10`)); | |
| 51 | + expectPage(await json(`/courses/${courseId}/exams?limit=10`)); | |
| 52 | + }); | |
| 53 | +}); |
vitest.contract.config.tsmodified+3 -0
| @@ -25,6 +25,9 @@ export default defineConfig({ | ||
| 25 | 25 | globals: true, |
| 26 | 26 | environment: "node", |
| 27 | 27 | include: ["src/api/__tests__/contract/**/*.contract.test.ts"], |
| 28 | + // The scratch backend rate-limits all requests from its loopback client. | |
| 29 | + // Running files serially keeps a contract run deterministic and realistic. | |
| 30 | + fileParallelism: false, | |
| 28 | 31 | testTimeout: 30_000, |
| 29 | 32 | hookTimeout: 30_000, |
| 30 | 33 | }, |