Repository Public
1import { readFileSync } from "node:fs";2import { expect, test, type BrowserContext, type CDPSession, type Page } from "@playwright/test";34/**5 * The owner's journey, in one browser with a virtual platform authenticator (the automated stand-in for6 * Touch ID / Face ID): first setup → sign-in → sheet from a prompt → edit on the canvas and through Claude →7 * save and reload → publish → anonymous reading → export → unpublish.8 */9test.describe.configure({ mode: "serial" });10let context: BrowserContext, page: Page, cdp: CDPSession, authenticatorId = "", slug = "", editUrl = "";11const shot = (p: Page, name: string) => p.screenshot({ path: `e2e/screenshots/${name}.png`, fullPage: false });12const noOverflow = async (p: Page) => expect(await p.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);1314test.beforeAll(async ({ browser }) => {15 context = await browser.newContext({ locale: "fr-CA", viewport: { width: 1440, height: 900 } });16 page = await context.newPage();17 cdp = await context.newCDPSession(page);18 await cdp.send("WebAuthn.enable");19 ({ authenticatorId } = await cdp.send("WebAuthn.addVirtualAuthenticator", { options: { protocol: "ctap2", transport: "internal", hasResidentKey: true, hasUserVerification: true, isUserVerified: true, automaticPresenceSimulation: true } }));20});21test.afterAll(async () => { await context.storageState({ path: "e2e/.auth.json" }); await context.close(); });2223test("the studio is closed to visitors", async ({ browser }) => {24 const anon = await browser.newContext();25 const p = await anon.newPage();26 await p.goto("/admin");27 await expect(p).toHaveURL(/\/admin\/login/);28 await p.goto("/admin/setup");29 await expect(p.locator('[role="alert"]:not(#__next-route-announcer__)')).toBeVisible(); // no token: setup explains that nothing is pending for this visitor30 await anon.close();31});3233test("first setup: a passkey is enrolled and recovery codes are shown once", async () => {34 const url = readFileSync(process.env.SETUP_URL_FILE!, "utf8").trim();35 await page.goto(new URL(url).pathname + new URL(url).hash);36 await expect(page.getByRole("heading", { name: "Enregistrer une clé d’accès" })).toBeVisible();37 expect(page.url()).not.toContain("#"); // the token left the address bar38 await shot(page, "01-setup");39 await page.getByRole("button", { name: "Créer la clé d’accès" }).click();40 await expect(page.getByTestId("recovery-codes").locator("li")).toHaveCount(10);41 await page.getByRole("button", { name: /Je les ai notés/ }).click();42 await expect(page).toHaveURL(/\/admin$/);43 await expect(page.getByRole("heading", { level: 1 })).toBeVisible();44 await shot(page, "02-dashboard");45 // The link is dead afterwards.46 await page.goto(new URL(url).pathname + new URL(url).hash);47 await expect(page).toHaveURL(/\/admin(#.*)?$/); // signed in: straight to the studio (the fragment is a spent token)48 await page.goto("/admin");49});5051test("sign-out, a refused verification, then sign-in with the passkey", async () => {52 await page.getByRole("button", { name: "Se déconnecter" }).first().click();53 await expect(page).toHaveURL(/\/admin\/login/);54 await page.goto("/admin/sheets/new");55 await expect(page).toHaveURL(/\/admin\/login/);56 // The device refuses (no fingerprint match): the server never gets a verified assertion.57 await cdp.send("WebAuthn.setUserVerified", { authenticatorId, isUserVerified: false });58 await page.getByRole("button", { name: /Se connecter avec une clé d’accès/ }).click();59 await expect(page.locator('[role="alert"]:not(#__next-route-announcer__)')).toBeVisible();60 await expect(page).toHaveURL(/\/admin\/login/);61 await cdp.send("WebAuthn.setUserVerified", { authenticatorId, isUserVerified: true });62 await shot(page, "03-login");63 await page.getByRole("button", { name: /Se connecter avec une clé d’accès/ }).click();64 await expect(page).toHaveURL(/\/admin$/);65});6667test("a sheet is created from a prompt: plan first, then the draft", async () => {68 await page.goto("/admin/sheets/new");69 await page.locator("#prompt").fill("Crée une fiche de test sur un flux en trois étapes, avec une équation.");70 await page.getByRole("button", { name: "Proposer un plan" }).click();71 await expect(page.getByRole("heading", { name: "Plan proposé" })).toBeVisible({ timeout: 30_000 });72 await expect(page.getByText(/Coût du plan/)).toBeVisible();73 await shot(page, "04-plan");74 await page.getByRole("button", { name: "Générer la fiche" }).click();75 await expect(page).toHaveURL(/\/admin\/sheets\/[0-9a-f-]{36}\/edit/, { timeout: 60_000 });76 editUrl = new URL(page.url()).pathname;77 await expect(page.locator(".sheet h1")).toContainText("Fiche générée");78 await expect(page.locator(".sheet svg[role=img]").first()).toBeVisible(); // the diagram is drawn79 await expect(page.locator(".sheet .katex").first()).toBeVisible(); // the equation is typeset80 await shot(page, "05-editor");81});8283test("editing on the canvas: inline text, add, undo, autosave, reload", async () => {84 const heading = page.locator('[data-block-id="h-diagram"]');85 await heading.click();86 await heading.dblclick();87 const box = heading.locator("textarea");88 await box.fill("Schéma du flux");89 await box.press("Enter");90 await expect(heading).toContainText("Schéma du flux");91 // Add a block after the selection, then undo it.92 const count = await page.locator(".sheet [data-block-id]").count();93 await page.locator("aside").first().getByRole("button", { name: /Paragraphe|Paragraph/ }).first().click();94 await expect(page.locator(".sheet [data-block-id]")).toHaveCount(count + 1);95 await page.getByRole("button", { name: "Annuler", exact: true }).click();96 await expect(page.locator(".sheet [data-block-id]")).toHaveCount(count);97 // Autosave: the status goes "unsaved" → "saving" → "saved" without any click.98 await expect(page.getByRole("status").filter({ hasText: "Modifications non enregistrées" }).first()).toBeVisible();99 await expect(page.getByRole("status").filter({ hasText: /^\s*Brouillon\s*Enregistré\s*$/ }).first()).toBeVisible({ timeout: 15_000 });100 await page.reload();101 await expect(page.locator('[data-block-id="h-diagram"]')).toContainText("Schéma du flux");102});103104test("the property form edits the selected block", async () => {105 await page.locator('[data-block-id="intro"]').click();106 const form = page.locator("aside").last();107 const area = form.locator("textarea").first();108 await expect(area).toBeVisible();109 await area.fill("Une introduction réécrite depuis le panneau de propriétés.");110 await area.blur();111 await expect(page.locator('[data-block-id="intro"]')).toContainText("réécrite depuis le panneau");112});113114test("the visual diagram editor opens on the diagram and writes back", async () => {115 await page.locator('[data-block-id="d-flow"]').click();116 await page.getByRole("button", { name: "Éditeur visuel" }).first().click();117 const dialog = page.getByRole("dialog", { name: "Éditeur visuel" });118 await expect(dialog).toBeVisible();119 await expect(dialog.locator("svg").first()).toBeVisible();120 await shot(page, "06-diagram-editor");121 await dialog.getByRole("button", { name: "Fermer" }).click();122 await expect(dialog).toBeHidden();123});124125test("a conversation with Claude changes the sheet as a revision that can be undone", async () => {126 await page.keyboard.press("Escape"); // nothing selected: the request applies to the whole sheet127 await page.getByRole("tab", { name: "Claude" }).click();128 await page.locator("#ai-instruction").fill("Renomme la section du schéma");129 await page.getByRole("button", { name: "Envoyer à Claude" }).click();130 await expect(page.locator('[data-block-id="h-diagram"]')).toContainText("Schéma révisé", { timeout: 30_000 });131 await expect(page.locator('[data-block-id="note-ai"]')).toBeVisible();132 await expect(page.getByText(/1 ajouté/)).toBeVisible();133 await shot(page, "07-claude");134 await page.getByRole("button", { name: "Annuler ce changement" }).click();135 await expect(page.locator('[data-block-id="note-ai"]')).toHaveCount(0);136 await expect(page.locator('[data-block-id="h-diagram"]')).toContainText("Schéma du flux");137});138139test("the quality report lists what a human still has to check", async () => {140 await page.getByRole("tab", { name: /Qualité/ }).click();141 await expect(page.getByText(/Source non vérifiée/).first()).toBeVisible();142 await expect(page.getByText(/n’est pas une validation scientifique/)).toBeVisible();143});144145test("publication is an explicit act, then anyone can read the sheet", async ({ browser }) => {146 await page.getByRole("button", { name: "Publier", exact: true }).first().click();147 const dialog = page.getByRole("dialog").filter({ hasText: "Publier la fiche" });148 await expect(dialog.getByRole("button", { name: "Publier maintenant" })).toBeDisabled(); // not before the review box is ticked149 await dialog.getByRole("checkbox").check();150 await dialog.getByRole("button", { name: "Publier maintenant" }).click();151 await expect(dialog.getByText("Fiche publiée.")).toBeVisible();152 slug = (await dialog.getByRole("link", { name: "Voir la page publique" }).getAttribute("href"))!.replace("/s/", "");153 await dialog.getByRole("button", { name: /Fermer/ }).click();154 for (const viewport of [{ width: 390, height: 844 }, { width: 1440, height: 900 }]) {155 const anon = await browser.newContext({ viewport, locale: "fr-CA" });156 const p = await anon.newPage();157 const res = await p.goto(`/s/${slug}`);158 expect(res!.status()).toBe(200);159 await expect(p.locator(".sheet h1")).toContainText("Fiche générée");160 await expect(p.getByText("Simon-Pierre Boucher").first()).toBeVisible();161 await noOverflow(p);162 await shot(p, `08-public-${viewport.width}`);163 await anon.close();164 }165});166167test("exports are produced by the worker and downloadable", async () => {168 await page.getByRole("button", { name: "Plus" }).click();169 await page.getByRole("dialog").getByRole("button", { name: "Exporter" }).click();170 const dialog = page.getByRole("dialog").filter({ hasText: "PDF vectoriel" });171 await dialog.getByRole("button", { name: "PDF vectoriel" }).click();172 const link = dialog.getByRole("link", { name: "Télécharger" });173 await expect(link).toBeVisible({ timeout: 60_000 });174 const file = await page.request.get((await link.getAttribute("href"))!);175 expect(file.status()).toBe(200);176 expect(file.headers()["content-type"]).toBe("application/pdf");177 expect((await file.body()).subarray(0, 5).toString()).toBe("%PDF-");178 await dialog.getByRole("button", { name: /Fermer/ }).click();179});180181test("unpublishing removes the sheet from the public site at once", async ({ browser }) => {182 await page.getByRole("button", { name: "Plus" }).click();183 await page.getByRole("dialog").getByRole("button", { name: "Dépublier" }).click();184 const dialog = page.getByRole("dialog").filter({ hasText: "ne sera plus accessible" });185 await dialog.getByRole("button", { name: "Dépublier" }).click();186 await expect(dialog.getByText("Fiche dépubliée.")).toBeVisible();187 const anon = await browser.newContext();188 const p = await anon.newPage();189 expect((await p.goto(`/s/${slug}`))!.status()).toBe(404);190 await expect(p.getByText("404")).toBeVisible();191 await anon.close();192 await dialog.getByRole("button", { name: /Fermer/ }).click();193 expect(editUrl).toMatch(/edit$/);194});195196test("the other studio pages load real data without errors", async () => {197 const errors: string[] = [];198 page.on("pageerror", (e) => errors.push(e.message));199 for (const [path, name] of [["/admin", "09-dashboard"], ["/admin/assets", "10-assets"], ["/admin/templates", "11-templates"], ["/admin/settings", "12-settings"], ["/admin/revisions", "13-revisions"]]) {200 await page.goto(path);201 await expect(page.getByRole("heading", { level: 1 })).toBeVisible();202 await page.waitForLoadState("networkidle");203 await expect(page.locator('[role="alert"]:not(#__next-route-announcer__)')).toHaveCount(0);204 await noOverflow(page);205 await shot(page, name);206 }207 expect(errors).toEqual([]);208});209