import { expect } from "e2e";

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

const CHAPTERS = String.raw`{\rtf1\ansi\pard\pagebb Chapter one\par
Chapter two\par
Chapter three\par
\pard Closing note\par}`;

test(
  "An RTF page break before applies to following paragraphs until reset",
  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"], model: "default" });
    await t3.open(thread);
    await screen.getByRole("textbox", "Message Review chapters").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();

    // The first document preview loads its worker, which a cold Vite cache optimizes on demand.
    const document = browser.frameLocator('iframe[title^="chapters.rtf"]');
    const body = document.locator("body");
    await expect(body).toContainText("Chapter one", { timeout: 120_000 });
    await app.screenshot("first-page");
    await expect(body).toHaveText("Chapter one");
    await expect(screen.getByText("Page 1 of 3", { exact: true })).toBeVisible();

    const next = screen.getByRole("button", "Next page", { exact: true });
    await next.tap();
    await expect(body).toHaveText("Chapter two");
    await next.tap();
    await expect(body).toHaveText("Chapter three Closing note");
  },
);
