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

162 lines
7.5 KiB
TypeScript

import { expect, test } from "@playwright/test";
import {
attemptId,
isFullscreen,
mockAttemptApi,
noticeVersion,
numberContent,
setTabHidden,
startAttempt,
taskPrompt,
} from "./assessment-attempt-mock";
// Fullscreen and the cover of the task during a real attempt. The API is
// mocked: these specs check what the page does, not the server.
const coverHeading = "Задание скрыто";
const continueName = "Продолжить в полном экране";
const answerName = /^Числовой ответ/;
test.use({ actionTimeout: 10_000, trace: "off", screenshot: "off", video: "off" });
test("leaving fullscreen hides the task and sends a signal, and returning restores it with the draft", async ({
page,
}) => {
const recorded = await mockAttemptApi(page, { content: numberContent });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
// The click that resumed the work asked for fullscreen.
await expect.poll(() => isFullscreen(page)).toBe(true);
await page.getByRole("textbox", { name: answerName }).fill("73");
await page.evaluate(() => document.exitFullscreen());
await expect(page.getByRole("heading", { name: coverHeading })).toBeVisible();
// The task is replaced, not blurred: neither its text nor the answer controls are in the page.
expect(await page.locator("body").innerText()).not.toContain(taskPrompt);
expect(await page.content()).not.toContain(taskPrompt);
await expect(page.getByRole("textbox", { name: answerName })).toHaveCount(0);
await expect(page.getByRole("heading", { name: "Задание 1" })).toHaveCount(0);
// The timer is the server's and stays on screen.
await expect(page.getByRole("timer")).toBeVisible();
// The keyboard focus waits on the way back.
await expect(page.getByRole("button", { name: continueName })).toBeFocused();
// The collector sends the exit with the next batch (every 10 seconds).
await expect
.poll(() => recorded.signals.some((event) => event.kind === "fullscreen_exit"), {
timeout: 15_000,
})
.toBe(true);
await page.getByRole("button", { name: continueName }).click();
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect(page.getByRole("heading", { name: coverHeading })).toHaveCount(0);
await expect.poll(() => isFullscreen(page)).toBe(true);
await expect(page.getByText(taskPrompt)).toBeVisible();
await expect(page.getByRole("textbox", { name: answerName })).toHaveValue("73");
});
test("leaving the tab covers the task and returning in fullscreen shows it again", async ({
page,
}) => {
const recorded = await mockAttemptApi(page, { content: numberContent });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect.poll(() => isFullscreen(page)).toBe(true);
await page.getByRole("textbox", { name: answerName }).fill("5");
await setTabHidden(page, true);
await expect(page.getByRole("heading", { name: coverHeading })).toBeVisible();
expect(await page.locator("body").innerText()).not.toContain(taskPrompt);
// Only the tab was left, so there is nothing to click: returning is enough.
await expect(page.getByRole("button", { name: continueName })).toHaveCount(0);
await page.waitForTimeout(300);
await setTabHidden(page, false);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect(page.getByText(taskPrompt)).toBeVisible();
await expect(page.getByRole("textbox", { name: answerName })).toHaveValue("5");
await expect
.poll(() => recorded.signals.some((event) => event.kind === "focus_loss"), { timeout: 15_000 })
.toBe(true);
});
test("a browser without fullscreen explains it and still takes the test with the cover on leave", async ({
page,
}) => {
// iPhone Safari has no element fullscreen.
await page.addInitScript(() => {
Object.defineProperty(document, "fullscreenEnabled", { configurable: true, get: () => false });
Object.defineProperty(Element.prototype, "requestFullscreen", {
configurable: true,
value: undefined,
});
});
const recorded = await mockAttemptApi(page, { content: numberContent });
await page.goto(`/candidate/assessment/${attemptId}`);
// The explanation is on screen before the work starts, and the start is not blocked.
await expect(page.getByText(/не поддерживает полноэкранный режим/)).toBeVisible();
await expect(page.getByText(/Тест можно пройти и без него/)).toBeVisible();
await page.getByRole("button", { name: "Начать или продолжить" }).click();
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
expect(await isFullscreen(page)).toBe(false);
await setTabHidden(page, true);
await expect(page.getByRole("heading", { name: coverHeading })).toBeVisible();
expect(await page.locator("body").innerText()).not.toContain(taskPrompt);
await setTabHidden(page, false);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
// The test can be finished from the keyboard in this mode too.
await page.getByRole("textbox", { name: answerName }).focus();
await page.keyboard.type("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);
});
test("a browser that refuses the fullscreen request continues without it", async ({ page }) => {
await page.addInitScript(() => {
Element.prototype.requestFullscreen = () =>
Promise.reject(new TypeError("Permissions check failed"));
});
await mockAttemptApi(page, { content: numberContent });
await startAttempt(page);
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect(page.getByText(/не включил полноэкранный режим/)).toBeVisible();
expect(await isFullscreen(page)).toBe(false);
// No cover is waiting for a fullscreen that cannot come.
await expect(page.getByRole("heading", { name: coverHeading })).toHaveCount(0);
});
test("closing the notice without confirming leaves fullscreen and starts nothing", async ({
page,
}) => {
const recorded = await mockAttemptApi(page, {
content: numberContent,
noticeAcknowledged: false,
});
await startAttempt(page);
const dialog = page.getByRole("dialog", { name: /какие сигналы собираются/ });
await expect(dialog).toBeVisible();
await expect(dialog.getByText("Полноэкранный режим", { exact: false }).first()).toBeVisible();
await expect.poll(() => isFullscreen(page)).toBe(true);
await page.getByRole("button", { name: "Закрыть без подтверждения" }).click();
await expect(dialog).toBeHidden();
await expect.poll(() => isFullscreen(page)).toBe(false);
await expect(page.getByRole("heading", { name: "Задание 1" })).toHaveCount(0);
expect(recorded.acknowledgements).toHaveLength(0);
// Confirming goes on to the test in fullscreen.
await page.getByRole("button", { name: "Начать или продолжить" }).click();
await page.getByRole("button", { name: "Подтверждаю ознакомление и начинаю" }).click();
await expect(page.getByRole("heading", { name: "Задание 1" })).toBeVisible();
await expect.poll(() => isFullscreen(page)).toBe(true);
expect(recorded.acknowledgements).toEqual([{ notice_version: noticeVersion }]);
});