Files
glenloch-media/e2e/training.spec.ts
T
bhursey cb5976edb2
CI / build (push) Has been cancelled
feat(training): rich-text lessons with inline images and 25 MB manuals
Admins were authoring plain-text lessons, which made illustrated tutorials
and reordering images impossible. Lesson bodies are now sanitized HTML from a
Tiptap editor with alignment, sizing, drag-reorder and alt text, and inline
images reuse the training_resources table so no migration is needed.
Attachments grow to 25 MB, PDFs stream inline, and SVG is rejected because it
can execute as a document. Legacy plain-text bodies are normalized on read.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-20 12:18:24 +00:00

135 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect } from "@playwright/test";
import { signInAs, resetDb, BASE } from "./dev";
/**
* Training authoring (F2): create a lesson, publish it, upload an attachment,
* and confirm a volunteer sees the content without authoring controls.
*/
let lessonTitle: string;
let lessonId: string;
test.describe.configure({ mode: "serial" });
test.describe("training", () => {
test.beforeAll(async ({ browser }) => {
const context = await browser.newContext();
const page = await context.newPage();
await resetDb(page);
await context.close();
});
test("admin authors a lesson and uploads an attachment", async ({ page }) => {
await page.setViewportSize({ width: 1024, height: 900 });
await signInAs(page, "admin@glenloch.local");
await page.goto(`${BASE}/training`);
// Seeded starter content is visible.
await expect(page.getByRole("heading", { name: "Audio team basics" })).toBeVisible();
lessonTitle = `E2E lesson ${Date.now()}`;
await page.getByLabel("Title").fill(lessonTitle);
await page.getByLabel("Summary").fill("Created from the admin UI");
await page.getByLabel("Content").fill("Step one\nStep two");
await page.getByRole("button", { name: "Create lesson" }).click();
await expect(
page.getByRole("heading", { name: lessonTitle }),
).toBeVisible();
const list = (await (
await page.request.get(`${BASE}/api/dev/training/lessons`)
).json()) as { lessons: { id: string; title: string }[] };
lessonId = list.lessons.find((lesson) => lesson.title === lessonTitle)!.id;
const upload = await page.request.post(
`${BASE}/api/dev/training/resources`,
{
multipart: {
lessonId,
file: {
name: "guide.txt",
mimeType: "text/plain",
buffer: Buffer.from("hello from the upload test"),
},
},
},
);
expect(upload.status(), "upload should be accepted").toBe(201);
await page.reload();
await expect(page.getByRole("link", { name: "guide.txt" })).toBeVisible();
});
test("admin adds a rich image and a PDF manual", async ({ page }) => {
await page.setViewportSize({ width: 1024, height: 900 });
await signInAs(page, "admin@glenloch.local");
await page.goto(`${BASE}/training`);
const lesson = page
.locator("li")
.filter({ has: page.getByRole("heading", { name: lessonTitle }) })
.first();
await lesson.getByRole("button", { name: "Edit" }).click();
// 1×1 PNG, but the upload route only needs a valid PNG signature here.
const png = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9Z7ZkAAAAASUVORK5CYII=",
"base64",
);
await page
.getByLabel("Upload an image for the lesson")
.setInputFiles({ name: "console.png", mimeType: "image/png", buffer: png });
const image = page.getByRole("img", { name: "console.png" });
await expect(image).toBeVisible();
await image.click();
await expect(
page.getByRole("toolbar", { name: "Selected image" }),
).toBeVisible();
await page.getByRole("button", { name: "Align image right" }).click();
await page
.getByRole("button", { name: "Set image width to 50%" })
.click();
await page.getByRole("button", { name: "Save lesson" }).click();
await expect(
lesson.locator('img[data-align="right"][width="50%"]'),
).toBeVisible();
const pdf = Buffer.from("%PDF-1.7\n% E2E manual\n");
await lesson
.getByLabel("Attach a manual or document")
.setInputFiles({ name: "audio-manual.pdf", mimeType: "application/pdf", buffer: pdf });
await lesson.getByRole("button", { name: "Attach", exact: true }).click();
const manual = lesson.getByRole("link", { name: "audio-manual.pdf" });
await expect(manual).toBeVisible();
const href = await manual.getAttribute("href");
expect(href).toBeTruthy();
const download = await page.request.get(`${BASE}${href}`);
expect(download.status()).toBe(200);
expect(download.headers()["content-disposition"]).toContain("inline");
});
test("volunteer sees published lessons but no authoring controls", async ({
page,
}) => {
await page.setViewportSize({ width: 1024, height: 900 });
await signInAs(page, "avery@glenloch.local");
await page.goto(`${BASE}/training`);
await expect(
page.getByRole("heading", { name: lessonTitle }),
).toBeVisible();
await expect(
page.getByRole("button", { name: "Create lesson" }),
).toHaveCount(0);
await expect(page.getByRole("link", { name: "guide.txt" })).toBeVisible();
await expect(page.getByRole("img", { name: "console.png" })).toBeVisible();
await expect(
page.getByRole("link", { name: "audio-manual.pdf" }),
).toBeVisible();
});
});