From a38d7571eab6bbbd6ab72a9222ac8103f6d40178 Mon Sep 17 00:00:00 2001 From: shira Date: Fri, 9 Oct 2026 17:14:58 +0300 Subject: [PATCH] test(e2e): register assessment response waits right before their actions --- frontend/e2e/assessment.spec.ts | 49 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/frontend/e2e/assessment.spec.ts b/frontend/e2e/assessment.spec.ts index e22efa0..7fcf3ac 100644 --- a/frontend/e2e/assessment.spec.ts +++ b/frontend/e2e/assessment.spec.ts @@ -197,13 +197,15 @@ async function answerRequiredNumericItems( await focusWithKeyboard(page, answerField); await page.keyboard.type("1"); + const submitButton = page.getByRole("button", { name: "Отправить ответ" }); + await focusWithKeyboard(page, submitButton); + // A response wait expires after the action timeout counted from its + // registration, so it is registered only right before the key press. const answerResponsePromise = page.waitForResponse( (response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/v1/me/assessment-attempts/${attemptId}/answers`, ); - const submitButton = page.getByRole("button", { name: "Отправить ответ" }); - await focusWithKeyboard(page, submitButton); await page.keyboard.press("Enter"); expect((await answerResponsePromise).status()).toBe(200); submitted += 1; @@ -308,6 +310,8 @@ test("candidate can review, resume, and finish separate assessment modes", async await expect(page.getByRole("button", { name: "Завершить проверку" })).toBeVisible(); await expect(page.getByRole("heading", { name: "Результат с сервера" })).toHaveCount(0); + const finalizeRootButton = page.getByRole("button", { name: "Завершить проверку" }); + await focusWithKeyboard(page, finalizeRootButton); const finalizeRealResponsePromise = page.waitForResponse( (response) => response.request().method() === "POST" && @@ -321,8 +325,6 @@ test("candidate can review, resume, and finish separate assessment modes", async new URL(response.url()).pathname === `/api/v1/me/assessment-attempts/${realAttempt.id}/result`, ); - const finalizeRootButton = page.getByRole("button", { name: "Завершить проверку" }); - await focusWithKeyboard(page, finalizeRootButton); await page.keyboard.press("Enter"); expect((await finalizeRealResponsePromise).status()).toBe(200); const resultResponse = await resultResponsePromise; @@ -487,28 +489,28 @@ test("candidate can review, resume, and finish separate assessment modes", async const answerField = page.getByRole("textbox", { name: /^Числовой ответ/ }); await focusWithKeyboard(page, answerField); await page.keyboard.type("1"); + const submitButton = page.getByRole("button", { name: "Отправить ответ" }); + await focusWithKeyboard(page, submitButton); const answerResponsePromise = page.waitForResponse( (response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/v1/me/assessment-attempts/${expressAttempt.id}/answers`, ); - const submitButton = page.getByRole("button", { name: "Отправить ответ" }); - await focusWithKeyboard(page, submitButton); await page.keyboard.press("Enter"); expect((await answerResponsePromise).status()).toBe(200); if (sequence < 3) await expect(page.getByRole("heading", { name: `Задание ${sequence + 1}` })).toBeVisible(); } + const finalizeButton = page.getByRole("button", { name: "Завершить проверку" }); + await focusWithKeyboard(page, finalizeButton); const finalizeResponsePromise = page.waitForResponse( (response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/v1/me/assessment-attempts/${expressAttempt.id}/finalize`, ); - const finalizeButton = page.getByRole("button", { name: "Завершить проверку" }); - await focusWithKeyboard(page, finalizeButton); await page.keyboard.press("Enter"); expect((await finalizeResponsePromise).status()).toBe(200); await expect( @@ -871,11 +873,14 @@ function isFinalizeResponse(response: Response, attemptId: string): boolean { ); } -/** Creates an attempt from the assessment page, resumes it, and answers every item. */ +/** + * Creates an attempt from the assessment page, resumes it, answers every item, + * and finalizes it. Returns the attempt ID and the stored result the page read. + */ async function runAttemptToResult( page: Page, start: { name: string; confirmNotice: boolean }, -): Promise { +): Promise<{ id: string; result: Response }> { const createResponse = page.waitForResponse( (response) => response.request().method() === "POST" && @@ -895,13 +900,23 @@ async function runAttemptToResult( const resumed = (await (await resumeResponse).json()) as AttemptSnapshot; await answerRequiredNumericItems(page, attempt.id, resumed.progress.max_items); + // The result is read only after the finalize command succeeds, so both waits + // start right before that click. A wait registered before the whole run + // would expire after the action timeout while items are still answered. const finalizeResponse = page.waitForResponse((response) => isFinalizeResponse(response, attempt.id), ); + const resultResponse = page.waitForResponse( + (response) => + response.request().method() === "GET" && + response.status() === 200 && + new URL(response.url()).pathname === `/api/v1/me/assessment-attempts/${attempt.id}/result`, + ); await page.getByRole("button", { name: "Завершить проверку" }).click(); expect((await finalizeResponse).status()).toBe(200); + const result = await resultResponse; await expect(page.getByRole("heading", { name: resultHeading })).toBeVisible(); - return attempt.id; + return { id: attempt.id, result }; } async function signOut(page: Page): Promise { @@ -955,17 +970,11 @@ test("results keep the express run apart and stay private to the signed-in candi await page.getByLabel("Язык задания").selectOption("python"); // The full result: method, date, category state, and the provisional status. - const realResult = page.waitForResponse( - (response) => - response.request().method() === "GET" && - response.status() === 200 && - /^\/api\/v1\/me\/assessment-attempts\/[^/]+\/result$/.test(new URL(response.url()).pathname), - ); - const realId = await runAttemptToResult(page, { + const { id: realId, result: realResult } = await runAttemptToResult(page, { name: "Начать полную проверку", confirmNotice: true, }); - const stored = (await (await realResult).json()) as { mode: "demo_basic" | "adaptive" }; + const stored = (await realResult.json()) as { mode: "demo_basic" | "adaptive" }; // Only a short phrase is a live message; the record is ordinary content and // the focus moved to its heading. await expect(page.getByRole("heading", { name: resultHeading })).toBeFocused(); @@ -990,7 +999,7 @@ test("results keep the express run apart and stay private to the signed-in candi // The express run is a demonstration: no category, and it offers the full test. await page.goto("/candidate/assessment"); await expect(page.getByRole("button", { name: "Начать демонстрацию" })).toBeVisible(); - const expressId = await runAttemptToResult(page, { + const { id: expressId } = await runAttemptToResult(page, { name: "Начать демонстрацию", confirmNotice: false, });