import { expect, test } from "@playwright/test"; import { mockAttemptApi, numberContent, startAttempt, watermarkCode, } from "./assessment-attempt-mock"; // The watermark of a real attempt: a faint visible tile with the attempt code and // the time, and an invisible mark in the task text that assistive technology // never meets. The API is mocked: these specs check what the page does with the // mark the server sends, not how the server makes it. const mark = "\u200b\u200c\u200d\u2060\u200b\u200c"; const markedPrompt = `SECRET-PROMPT: which HTTP status ${mark}code means that a resource ${mark}was created?`; const readablePrompt = "SECRET-PROMPT: which HTTP status code means that a resource was created?"; test.use({ actionTimeout: 10_000, trace: "off", screenshot: "off", video: "off" }); test("the task screen shows a faint tile with the attempt code and the time, and nothing personal", async ({ page, }) => { await mockAttemptApi(page, { content: numberContent }); await startAttempt(page); await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible(); const tile = page.getByTestId("watermark-tile"); await expect(tile).toHaveAttribute("data-code", watermarkCode); await expect(tile).toHaveAttribute("aria-hidden", "true"); const text = (await tile.textContent()) ?? ""; expect(text).toMatch(new RegExp(`${watermarkCode} \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}Z`)); // The tile carries no name or contact of the candidate: the mocked account has an e-mail. expect(text).not.toContain("candidate@example.test"); expect(text).not.toContain("@"); // It is faint, it covers the screen, and it takes no click. const style = await tile.evaluate((element) => { const mark = element.firstElementChild as HTMLElement; return { opacity: Number(getComputedStyle(mark).opacity), pointerEvents: getComputedStyle(element).pointerEvents, position: getComputedStyle(element).position, }; }); expect(style.opacity).toBeGreaterThan(0.02); expect(style.opacity).toBeLessThan(0.15); expect(style.pointerEvents).toBe("none"); expect(style.position).toBe("fixed"); await page.getByRole("textbox", { name: /^Числовой ответ/ }).fill("201"); await expect(page.getByRole("textbox", { name: /^Числовой ответ/ })).toHaveValue("201"); }); test("an attempt without a watermark shows no tile", async ({ page }) => { await mockAttemptApi(page, { content: numberContent, watermark: null }); await startAttempt(page); await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible(); await expect(page.getByTestId("watermark-tile")).toHaveCount(0); }); test("the invisible mark stays in the page but out of the accessibility tree", async ({ page }) => { await mockAttemptApi(page, { content: numberContent, prompt: markedPrompt }); await startAttempt(page); await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible(); const prompt = page.locator("article p").first(); // The page keeps the mark, so that anything taken out of the page carries it. expect(await prompt.evaluate((element) => element.textContent)).toBe(markedPrompt); // Every run of invisible characters sits in an element hidden from assistive technology. const hidden = await prompt.evaluate((element) => { const runs = [...element.querySelectorAll("[data-watermark-mark]")]; return { count: runs.length, allHidden: runs.every((run) => run.getAttribute("aria-hidden") === "true"), }; }); expect(hidden).toEqual({ count: 2, allHidden: true }); // The accessibility tree holds the words and no format character. const snapshot = await prompt.ariaSnapshot(); expect(snapshot).toContain(readablePrompt); expect(snapshot).not.toMatch(/[\u200b-\u200d\u2060]/u); });