/**
 * Full-browser check (Playwright) for the same class of bug as
 * `src/components/InitiativeApplicationForm.draftTyping.integration.test.tsx`.
 *
 * Requires a **running** frontend (default E2E_BASE_URL) and an **authenticated** applicant
 * session with the « Đơn Đề nghị Công nhận » tab reachable (Báo cáo gate completed if applicable).
 *
 * Run (example):
 *   cd fe0 && npm run dev
 *   E2E_FORM_TYPING=1 npm run test:e2e -- e2e/initiative-application-form-typing.spec.ts
 */
import { test, expect } from "@playwright/test";

test.describe("Initiative Đơn form — keystroke regressions", () => {
  test.skip(
    process.env.E2E_FORM_TYPING !== "1",
    "Opt-in only: export E2E_FORM_TYPING=1 with a logged-in dev server.",
  );

  test("browser typing does not surface React Maximum update depth in console", async ({ page }) => {
    const panics: string[] = [];
    page.on("console", (msg) => {
      if (msg.type() !== "error" && msg.type() !== "warning") return;
      const t = msg.text();
      if (/Maximum update depth exceeded|Too many re-renders/u.test(t)) panics.push(t);
    });

    await page.goto("/dashboard", { waitUntil: "domcontentloaded", timeout: 30_000 });
    const appTab = page.getByRole("tab", { name: /Đơn Đề nghị Công nhận/u });
    await expect(appTab).toBeVisible({ timeout: 20_000 });
    await appTab.click();

    const summary = page.locator("textarea.min-h-32").first();
    await expect(summary).toBeVisible({ timeout: 25_000 });
    await page.getByPlaceholder("Nhập tên sáng kiến...").focus();
    await page.keyboard.insertText("Đánh máy ");
    await summary.click();
    await page.keyboard.insertText("Mô tả từng ký tự ");
    await page.waitForTimeout(500);
    expect(panics).toEqual([]);
  });
});
