/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("RTF preview retains strikethrough and vertical text positioning", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("RTF styles", { "text-styles.rtf": String.raw`{\rtf1\ansi Strikethrough: {\strike old}\par Superscript: E=mc\super 2\nosupersub\par Subscript: H\sub 2\nosupersub O\par Mixed: \strike\super note\plain  plain text}` });
  const thread = await t3.thread({ project, title: "Review RTF styles", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Review RTF styles").waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("text-styles.rtf");
  await screen.getByRole("option", "text-styles.rtf", { exact: false }).tap();
  await screen.getByText("1 page", { exact: false }).waitFor({ timeout: 120_000 });
  const frame = browser.frameLocator('iframe[title^="text-styles.rtf"]');
  await frame.getByText("Strikethrough:", { exact: false }).waitFor();
  await app.screenshot("rtf-text-styles");
  const formatting = await browser.evaluate(() => {
    const iframe = document.querySelector<HTMLIFrameElement>('iframe[title^="text-styles.rtf"]')!;
    const doc = new DOMParser().parseFromString(iframe.srcdoc, "text/html");
    return {
      strike: [...doc.querySelectorAll("s")].some(el => el.textContent === "old"),
      super: [...doc.querySelectorAll("sup")].some(el => el.textContent === "2"),
      sub: [...doc.querySelectorAll("sub")].some(el => el.textContent === "2"),
      mixed: [...doc.querySelectorAll("s sup, sup s")].some(el => el.textContent === "note"),
      reset: ![...doc.querySelectorAll("s, sup, sub")].some(el => el.textContent?.includes("plain text") || el.textContent?.includes(" O")),
    };
  });
  expect(formatting).toEqual({ strike: true, super: true, sub: true, mixed: true, reset: true });
});
