import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const CHAPTERS = String.raw`{\rtf1\ansi\pard Introduction\par
{\pagebb Chapter opening\par}
Closing note\par}`;

test("RTF group restores page break before after its paragraph", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo", { "chapters.rtf": CHAPTERS });
  const thread = await t3.thread({ project, title: "Review chapters", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByTestId("composer-editor").waitFor();
  await browser.keyboard.press("Control+p");
  const readme = screen.getByRole("option", "README.md", { exact: false });
  await readme.waitFor();
  await screen.getByPlaceholder("Search files…").fill("chapters.rtf");
  await readme.waitFor({ state: "hidden" });
  await screen.getByRole("option", "chapters.rtf", { exact: false }).tap();
  const body = browser.frameLocator('iframe[title^="chapters.rtf"]').locator("body");
  await body.getByText("Introduction", { exact: true }).waitFor({ timeout: 120_000 });
  await screen.getByRole("button", "Next page", { exact: true }).tap();
  await body.getByText("Chapter opening", { exact: false }).waitFor();
  await app.screenshot("chapter-opening-and-closing-note");
  await expect(body).toHaveText("Chapter opening Closing note");
  await expect(screen.getByText("Page 2 of 2", { exact: true })).toBeVisible();
});
