import { expect, test } from "@playwright/test"

test.describe("Editor smoke", () => {
  test.beforeEach(async ({ page }) => {
    // Enforce ?e2e=true on every navigation so __testUtils__ is always
    // available and all tests share the same editor bootstrapping path.
    await page.goto("/editor?e2e=true")
  })

  test("page loads and renders editor blocks", async ({ page }) => {
    // toPass auto-retries — crucial for Vue/PM async rendering.
    await expect(async () => {
      const count = await page.locator("[data-block-id]").count()
      expect(count).toBeGreaterThanOrEqual(10)
    }).toPass({ timeout: 5000 })
  })

  test("toolbar is visible with undo/redo buttons", async ({ page }) => {
    const undo = page.locator('[data-toolbar-action="undo"]')
    const redo = page.locator('[data-toolbar-action="redo"]')
    await expect(undo).toBeVisible()
    await expect(redo).toBeVisible()
  })

  test("typing in first block changes content", async ({ page }) => {
    const firstBlock = page.locator("[data-block-id]").first()
    await firstBlock.focus()
    await page.keyboard.type("Hello Playwright")
    const content = await page.evaluate(() =>
      window.__testUtils__?.getContent(),
    )
    expect(content).toContain("Hello Playwright")
  })

  test("__testUtils__ selectText works", async ({ page }) => {
    const blockIds = await page.evaluate(() =>
      window.__testUtils__?.getBlockIds(),
    )
    expect(blockIds).toBeDefined()
    expect(blockIds?.length).toBeGreaterThan(0)
  })
})
