Files
lincodex/frontend/e2e/assessment-capture.spec.ts
T

163 lines
7.0 KiB
TypeScript

import { expect, test } from "@playwright/test";
import {
attemptId,
mockAttemptApi,
numberContent,
startAttempt,
type LeaseState,
} from "./assessment-attempt-mock";
// Weak capture hints and the one-open-window rule of a real attempt. The API is
// mocked: these specs check what the page sends and shows, not what the server
// decides. A screenshot or a phone photo cannot be prevented, so the page never
// tries: it only reports that a screenshot key was pressed.
const answerName = /^Числовой ответ/;
const flushTimeout = 15_000;
test.use({ actionTimeout: 10_000, trace: "off", screenshot: "off", video: "off" });
test("PrintScreen and the macOS shortcuts are recorded as a signal and the test goes on", async ({
page,
}) => {
const recorded = await mockAttemptApi(page, { content: numberContent });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
// The page only watches: the key event is not cancelled, so the system still takes the screenshot.
const notCancelled = await page.evaluate(() =>
document.dispatchEvent(
new KeyboardEvent("keydown", { key: "PrintScreen", code: "PrintScreen", cancelable: true }),
),
);
expect(notCancelled).toBe(true);
await page.waitForTimeout(1_100);
await page.keyboard.press("Meta+Shift+4");
await expect
.poll(() => recorded.signals.filter((event) => event.kind === "screenshot_key").length, {
timeout: flushTimeout,
})
.toBe(2);
const event = recorded.signals.find((signal) => signal.kind === "screenshot_key");
// The event holds the fact and the item, never the key, the screen, or any image.
expect(Object.keys(event ?? {}).sort()).toEqual(["item_instance_id", "kind", "occurred_at"]);
// The task goes on: nothing was covered, and the answer is typed and sent.
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await page.getByRole("textbox", { name: answerName }).fill("42");
await page.getByRole("button", { name: "Отправить ответ" }).click();
await expect.poll(() => recorded.answers.length).toBe(1);
});
test("open developer tools, guessed from the window size, are recorded as a weak signal", async ({
page,
}) => {
const recorded = await mockAttemptApi(page, { content: numberContent });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
expect(recorded.signals.some((signal) => signal.kind === "devtools_open")).toBe(false);
// A docked panel makes the outer window much taller than the page.
await page.evaluate(() => {
Object.defineProperty(window, "outerHeight", {
configurable: true,
get: () => window.innerHeight + 420,
});
window.dispatchEvent(new Event("resize"));
});
await expect
.poll(() => recorded.signals.filter((signal) => signal.kind === "devtools_open").length, {
timeout: flushTimeout,
})
.toBe(1);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
});
test("a second tab of the attempt is refused with an explanation until the first one is closed", async ({
browser,
}) => {
const context = await browser.newContext({ locale: "ru-RU" });
const lease: LeaseState = { holder: null };
const first = await context.newPage();
const second = await context.newPage();
const firstApi = await mockAttemptApi(first, { content: numberContent, lease });
const secondApi = await mockAttemptApi(second, { content: numberContent, lease });
await startAttempt(first);
await expect(first.getByRole("heading", { name: "Задание 1" })).toBeVisible();
expect(firstApi.holds).toHaveLength(1);
const firstClient = firstApi.holds[0] ?? "";
expect(firstClient).toMatch(/^[A-Za-z0-9_-]{16,64}$/);
// The session start and the answer carry the page identifier as well.
expect(firstApi.starts).toEqual([firstClient]);
// The attempt in a second tab: refused, explained, and the task is not shown.
await startAttempt(second);
const explanation = second
.getByRole("alert")
.filter({ hasText: "Попытка открыта в другом окне" });
await expect(explanation).toBeVisible();
await expect(explanation).toContainText("Работать над попыткой можно только в одном окне");
await expect(second.getByRole("heading", { name: "Задание 1" })).toHaveCount(0);
expect(await second.content()).not.toContain("SECRET-PROMPT");
expect(secondApi.holds).toHaveLength(1);
expect(secondApi.holds[0]).not.toBe(firstClient);
// Nothing was started in the second tab, and the first tab is undisturbed.
expect(secondApi.starts).toEqual([]);
await expect(first.getByRole("heading", { name: "Задание 1" })).toBeVisible();
// The first tab goes to the list of checks: its page releases the session at once.
await first.getByRole("link", { name: "К доступным проверкам" }).click();
await expect.poll(() => firstApi.releases).toEqual([firstClient]);
expect(lease.holder).toBeNull();
// The second tab continues.
await second.getByRole("button", { name: "Начать или продолжить" }).click();
await expect(second.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect(explanation).toHaveCount(0);
expect(secondApi.holds).toHaveLength(2);
expect(lease.holder).toBe(secondApi.holds[1]);
await context.close();
});
test("a page that is going away releases the work session", async ({ page }) => {
const lease: LeaseState = { holder: null };
const recorded = await mockAttemptApi(page, { content: numberContent, lease });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
expect(lease.holder).not.toBeNull();
await page.evaluate(() => window.dispatchEvent(new Event("pagehide")));
await expect.poll(() => recorded.releases.length).toBe(1);
expect(lease.holder).toBeNull();
});
test("a tab that loses the work session to another window stops and says so", async ({
browser,
}) => {
const context = await browser.newContext({ locale: "ru-RU" });
const lease: LeaseState = { holder: null };
const page = await context.newPage();
const recorded = await mockAttemptApi(page, {
content: numberContent,
lease,
renewAfterSeconds: 5,
});
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
expect(recorded.holds).toHaveLength(1);
// Another window takes the session over (the mock server hands it away).
lease.holder = "another-window-AAAAAAAAAAAAAAAA";
await expect(page.getByRole("heading", { name: "Задание 1" })).toHaveCount(0, {
timeout: 12_000,
});
await expect(
page.getByRole("alert").filter({ hasText: "Попытка открыта в другом окне" }),
).toBeVisible();
// The attempt in the page is the same one; it only waits for the session to be free.
expect(page.url()).toContain(attemptId);
await context.close();
});