|
| 1 | +import type { Page, Route } from "@playwright/test"; |
| 2 | +import { admin, AI_TIMEOUT, chooseForMotion, expect, startDebate, test } from "./fixtures"; |
| 3 | + |
| 4 | +// Feature map: features/ai-failures.md. Each failure is forced in the browser, |
| 5 | +// so the provider is never called and the recipe runs the same in both modes. |
| 6 | +const motion = "This House would make public transport free at the point of use"; |
| 7 | +const resolution = "Resolved: cities should ban private cars downtown."; |
| 8 | + |
| 9 | +/** Answers every `ai` request except a round's quota check as the server does for a refused session. */ |
| 10 | +async function refuseSession(page: Page): Promise<void> { |
| 11 | + await page.route("**/functions/v1/ai", async (route: Route) => { |
| 12 | + if (route.request().method() === "POST" && (route.request().postDataJSON() as { quotaCheck?: boolean } | null)?.quotaCheck) { |
| 13 | + await route.fallback(); |
| 14 | + return; |
| 15 | + } |
| 16 | + await route.fulfill({ status: 401, contentType: "application/json", body: JSON.stringify({ error: "Invalid JWT" }) }); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +async function expectBackOnLogin(page: Page): Promise<void> { |
| 21 | + await expect(page).toHaveURL(/\/login$/, { timeout: AI_TIMEOUT }); |
| 22 | + await expect(page.getByRole("status").filter({ hasText: "Your session expired. Sign in again." })).toBeVisible(); |
| 23 | +} |
| 24 | + |
| 25 | +test("fail-signin-tool: a refused session on Generate offers Sign in again, not Retry, and it opens the login page", async ({ signedIn: page, evidence }) => { |
| 26 | + await startDebate(page, motion); |
| 27 | + await page.goto("/app/generate"); |
| 28 | + await refuseSession(page); |
| 29 | + await page.getByRole("button", { name: "Generate debate case" }).click(); |
| 30 | + const failure = page.getByRole("alert").filter({ hasText: "Sign-in required" }); |
| 31 | + await expect(failure).toBeVisible({ timeout: AI_TIMEOUT }); |
| 32 | + await expect(failure.getByRole("button", { name: "Retry" })).toHaveCount(0); |
| 33 | + await evidence.shot("01-generate-sign-in-again"); |
| 34 | + await evidence.aria("01-generate-sign-in-again", failure); |
| 35 | + |
| 36 | + await failure.getByRole("button", { name: "Sign in again" }).click(); |
| 37 | + await expectBackOnLogin(page); |
| 38 | + await evidence.shot("02-login-after-sign-in-again"); |
| 39 | +}); |
| 40 | + |
| 41 | +test("fail-network-ask: a question that never reached the server stays with Retry, and Retry answers it once", async ({ signedIn: page, evidence }) => { |
| 42 | + const question = "What is the strongest objection to free public transport?"; |
| 43 | + await page.goto("/app/ask"); |
| 44 | + await page.route("**/functions/v1/ai-stream", (route) => route.abort("internetdisconnected")); |
| 45 | + await page.route("**/functions/v1/ai", (route) => route.abort("internetdisconnected")); |
| 46 | + await page.getByRole("textbox", { name: "Message to your debate coach" }).fill(question); |
| 47 | + await page.keyboard.press("Enter"); |
| 48 | + const failure = page.getByRole("alert").filter({ hasText: "Check your connection and try again." }); |
| 49 | + await expect(failure).toBeVisible({ timeout: AI_TIMEOUT }); |
| 50 | + await evidence.shot("01-ask-network-retry"); |
| 51 | + await evidence.aria("01-ask-network-retry", failure); |
| 52 | + |
| 53 | + await page.unroute("**/functions/v1/ai-stream"); |
| 54 | + await page.unroute("**/functions/v1/ai"); |
| 55 | + const answer = page.waitForResponse((response) => response.url().endsWith("/functions/v1/ai-stream"), { timeout: AI_TIMEOUT }); |
| 56 | + await failure.getByRole("button", { name: "Retry" }).click(); |
| 57 | + await answer; |
| 58 | + await expect(failure).toHaveCount(0); |
| 59 | + await expect(page.getByText(question)).toHaveCount(1); |
| 60 | + await evidence.shot("02-ask-answered-after-retry"); |
| 61 | +}); |
| 62 | + |
| 63 | +test("fail-signin-ask: a refused session in Ask offers Sign in again", async ({ signedIn: page, evidence }) => { |
| 64 | + await page.goto("/app/ask"); |
| 65 | + await page.route("**/functions/v1/ai-stream", (route) => route.fulfill({ status: 401, contentType: "application/json", body: JSON.stringify({ error: "Invalid JWT" }) })); |
| 66 | + await refuseSession(page); |
| 67 | + await page.getByRole("textbox", { name: "Message to your debate coach" }).fill("How do I weigh two impacts?"); |
| 68 | + await page.keyboard.press("Enter"); |
| 69 | + const failure = page.getByRole("alert").filter({ has: page.getByRole("button", { name: "Sign in again" }) }); |
| 70 | + await expect(failure).toBeVisible({ timeout: AI_TIMEOUT }); |
| 71 | + await expect(failure.getByRole("button", { name: "Retry" })).toHaveCount(0); |
| 72 | + await evidence.shot("01-ask-sign-in-again"); |
| 73 | + await evidence.aria("01-ask-sign-in-again", failure); |
| 74 | +}); |
| 75 | + |
| 76 | +test("fail-limit-tool: a tool refused by the daily limit opens the plan dialog", async ({ signedIn: page, user, evidence }) => { |
| 77 | + await admin.from("user_settings").update({ daily_limit_override: 0 }).eq("user_id", await user.userId()); |
| 78 | + await page.goto("/app/brainstorm"); |
| 79 | + await page.getByLabel("Motion / topic *").fill(motion); |
| 80 | + await page.getByRole("button", { name: "Start brainstorming" }).click(); |
| 81 | + await chooseForMotion(page, "once"); |
| 82 | + const dialog = page.getByRole("dialog").filter({ hasText: "Daily AI uses used up" }); |
| 83 | + await expect(dialog).toBeVisible({ timeout: AI_TIMEOUT }); |
| 84 | + await evidence.shot("01-brainstorm-limit-dialog"); |
| 85 | + await evidence.aria("01-brainstorm-limit-dialog", dialog); |
| 86 | +}); |
| 87 | + |
| 88 | +test("fail-signin-sparring: a refused session for the opponent's turn offers Sign in again instead of Retry", async ({ signedIn: page, evidence }) => { |
| 89 | + await page.goto("/app/sparring"); |
| 90 | + await page.getByLabel("Motion").fill(motion); |
| 91 | + await page.getByRole("combobox", { name: "Round format" }).click(); |
| 92 | + await page.getByRole("option", { name: /Lincoln-Douglas · 5 speeches/ }).click(); |
| 93 | + await page.getByRole("button", { name: "Start round" }).click(); |
| 94 | + await chooseForMotion(page, "create"); |
| 95 | + const draft = page.getByRole("textbox", { name: "Draft for Affirmative constructive" }); |
| 96 | + await expect(draft).toBeVisible({ timeout: AI_TIMEOUT }); |
| 97 | + await refuseSession(page); |
| 98 | + await draft.fill("Universal access removes the direct price barrier to participation in work, school, and civic life."); |
| 99 | + await page.getByRole("button", { name: "Submit speech" }).click(); |
| 100 | + await expect(page.getByRole("heading", { name: "The opponent could not answer" })).toBeVisible({ timeout: AI_TIMEOUT }); |
| 101 | + await expect(page.getByRole("button", { name: "Retry opponent turn" })).toHaveCount(0); |
| 102 | + await expect(page.getByRole("button", { name: "Sign in again" })).toBeVisible(); |
| 103 | + await evidence.shot("01-sparring-sign-in-again"); |
| 104 | +}); |
| 105 | + |
| 106 | +test("fail-signin-crossfire: a refused session for the AI's question offers Sign in again instead of Retry", async ({ signedIn: page, evidence }) => { |
| 107 | + await page.goto("/app/crossfire"); |
| 108 | + await page.getByTestId("crossfire-resolution").fill(resolution); |
| 109 | + await page.getByTestId("crossfire-argument").fill("Private cars create congestion and unsafe streets."); |
| 110 | + await page.getByTestId("mode-ai-questions-user").click(); |
| 111 | + await refuseSession(page); |
| 112 | + await page.getByTestId("start-crossfire").click(); |
| 113 | + await chooseForMotion(page, "create"); |
| 114 | + await expect(page.getByRole("button", { name: "Sign in again" })).toBeVisible({ timeout: AI_TIMEOUT }); |
| 115 | + await expect(page.getByTestId("retry-ai")).toHaveCount(0); |
| 116 | + await evidence.shot("01-crossfire-sign-in-again"); |
| 117 | +}); |
0 commit comments