diff --git a/frontend/e2e/acceptance-path.spec.ts b/frontend/e2e/acceptance-path.spec.ts index 959208f..249578f 100644 --- a/frontend/e2e/acceptance-path.spec.ts +++ b/frontend/e2e/acceptance-path.spec.ts @@ -206,7 +206,7 @@ function apiResponse(method: string, path: string | RegExp) { // The answer of a rate-limit fixture item follows from its statement: count // the requests that find `limit` accepted ones in (t - window, t]. function rejectedRequests(statement: string): number { - const limit = Number(/fewer than (\d+) previously accepted/.exec(statement)?.[1]); + const limit = Number(/(?:fewer|less) than (\d+) previously accepted/.exec(statement)?.[1]); const window = Number(/\(t - (\d+), t\]/.exec(statement)?.[1]); const times = /arrival times: ([\d ]+)\./.exec(statement)?.[1]?.trim().split(/\s+/).map(Number); if (!Number.isInteger(limit) || !Number.isInteger(window) || !times?.length) { @@ -264,6 +264,19 @@ async function grantC3(page: Page): Promise { expect((await granted).status()).toBe(200); } +// The served statement of a real attempt carries an invisible mark (format +// characters) and may use an interchangeable wording ("less than" for "fewer +// than"), so the helper waits for the statement and drops the mark characters. +async function readStatement(page: Page): Promise { + const paragraph = page.locator("article p").first(); + const readable = async () => + ((await paragraph.textContent()) ?? "").replace(/\p{Cf}/gu, "").replace(/\s+/g, " "); + await expect + .poll(readable, { timeout: 15_000 }) + .toMatch(/^A gateway applies a sliding-window limiter/); + return readable(); +} + // Takes the full Backend Junior test in the browser and answers every item // from its statement. With `reloadAfter` the page is reloaded after that many // answers: the same attempt and item come back and nothing is answered twice. @@ -297,9 +310,7 @@ async function passFullTest(page: Page, reloadAfter?: number): Promise { await expect(page.getByRole("heading", { name: `Задание ${sequence}` })).toBeVisible({ timeout: 20_000, }); - const statement = await page - .getByText(/^A gateway applies a sliding-window limiter/) - .innerText(); + const statement = await readStatement(page); await page .getByRole("textbox", { name: /^Числовой ответ/ }) .fill(String(rejectedRequests(statement))); diff --git a/frontend/e2e/assessment.spec.ts b/frontend/e2e/assessment.spec.ts index 142bda7..57d1537 100644 --- a/frontend/e2e/assessment.spec.ts +++ b/frontend/e2e/assessment.spec.ts @@ -7,6 +7,7 @@ import { type Response, type Route, } from "@playwright/test"; +import { expectNoHorizontalOverflow } from "./viewport"; type InboxMessage = { text?: string; @@ -173,14 +174,6 @@ async function focusWithKeyboard(page: Page, target: Locator): Promise { throw new Error("Keyboard navigation did not reach the assessment action."); } -async function expectNoHorizontalOverflow(page: Page, width: number): Promise { - await page.setViewportSize({ width, height: 900 }); - const overflow = await page.evaluate( - () => document.documentElement.scrollWidth > window.innerWidth, - ); - expect(overflow, `no horizontal overflow at ${width}px`).toBe(false); -} - async function navigateInApp(page: Page, path: string): Promise { await page.evaluate((target) => { window.history.pushState(window.history.state, "", target); diff --git a/frontend/e2e/minimal-path-a11y.spec.ts b/frontend/e2e/minimal-path-a11y.spec.ts index c5c9939..984c352 100644 --- a/frontend/e2e/minimal-path-a11y.spec.ts +++ b/frontend/e2e/minimal-path-a11y.spec.ts @@ -1,4 +1,5 @@ import { expect, test, type Locator, type Page, type Request, type Route } from "@playwright/test"; +import { expectNoHorizontalOverflow } from "./viewport"; // States, focus, and error binding of the minimal path with a stubbed API. The // API answers come from this file, so each test controls one failure at a time @@ -161,14 +162,6 @@ async function mockApi(page: Page, routes: Routes): Promise> return calls; } -async function expectNoHorizontalOverflow(page: Page, width: number): Promise { - await page.setViewportSize({ width, height: 900 }); - const overflow = await page.evaluate( - () => document.documentElement.scrollWidth > window.innerWidth, - ); - expect(overflow, `no horizontal overflow at ${width}px`).toBe(false); -} - // The focus outline comes from the global :focus-visible rule. A control that // has focus from the keyboard must match that selector and draw an outline. async function expectFocusOutline(target: Locator): Promise { diff --git a/frontend/e2e/viewport.ts b/frontend/e2e/viewport.ts new file mode 100644 index 0000000..d2bc39c --- /dev/null +++ b/frontend/e2e/viewport.ts @@ -0,0 +1,40 @@ +import { expect, type Page } from "@playwright/test"; + +const continueInFullscreenName = "Продолжить в полном экране"; + +/** + * Resizes the viewport the way a user would while taking a real attempt. + * + * A real attempt holds the page in fullscreen, and the browser refuses to + * resize a fullscreen window. The helper leaves fullscreen first. The page then + * covers the task, as it does for a candidate who pressed Escape, so the helper + * returns through the cover button, which also asks for fullscreen again, and + * waits for the task to be back. Pages that never entered fullscreen are + * resized directly. + */ +export async function resizeViewport( + page: Page, + size: { width: number; height: number }, +): Promise { + const wasFullscreen = await page.evaluate(() => document.fullscreenElement !== null); + if (wasFullscreen) { + await page.evaluate(() => document.exitFullscreen()); + await expect.poll(() => page.evaluate(() => document.fullscreenElement)).toBeNull(); + } + await page.setViewportSize(size); + if (!wasFullscreen) return; + + const continueButton = page.getByRole("button", { name: continueInFullscreenName }); + await expect(continueButton).toBeVisible(); + await continueButton.click(); + await expect(continueButton).toHaveCount(0); + await expect.poll(() => page.evaluate(() => document.fullscreenElement !== null)).toBe(true); +} + +export async function expectNoHorizontalOverflow(page: Page, width: number): Promise { + await resizeViewport(page, { width, height: 900 }); + const overflow = await page.evaluate( + () => document.documentElement.scrollWidth > window.innerWidth, + ); + expect(overflow, `no horizontal overflow at ${width}px`).toBe(false); +}