import { expect, test } from "@playwright/test";
import { deleteBook, importEpub } from "./fixtures";

test("imports a bilingual EPUB and opens the complete Edge player", async ({ page }) => {
  const title = `听阅导入验收-${Date.now()}`;
  let bookId: string | null = null;
  try {
    bookId = await importEpub(page, title);

    await expect(page.getByText("表格 · 默认跳过")).toBeVisible();
    await expect(page.getByText("复杂公式 · 默认跳过")).toBeVisible();
    await page.getByRole("link", { name: "立即收听" }).click();
    await page.waitForURL(new RegExp(`/books/${bookId}/listen$`));

    await expect(page.getByRole("heading", { name: title })).toBeVisible();
    await expect(page.getByLabel("逐句正文")).toContainText("中文端到端验收句");
    await expect(page.getByLabel("逐句正文")).toContainText("English end-to-end sentence");
    await expect(page.getByRole("button", { name: "播放" })).toBeVisible();
    await expect(page.getByRole("button", { name: "后退 15 秒" })).toBeVisible();
    await expect(page.getByRole("button", { name: "前进 15 秒" })).toBeVisible();
    await expect(page.getByLabel("本句播放位置")).toBeVisible();

    await page.getByLabel("播放速度").selectOption("1.5");
    await expect(page.getByLabel("播放速度")).toHaveValue("1.5");
    await page.getByLabel("睡眠定时").selectOption("chapter");
    await expect(page.getByText("睡眠定时：本章结束")).toBeVisible();

    await expect.poll(() => page.evaluate(() => navigator.mediaSession?.metadata?.album ?? null))
      .toBe(title);
  } finally {
    await deleteBook(page, bookId);
  }
});
