199 lines
7.9 KiB
TypeScript
199 lines
7.9 KiB
TypeScript
import { expect, test, type Page } from "@playwright/test";
|
|
import {
|
|
exampleInput,
|
|
mockAttemptApi,
|
|
numberContent,
|
|
practiceContent,
|
|
setTabHidden,
|
|
startAttempt,
|
|
taskPrompt,
|
|
} from "./assessment-attempt-mock";
|
|
|
|
// Copy, selection, context menu, drag, print, and paste rules during a real
|
|
// attempt. The API is mocked: these specs check what the page does.
|
|
|
|
const sentinel = "CLIPBOARD-SENTINEL";
|
|
const modifier = "ControlOrMeta";
|
|
const refusedNotice = "Вставка в поля ответа отключена";
|
|
|
|
test.use({ actionTimeout: 10_000, trace: "off", screenshot: "off", video: "off" });
|
|
|
|
async function allowClipboard(page: Page): Promise<void> {
|
|
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
|
|
}
|
|
|
|
async function writeClipboard(page: Page, text: string): Promise<void> {
|
|
await page.evaluate((value) => navigator.clipboard.writeText(value), text);
|
|
}
|
|
|
|
async function readClipboard(page: Page): Promise<string> {
|
|
return page.evaluate(() => navigator.clipboard.readText());
|
|
}
|
|
|
|
/** Sends the queued signals now: hiding the tab flushes the batch. */
|
|
async function flushSignals(page: Page): Promise<void> {
|
|
await setTabHidden(page, true);
|
|
await page.waitForTimeout(300);
|
|
await setTabHidden(page, false);
|
|
await page.waitForTimeout(500);
|
|
}
|
|
|
|
test("the task text cannot be selected, copied, dragged, or printed, and stays readable by assistive tools", async ({
|
|
page,
|
|
}) => {
|
|
await allowClipboard(page);
|
|
await mockAttemptApi(page, { content: numberContent });
|
|
await startAttempt(page);
|
|
const prompt = page.getByText(taskPrompt);
|
|
await expect(prompt).toBeVisible();
|
|
|
|
// Selection: the text is not selectable, so a triple click and select-all take none of it.
|
|
expect(await prompt.evaluate((element) => getComputedStyle(element).userSelect)).toBe("none");
|
|
await prompt.click({ clickCount: 3 });
|
|
expect(await page.evaluate(() => window.getSelection()?.toString())).toBe("");
|
|
await page.mouse.click(5, 5);
|
|
await page.keyboard.press(`${modifier}+a`);
|
|
expect(await page.evaluate(() => window.getSelection()?.toString())).not.toContain("SECRET");
|
|
|
|
// Copy: neither the keyboard nor a script that selects the text gets it into the clipboard.
|
|
await writeClipboard(page, sentinel);
|
|
await page.keyboard.press(`${modifier}+c`);
|
|
expect(await readClipboard(page)).not.toContain("SECRET-PROMPT");
|
|
const refused = await page.evaluate(() => {
|
|
const element = document.querySelector("article p");
|
|
if (!element) return null;
|
|
const range = document.createRange();
|
|
range.selectNodeContents(element);
|
|
const selection = window.getSelection();
|
|
selection?.removeAllRanges();
|
|
selection?.addRange(range);
|
|
let prevented: boolean | null = null;
|
|
const listener = (event: Event) => {
|
|
prevented = event.defaultPrevented;
|
|
};
|
|
// A bubbling listener on the window runs after the page's own handlers.
|
|
window.addEventListener("copy", listener);
|
|
document.execCommand("copy");
|
|
window.removeEventListener("copy", listener);
|
|
return prevented;
|
|
});
|
|
expect(refused).toBe(true);
|
|
expect(await readClipboard(page)).not.toContain("SECRET-PROMPT");
|
|
|
|
// Context menu and drag are cancelled on the task; a field keeps its own menu.
|
|
const prevented = await page.evaluate(() => {
|
|
const element = document.querySelector("article p");
|
|
const field = document.querySelector("input[name=number-answer]");
|
|
const cancelled = (target: Element | null, event: Event) =>
|
|
target !== null && !target.dispatchEvent(event);
|
|
return {
|
|
taskMenu: cancelled(
|
|
element,
|
|
new MouseEvent("contextmenu", { bubbles: true, cancelable: true }),
|
|
),
|
|
taskDrag: cancelled(element, new DragEvent("dragstart", { bubbles: true, cancelable: true })),
|
|
fieldMenu: cancelled(
|
|
field,
|
|
new MouseEvent("contextmenu", { bubbles: true, cancelable: true }),
|
|
),
|
|
};
|
|
});
|
|
expect(prevented).toEqual({ taskMenu: true, taskDrag: true, fieldMenu: false });
|
|
|
|
// The field's own text can still be copied.
|
|
await page.getByRole("textbox", { name: /^Числовой ответ/ }).fill("4242");
|
|
await page.keyboard.press(`${modifier}+a`);
|
|
await page.keyboard.press(`${modifier}+c`);
|
|
expect(await readClipboard(page)).toBe("4242");
|
|
|
|
// The text stays in the page for a screen reader.
|
|
expect(await page.getByRole("article").ariaSnapshot()).toContain("SECRET-PROMPT");
|
|
|
|
// Printing leaves the task out.
|
|
await page.emulateMedia({ media: "print" });
|
|
await expect(page.getByRole("article")).toBeHidden();
|
|
expect(await page.locator("body").innerText()).not.toContain("SECRET-PROMPT");
|
|
await page.emulateMedia({ media: "screen" });
|
|
await expect(prompt).toBeVisible();
|
|
});
|
|
|
|
test("an answer field refuses paste, says so, records the attempt as a signal, and still takes typing", async ({
|
|
page,
|
|
}) => {
|
|
await allowClipboard(page);
|
|
const recorded = await mockAttemptApi(page, { content: numberContent });
|
|
await startAttempt(page);
|
|
const field = page.getByRole("textbox", { name: /^Числовой ответ/ });
|
|
await expect(field).toBeVisible();
|
|
|
|
await writeClipboard(page, "PASTED123");
|
|
await field.click();
|
|
await page.keyboard.press(`${modifier}+v`);
|
|
await expect(field).toHaveValue("");
|
|
await expect(page.getByText(refusedNotice)).toBeVisible();
|
|
|
|
await page.keyboard.type("77");
|
|
await expect(field).toHaveValue("77");
|
|
await expect(page.getByText(refusedNotice)).toHaveCount(0);
|
|
|
|
await flushSignals(page);
|
|
const pastes = recorded.signals.filter((event) => event.kind === "paste");
|
|
expect(pastes.map((event) => event.size_bytes)).toEqual(["PASTED123".length]);
|
|
expect(JSON.stringify(recorded.signals)).not.toContain("PASTED123");
|
|
});
|
|
|
|
test("the practice code editor accepts paste and records its size, and protects its examples", async ({
|
|
page,
|
|
}) => {
|
|
await allowClipboard(page);
|
|
const recorded = await mockAttemptApi(page, { content: practiceContent });
|
|
await startAttempt(page);
|
|
const editor = page.getByRole("textbox", { name: "Код решения" });
|
|
await expect(editor).toBeVisible();
|
|
|
|
const code = "print('PASTED-CODE')";
|
|
await writeClipboard(page, code);
|
|
await editor.click();
|
|
await page.keyboard.press(`${modifier}+v`);
|
|
await expect(editor).toHaveValue(code);
|
|
await expect(page.getByText(refusedNotice)).toHaveCount(0);
|
|
await page.keyboard.type(" # note");
|
|
await expect(editor).toHaveValue(`${code} # note`);
|
|
|
|
// Dropped text is refused even here, because a drop is not a recorded paste.
|
|
const dropCancelled = await editor.evaluate(
|
|
(element) => !element.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true })),
|
|
);
|
|
expect(dropCancelled).toBe(true);
|
|
|
|
// The examples are task text: not selectable, not copyable.
|
|
const example = page.getByText(exampleInput);
|
|
expect(await example.evaluate((element) => getComputedStyle(element).userSelect)).toBe("none");
|
|
|
|
await flushSignals(page);
|
|
const pastes = recorded.signals.filter((event) => event.kind === "paste");
|
|
expect(pastes.map((event) => event.size_bytes)).toEqual([code.length]);
|
|
expect(JSON.stringify(recorded.signals)).not.toContain("PASTED-CODE");
|
|
});
|
|
|
|
test("an item is answered with the keyboard alone while the protection is on", async ({ page }) => {
|
|
const recorded = await mockAttemptApi(page, { content: numberContent });
|
|
await startAttempt(page);
|
|
const field = page.getByRole("textbox", { name: /^Числовой ответ/ });
|
|
await expect(field).toBeVisible();
|
|
|
|
for (let step = 0; step < 30; step += 1) {
|
|
if (await field.evaluate((element) => element === document.activeElement)) break;
|
|
await page.keyboard.press("Tab");
|
|
}
|
|
await expect(field).toBeFocused();
|
|
await page.keyboard.type("42");
|
|
await expect(field).toHaveValue("42");
|
|
await page.keyboard.press("Tab");
|
|
await expect(page.getByRole("button", { name: "Отправить ответ" })).toBeFocused();
|
|
await page.keyboard.press("Enter");
|
|
|
|
await expect.poll(() => recorded.answers.length).toBe(1);
|
|
expect(recorded.answers[0]).toMatchObject({ answer: { format: "number", number: 42 } });
|
|
});
|